Wednesday, October 26, 2011

The else if Clause

There is one more way in which we can write program for Example 2.4. This involves usage of else if blocks as shown below:
/* else if ladder demo */
main( )
{
int m1, m2, m3, m4, m5, per ;
per = ( m1+ m2 + m3 + m4+ m5 ) / per ;
if ( per >= 60 )
printf ( "First division" ) ;
else if ( per >= 50 )
printf ( "Second division" ) ;
else if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "fail" ) ;
}

You can note that this program reduces the indentation of the statements. In this case every else is associated with its previous if. The last else goes to work only if all the conditions fail. Even in else if ladder the last else is optional.

Note that the else if clause is nothing different. It is just a way of rearranging the else with the if that follows it. This would be evident if you look at the following code:

if ( i == 2 ) if ( i == 2 )
printf ( "With you…" ) ; printf ( "With you…" ) ;
else elseif(j==2)
{ printf("…Allthetime");
if ( j == 2 )
printf ( "…All the time" ) ;
}

Another place where logical operators are useful is when we want to write programs for complicated logics that ultimately boil down
to only two answers. For example, consider the following example:

Example 2.5: A company insures its drivers in the following cases:

− If the driver is married.
− If the driver is unmarried, male & above 30 years of age.
− If the driver is unmarried, female & above 25 years of age.
In all other cases the driver is not insured. If the marital status, sex and age of the driver are the inputs, write a program to determine whether the driver is to be insured or not.
Here after checking a complicated set of instructions the final output of the program would be one of the two—Either the driver should be ensured or the driver should not be ensured. As mentioned above, since these are the only two outcomes this problem can be solved using logical operators. But before we do that let us write a program that does not make use of logical operators.
/* Insurance of driver - without using logical operators */

main( )
{
char sex, ms ;
int age ;
printf ( "Enter age, sex, marital status " ) ;
scanf ( "%d %c %c", &age, &sex, &ms ) ;
if ( ms == 'M' )
printf ( "Driver is insured" ) ;
else
{
if ( sex == 'M' )
{
if ( age > 30 )
printf ( "Driver is insured" ) ;
else
printf ( "Driver is not insured" ) ;
}
else
{
if ( age > 25 )
printf ( "Driver is insured" ) ;
else
printf ( "Driver is not insured" ) ;
}
}
}

From the program it is evident that we are required to match several ifs and elses and several pairs of braces. In a more real-life situation there would be more conditions to check leading to the program creeping to the right. Let us now see how to avoid these problems by using logical operators.
As mentioned above, in this example we expect the answer to be either ‘Driver is insured’ or ‘Driver is not insured’. If we list down all those cases in which the driver is insured, then they would be: 

(a) Driver is married.

(b) Driver is an unmarried male above 30 years of age.

(c) Driver is an unmarried female above 25 years of age.

Since all these cases lead to the driver being insured, they can be combined together using && and || as shown in the program below:
/* Insurance of driver - using logical operators */

main( )
{
char sex, ms ;
int age ;
printf ( "Enter age, sex, marital status " ) ;
scanf ( "%d %c %c" &age, &sex, &ms ) ;
if ( ( ms == 'M') || ( ms == 'U' && sex == 'M' && age > 30 ) ||
( ms == 'U' && sex == 'F' && age > 25 ) )
printf ( "Driver is insured" ) ;
else
printf ( "Driver is not insured" ) ;
}
In this program it is important to note that:
− The driver will be insured only if one of the conditions enclosed in parentheses evaluates to true.
− For the second pair of parentheses to evaluate to true, each condition in the parentheses separated by && must evaluate to true.
− Even if one of the conditions in the second parentheses evaluates to false, then the whole of the second parentheses evaluates to false.
− The last two of the above arguments apply to third pair of parentheses as well.
Thus we can conclude that the && and || are useful in the following programming situations:

(a) When it is to be tested whether a value falls within a particular range or not.

(b)When after testing several conditions the outcome is only one of the two answers (This problem is often called yes/no problem).
There can be one more situation other than checking ranges or yes/no problem where you might find logical operators useful. The following program demonstrates it.

Example 2.6: Write a program to calculate the salary as per the following table:





main( )
{
char g ;
int yos, qual, sal ;
printf ( "Enter Gender, Years of Service and
Qualifications ( 0 = G, 1 = PG ):" ) ;
scanf ( "%c%d%d", &g, &yos, &qual ) ;
if ( g == 'm' && yos >= 10 && qual == 1 )
sal = 15000 ;
else if ( ( g == 'm' && yos >= 10 && qual == 0 ) ||
( g == 'm' && yos < 10 && qual == 1 ) )
sal = 10000 ;
else if ( g == 'm' && yos < 10 && qual == 0 )
sal = 7000 ;
else if ( g == 'f' && yos >= 10 && qual == 1 )
sal = 12000 ;
else if ( g == 'f' && yos >= 10 && qual == 0 )
sal = 9000 ;
else if ( g == 'f' && yos < 10 && qual == 1 )
sal = 10000 ;
else if ( g == 'f' && yos < 10 && qual == 0 )
sal = 6000 ;
printf ( "\nSalary of Employee = %d", sal ) ;
}

No comments:

Post a Comment