12 Class Notes
12 Class Notes
Operators are nothing but symbols that tell the compiler to perform some
specific operations. Operators are of the following types -
1. Arithmetic Operators
Arithmetic operators perform some arithmetic operation on one or two
operands. Operators that operate on one operand are called unary
arithmetic operators and operators that operate on two operands are
called binary arithmetic operators.
+,-,*,/,% are binary operators.
++, -- are unary operators.
++ Incrementer A++ = 6
-- Decrementer A-- = 4
int a=10;
int b;
b = a++;
cout<<a<<" "<<b<<endl;
Output : 11 10
int a=10;
int b;
b = ++a;
cout<<a<<" "<<b<<endl;
Output : 11 11
2. Relational Operators
Relational operators define the relation between 2 entities.
They give a boolean value as result i.e true or false.
> Gives true if left operand is more than right A>B is not
operand true
< Gives true if left operand is less than right operand A<B is true
>= Gives true if left operand is more than right A>=B is not
operand or equal to it true
<= Gives true if left operand is more than right A<=B is true
operand or equal to it
Example -
int n;
cin>>n;
if(n<10){
cout<<"Less than 10"<<endl;
}
else if(n==10){
cout<<"Equal to 10"<<endl;
}
else{
cout<<"More than 10"<<endl;
}
3. Logical Operators
Logical operators are used to connect multiple expressions or conditions
together.
We have 3 basic logical operators.
&& AND operator. Gives true if both operands are non- (A && B) is
zero false
Example -
If we need to check whether a number is divisible by both 2 and 3, we will
use AND operator
If this expression gives true value then that means that num is divisible by
both 2 and 3.
(num%2==0) || (num%3==0)
If this expression gives true value then that means that num is divisible by
2 or 3 or both.
4. Bitwise Operators
Bitwise operators are the operators that operate on bits and perform bit-
by-bit operations.
5. Assignment Operators
= Assigns value of right operand to left operand A=B will put value
of B in A
-= Subtracts right operand from the left operand A-=B means A=A-B
and assigns the result to left operand.
*= Multiplies right operand with the left operand A*=B means A=A*B
and assigns the result to left operand.
/= Divides left operand with the right operand and A/=B means A=A/B
assigns the result to left operand.
6. Misc Operators
Precedence of Operators
Category Operator Associativity
Examples
1.
#include<stdio.h>
int main()
{
int a = 5;
a = 1, 2, 3;
printf("%d", a);
return 0;
}
Ans. 1
(Priority for the values assigned to any variable is given from left to
right)
2.
#include<stdio.h>
int main()
{
int a;
a = (1, 2, 3);
printf("%d", a);
return 0;
}
Ans. 3
(Priority for the values inside a brackets () assigned to any variable is
given from right to left.)
3.
#include<stdio.h>
int main()
{
int x = 2;
(x & 1) ? printf("true") : printf("false");
return 0;
}
Ans. False
(As & is a unary operator we have to assume all decimal values to
binary(0's and 1's)
210 = 00000010 2
Now we go for condition (00000010 & 00000001)
Clearly, condition false as it leads to 0 when multiplied.)
4.
#include<stdio.h>
int main()
{
printf("%d",3 * 2--);
}
Ans. 6
(2-- stands meaningless)
5.
#include<stdio.h>
int main()
{
int i = 10;
i++;
i * i;
printf("%d\n",i);
return 0;
}
Ans. 11
(i++ alone store the result in variable i.)
6.
#include<stdio.h>
int main()
{
int a = 1, b = 3, c;
c = b << a;
b = c * (b * (++a)--);
a = a >> b;
printf("%d",b);
return 0;
}
Ans. 36
(c = 0011 << 1
c = 0110
b = 6* (3*(2)--)
b = 6*6
)