890 likes | 1.19k Vues
Operators. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations and is usually used to manipulate data and variables” Ex: a+b. Operators. Arithmetic operators Relational operators Logical operators
E N D
Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations and is usually used to manipulate data and variables” Ex: a+b
Operators • Arithmetic operators • Relational operators • Logical operators • Assignment operators • Increment and decrement operators • Conditional operators • Bitwise operators • Special operators
Here a and b are variables and are known as operands. • Integer Arithmetic: when both the operands in a single arithmetic expression such as a+b are integers. The expression is called an integer expression, and the operation is called an integer arithmetic. Examples: if a=14, b=4 then a-b=10 a+b=18 a*b=56 a/b=3 (decimal part is truncated) a%b=2 (remainder of division)
In Integer Division , 6/7=0; -6/-7=0; -6/7 is 0 or -1. In modulo division, the sign of the result is always the sign of the first operand. -14%3=-2; -14%-3 =-2; 14%-3=2; • Real Arithmetic: • An arithmetic operation involving only real operands is called real arithmetic.
If x, y and z are floats ,then we will have x=6.0/7.0=0.857143 y=1.0/3.0=0.333333 z=-2.0/3.0=-0.666667 The operator % can not be used with real operands. Mixed Mode Arithmetic: When one of the operands is real and the other is integer, the expression is called a mixed-mode arithmetic expression.
If either operand is of the real type, then only the real operation is performed and the result is always a real number. 15/10.0=1.5 where as 15/10=1
Example #include <iostream> using namespace std; main() { int a = 21; int b = 10; int c ; c = a + b; cout << "Line 1 - Value of c is :" << c << endl ; c = a - b; cout << "Line 2 - Value of c is :" << c << endl ;
c = a * b; cout << "Line 3 - Value of c is :" << c << endl ; c = a / b; cout << "Line 4 - Value of c is :" << c << endl ; c = a % b; cout << "Line 5 - Value of c is :" << c << endl ; return 0; }
OUTPUT • Line 1 - Value of c is :31 • Line 2 - Value of c is :11 • Line 3 - Value of c is :210 • Line 4 - Value of c is :2 • Line 5 - Value of c is :1
An expression containing a relational operator is termed as a relational expression. • Ex: a<b or 1<20 • The value of a relational expression is either one or zero. • 1 –true • 0-false • Relational expressions are used in decision statements such as if and while .
Examples • 4.5 <= 10 TRUE • 4.5 < -10 FALSE • -35 >=0 FALSE • 10 <7+5 TRUE • a+b =c+d TRUE iff sum of values of and b is equal to the sum of values of c and d.
Relational operator Complements • > is complement of <= • < is complement of >= • == is complement of !=
Logical operators && and || are used when we want to test more than one condition and make decisions. Ex: a>b && x==10 • An expression that combines two or more relational expressions is called as Logical expression or a compound relational expression • Ex: if (a==b && b==c)
Relative Precedence Highest ! > >= < <= == != && Lowest ||
Assignment operators Assignment operators are used to assign the result of an expression to a variable. Assignment Operator ‘=‘. Syntax: v op = exp; Where v = variable, op = shorthand assignment operator exp = expression Ex: x=x+3 x+=3
Advantages of shorthand Assignment operators • What appears on the left-hand side need not be repeated and therefore it becomes easier to write. • The statement is more concise and easier to read. • The statement is more efficient.
Program for bitwise operators #include <iostream.h> using namespace std; main() { unsigned int a = 60; // 60 = 0011 1100 unsigned int b = 13; // 13 = 0000 1101 int c = 0; c = a & b; // 12 = 0000 1100 cout << "Line 1 - Value of c is : " << c << endl ; c = a | b; // 61 = 0011 1101 cout << "Line 2 - Value of c is: " << c << endl ;
c = a ^ b; // 49 = 0011 0001 cout << "Line 3 - Value of c is: " << c << endl ; c = ~a; // -61 = 1100 0011 cout << "Line 4 - Value of c is: " << c << endl ; c = a << 2; // 240 = 1111 0000 cout << "Line 5 - Value of c is: " << c << endl ; c = a >> 2; // 15 = 0000 1111 cout << "Line 6 - Value of c is: " << c << endl ; return 0; }
output • Line 1 - Value of c is : 12 • Line 2 - Value of c is: 61 • Line 3 - Value of c is: 49 • Line 4 - Value of c is: -61 • Line 5 - Value of c is: 240 • Line 6 - Value of c is: 15
Increment & Decrement Operators C supports 2 useful operators namely • Increment ++ • Decrement --operators The ++ operator adds a value 1 to the operand The -- operator subtracts 1 from the operand ++a or a++ (++a is equivalent to a=a+1 or a+=1;) --a or a-- (--a is equivalent to a=a-1 or a-=1);
Rules for ++ & -- operators • These require variables as their operands • When postfix either ++ or – is used with the variable in a given expression, the expression is evaluated first and then it is incremented or decremented by one • When prefix either ++ or – is used with the variable in a given expression, it is incremented or decremented by one first and then the expression is evaluated with the new value
Examples for ++ & -- operators Let the value of a =5 and b=++a then a = b =6 Let the value of a = 5 and b=a++ then a =6 but b=5 i.e.: 1. a prefix operator first adds 1 to the operand and then the result is assigned to the variable on the left 2. a postfix operator first assigns the value to the variable on left and then increments the operand.
program for increment operator : #include <iostream> using namespace std; main() { int a = 21; int c ; // Value of a will not be increased before assignment. c = a++; cout << "Line 1 - Value of a++ is :" << c << endl ; // After expression value of a is increased cout << "Line 2 - Value of a is :" << a << endl ; // Value of a will be increased before assignment.
c = ++a; cout << "Line 3 - Value of ++a is :" << c << endl ; return 0; } OUTPUT Line 1 - Value of a++ is :21 Line 2 - Value of a is :22 Line 3 - Value of ++a is :23
Conditional operators A ternary operator pair “? : “ is available in c to construct conditional expressions of the form Syntax: exp1 ? exp2 : exp3 where exp1,exp2 and exp3 are expressions Working of the ? Operator: Exp1 is evaluated first, if it is nonzero(1/true) then the expression2 is evaluated and this becomes the value of the expression, If exp1 is false(0/zero) exp3 is evaluated and its value becomes the value of the expression Ex: m=2; n=3 r=(m>n) ? m : n;
#include <iostream> using namespace std; int main () { // Local variable declaration: int x, y = 10; x = (y < 10) ? 30 : 40; cout << "value of x: " << x << endl; return 0; }
output • value of x: 40
Bitwise operators These operators allow manipulation of data at the bit level. These operators are used to perform bit operations. Decimal values are converted into binary values which are the sequence of bits and bit wise operators work on these bits.
Special operators • Comma operator ( ,) • sizeof operator – sizeof( ) • Pointer operators – ( & and *) • Member selection operators – ( . and ->)
Comma operator can be used to link the related expressions together . • sizeof() operator is used to find the memory space allocated for each C data types.
Operators in C++ • :: scope resolution operator • ::* pointer-to member declarator • ->* pointer-to member operator • .* pointer-to member operator • delete memory release operator • new memory allocation operator • endl linefeed operator • setw field width operator
Memory management operators • new • delete
new • An object can be created using ‘new ‘operator. • A data object created inside a block with new,will remain in existence until it is explicitly destroyed by using delete. Syntax: pointer _variable=new data-type; Ex: p=new int; q=new float;
Here, pointer_variable is a pointer of type data-type. • The new operator allocates sufficient memory to hold data object of type data-type and returns the address of the object. int *p = new int; float *q=new float
// initializing the memory using new • pointer_variable = new data_type (value); int *p = new int(25); //create memory space for array Pointer_variable = new data_type [size]; int *p=new int[10];
delete • when a data object is no longer needed, it is destroyed to release the memory space for reuse. • delete pointer_varaible; • Ex: delete p; • delete [size] pointer_varaible; • Ex: delete []p;
Use of new and delete operators #include<iostream.h> void main() { int *arr; int size; cout<< “enter size of the integer array”; cin>>size; cout<<“creating an array of size”<<size; arr = new int[size]; cout<<“memory allocated successfully”; delete arr; getch(); }
output • enter size of the integer array 6 • creating an array of size 6 • memory allocated successfully
#include <iostream> using namespace std; int main () { double *pvalue = NULL; // Pointer initialized with null pvalue = new double; // Request memory for the variable *pvalue = 29494.99; // Store value at allocated address cout << "Value of pvalue : " << *pvalue << endl; delete pvalue; // free up the memory. return 0; }
output • Value of pvalue: 29494.99
Note: • If sufficient memory is not available for allocation, then new returns a null pointer. p=new int; if(!p) { cout<<“allocation failed”; }
Advantages of new • It automatically computes the size of the data object. Need not use sizeof operator. • It automatically returns the correct pointer type , so that there is no need to use a type cast. • It is possible to initialize the object while creating the memory space. • Like any other operator, new and delete can be overloaded.
Manipulators • Manipulators are used to format the data display. • endl • setw