-----------------------------------------------------------------------------------
---------------------
Relational operator
-----------------------------------------------------------------------------------
---------------------
- greater than ( > )
- less than ( < )
- less than equal to (<= )
- greater than equal to ( >= )
- comparison ( == )
- not equal to ( != )
-----------------------------------------------------------------------------------
-----------------------
conditional or ternary operator
-----------------------------------------------------------------------------------
------------------------
syntax:
(condition) ? statement 1 : statement 2;
example:
int m = 2,n = 5;
(m == n) ? cout<<"both are equal ") : cout<<"both are not equal ";
-----------------------------------------------------------------------------------
--------------------
increment and decrement operator
-----------------------------------------------------------------------------------
--------------------
(++ ) increment by 1
( -- ) decrement by 1
1. prefix 2.postfix
++a a++
-- b b--
++c c++
int a= 3;
a++;//3
a--;//2
cout<<"a = %d\n",a++;//3
cout<<"a = %d\n",++a;//4
cout<<"a = %d\n",a--;//3
cout<<"a= %d\n",a;//3
-----------------------------------------------------------------------------------
------------------
arithmatic operator
-----------------------------------------------------------------------------------
-----------------
example:
int n1 = 43, n2 = 54, n3 = 68;
clrscr();
cout<<"addition is : %d\n",n1+n2;
cout<<"subtraction is : %d\n",n2-n3;
cout<<"multiplication is : %d\n",n3*n1;
cout<<"division is : %d\n",n2/n1;
cout<<"modulus is : %d\n",n3%n2;
-----------------------------------------------------------------------------------
--------------------
Assignment operator
-----------------------------------------------------------------------------------
--------------------
example:
int n1 = 43, n2 = 54, n3 = 68;
clrscr();
cout<<"addition is : %d\n",n1+=n2;
cout<<"n1 = %d, n2 = %d\n",n1,n2;
cout<<"subtraction is : %d\n",n2-=n3;
cout<<"n2 = %d, n3 = %d\n",n2,n3;
cout<<"multiplication is : %d\n",n3*=n1;
cout<<"n3 = %d, n1 = %d\n",n1,n2;
cout<<"division is : %d\n",n2/=n1;
cout<<"n2 = %d, n1 = %d\n",n1,n2;
-----------------------------------------------------------------------------------
-------------------------
Logical operators in c
-----------------------------------------------------------------------------------
-------------------------
a) AND (&&)
b) OR ( || )
c) NOT ( ! )
1 = true
0 = false
syntax:
AND(&&)
condition 1 condition 2 result
True True True
True False False
False True False
False False False
OR( | | )
condition 1 condition 2 result
True True True
True False True
False True True
False False False
NOT ( ! )
True False
False True
Example:
float f1 = 4.5, f2 =5.6;
0*1=0
(f1>f2) && (f2 != f1) ? cout<<"It's True" : cout<<"It's
False"; //f
(f1>f2) || (f2 == f1) ? cout<<"It's True" : cout<<"It's
False";//f
(f2 <= f2) && (f2 >=f1) ? cout<<"It's True" : cout<<"It's
False";//t
(f1==f2) && !(f2 != f1) ? cout<<"It's True" : cout<<"It's
False";//f
!(f1<=f2) || (f2 != f2) ? cout<<"It's True" : cout<<"It's
False";//f