Use of Logical Operators

C allows usage of three logical operators, namely, &&, || and !. These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively. There are several things to note about these logical operators. Most obviously, two of them are composed of double symbols: || and &&

Example: The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules:

Percentage above or equal to 60 – First division

Percentage between 50 and 59 – Second division

Percentage between 40 and 49 – Third division

Percentage less than 40 – Fail

main( ) 
{ 
int m1, m2, m3, m4, m5, per ; 
printf ( "Enter marks in five subjects " ) ; 
scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ; 
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 ) 
printf ( "First division" ) ; 
if ( ( per >= 50 ) && ( per < 60 ) ) 
printf ( "Second division" ) ; 
if ( ( per >= 40 ) && ( per < 50 ) ) 
printf ( "Third division" ) ; 
if ( per < 40 ) 
printf ( "Fail" ) ; 
} 

As can be seen from the second if statement, the && operator is used to combine two conditions. ‘Second division’ gets printed if both the conditions evaluate to be true. If one of the conditions evaluates as false then the whole thing is treated as false.