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 &&. Don’t use the single symbol | and &. These single symbols also have a meaning. They are bitwise operators, which we would examine in Chapter 14.
The first two operators, && and ||, allow two or more conditions to be combined in an if statement. Let us see how they are used in a program. Consider the following example.
Example 2.4: 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.
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" ) ;
}
}
}
This is a straight forward program. Observe that the program uses nested if-elses. This leads to three disadvantages:
(a)
(b)
(c)
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 elses.
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" ) ;
}
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:
(a)
(b)
The matching (or do I say mismatching) of the ifs with their corresponding elses gets avoided, since there are no elses 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 elses and matching of opening and closing braces that much more difficult.
Popular posts
-
विश्वविद्यालय लानुपर्ने प्रवेश परीक्षाको उत्तरपुस्तिका विश्वविद्यालयका पदाधिकारी र कलेजका सञ्चालक मिलेर काठमाडौँको होटलमा जाँचेको उजुरी परे...
-
साथमा न श्रीमान् नै छन् न त छोराछोरी । रात बिताउने कुटी भए पनि बिहान बेलुकाको आहारा जुटाउने खेतबारी पनि छैन । विवाह गरेको वर्ष दिन नबित्दै र...
-
Make better use of the XP login screen . If your system contains more than one user account, or if you've set up XP to require logins,...
-
EPIDEMIOLOGY Lung cancer, broadly divided into small cell lung cancer (SCLC) and non-small cell lung cancer (NSCLC), is the leading cause ...
-
Dear Friends, This is the space provided by the google at free of cost, i thought why don't i use it to get some help for the users who ...
-
Introduction Drugs are the cornerstone of modern therapeutics. Nevertheless, it is well recognized among physicians and in the lay communi...
-
Small cell lung cancer (SCLC) represents approximately 15% to 25% of lung cancers. SCLC is characterized by its high growth fraction, rapi...
-
EPIDEMIOLOGY Lung cancer, broadly divided into small cell lung cancer (SCLC) and non-small cell lung cancer (NSCLC), is the leading cause ...
-
One of the toughest challenges facing any designer is the web page. There are perhaps millions of pages in the World Wide Web all jostling ...
-
N cell launched wi-fi router for multiple users Ncell have just yesterday launched the wi-fi routers, it was quite intresteting topic and sh...
Aug 2, 2011
Subscribe to:
Post Comments (Atom)


