# 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.

## Related Posts

• 88
In this article we will learn about the NOT operator, written as !. This operator reverses the result of the expression it operates on. For example, if the expression evaluates to a non-zero value, then applying ! operator to it results into a 0. Vice versa, if the expression evaluates…
Tags: operator, logical, program, learn, programming, write, tutorial
• 75
Since we have now added the logical operators to the list of operators we know, it is time to review these operators and their priorities. The higher the position of an operator is in the table, higher is its priority. The following figure summarizes the working of all the three…
Tags: operators, logical, three, operator, learn, program, programming, write, tutorial
• 74
Like most languages, C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this: if ( this condition is true )       execute this statement ; The keyword if tells the compiler that what follows is a decision control…
Tags: printf, statement, program, operators, operator, learn, programming, write, tutorial
• 72
Let's have an example to understand else if clause : /* else if ladder demo */ main( ) { int m1, m2, m3, m4, m5, per ; per = ( m1+ m2 + m3 + m4+ m5 ) / per ; if ( per >= 60 ) printf ( "First…
Tags: printf, program, learn, programming, write, tutorial
• 72
The if statement can take any of the following forms: if ( condition ) do this ; if ( condition ) { do this ; and this ; } if ( condition ) do this ; else do this ; if ( condition ) { do this ; and this…
Tags: learn, program, write, programming, tutorial

#### Satya Prakash

VOIP Expert: More than 8 years of experience in Asterisk Development and Call Center operation Management. Unique Combination of Skill Set as IT, Analytics and operation management.