Logical Operator in C
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 &&. Don’t use the single symbol | and &. These single symbols also have a meaning.
The first two operators, && and ||, allow two or more conditions to be combined in an if statement.
Example:
Problem Statement:
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
Write a program to calculate the division obtained by the student.
Solution:
There are two ways in which we can write a program for this example. These methods are given below.
/* Method – I */
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 “) ;
else
{
if ( per >= 50 )
printf ( “Second division” ) ;
else
{
if ( per >= 40 )
printf ( “Third division” ) ;
else
printf ( “Fail” ) ;
}
}
}
Program Explanation:
This is a straight forward program. Observe that the program uses nested if-else. This leads to three disadvantages:
- As the number of conditions go on increasing the level of indentation also goes on increasing. As a result the whole program creeps to the right.
- Care needs to be exercised to match the corresponding ifs and else.
- Care needs to be exercised to match the corresponding pair of braces.
All these three problems can be eliminated by usage of ‘Logical operators’. The following program illustrates this.
/* Method – II */
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” ) ;
}
Program Explanation:
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 true. If one of the conditions evaluate to false then the whole thing is treated as false.
Two distinct advantages can be cited in favour of this program:
- The matching (or do I say mismatching) of the ifs with their corresponding else gets avoided, since there are no else in this program.
- In spite of using several conditions, the program doesn’t creep to the right. In the previous program the statements went on creeping to the right. This effect becomes more pronounced as the number of conditions go on increasing. This would make the task of matching the ifs with their corresponding else and matching of opening and closing braces that much more difficult.