Decision Control Instruction

Decision Control Instruction allows the computer to make a decision as to which instruction is to be executed next.

There are several programming situations where we need to take decisions, C language too must be able to perform different sets of actions depending on the circumstances.

C has three major decision-making instructions. These are:-

  1. if statement
  2. if-else statement
  3. switch statement  

1) The if Statement

Like most languages, C uses the keyword if to implement the decision control instruction.

The general form of if statement or its syntax looks like this:

if ( this condition is true ) 
execute this statement; 

or

if ( this condition is true ) 
{
    statement ;
    statement ;
    statement ;
    .
    .
    .
    . 
}

The keyword if tells the compiler that what follows is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is, is true, then the statement is executed. If the condition is not true then the statement is not executed. As a general rule, we express a condition using C’s ‘relational’ operators. The relational operators allow us to compare two values to see whether they are equal to each other, unequal, or whether one is greater than the other. Here’s how they look and how they are evaluated in C.

expressions true if conditions

Example of if statement: The current year and the year in which the employee joined the organization are entered through the keyboard. If the number of years for which the employee has served the organization is greater than 3 then a bonus of Rs. 2500/- is given to the employee. If the years of service are not greater than 3, then the program should do nothing.

/* Calculation of bonus */ 

main( ) 
{ 
    int bonus, cy, yoj, yr_of_ser ; 
    printf ( "Enter current year and year of joining " ) ; 
    scanf ( "%d %d", &cy, &yoj ) ; 
    yr_of_ser = cy - yoj ; 
    if ( yr_of_ser > 3 ) 
    { 
        bonus = 2500 ; 
 
        printf ( "Bonus = Rs. %d", bonus ) ; 
    } 
} 

Observe that here the two statements to be executed on the satisfaction of the condition have been enclosed within a pair of braces. If a pair of braces is not used then the C compiler assumes that the programmer wants only the immediately next statement after the if to be executed on the satisfaction of the condition. In other words, we can say that the default scope of the if statement is the immediately next statement after it.  

2) The If-else Statement

The if statement by itself will execute a single statement, or a group of statements when the expression following it evaluates to true. It does nothing when the expression evaluates to false. We can execute a statement or a group of statements if the expression evaluates to false. The general form of an if-else statement or its syntax looks like this:

if ( condition ) 
{ 
    do this;
    and this; 
} 
else 
{ 
    do this; 
    and this; 
}

Or

if ( condition ) 
    do this;
else 
    and this; 

In the above syntax if the condition returns true then the statements written in the if block is executed. If it returns false then control transfers to the else block.

Example of if-else Statement: We can understand it with the help of the following example. In a company, an employee is paid as under If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of basic salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of basic salary. If the employee’s salary is input through the keyboard write a program to find his gross salary.

/* Calculation of gross salary */ 
main( ) 
{ 
float bs, gs, da, hra ; 
printf ( "Enter basic salary " ) ; 
scanf ( "%f", &bs ) ; 
if ( bs < 1500 ) 
{ 
hra = bs * 10 / 100 ; 
da = bs * 90 / 100 ; 
} 
else 
{ 
hra = 500 ; 
da = bs * 98 / 100 ; 
} 
gs = bs + hra + da ; 
printf ( "gross salary = Rs. %f", gs ) ; 
}

A few points worth noting…

  1. The group of statements after the if upto and not including the else is called an ‘if block’. Similarly, the statements after the else form the ‘else block’.
  2. Notice that the else is written exactly below the if. The statements in the if block and those in the else block have been indented to the right.
  3. Had there been only one statement to be executed in the if block and only one statement in the else block we could have dropped the pair of braces.
  4. As with the if statement, the default scope of else is also the statement immediately after the else. To override this default scope a pair of braces as shown in the above example must be used.

Forms of if and if-else

The if statement can take any of the following forms:

(a) if ( condition ) 
        do this ; 

(b) if ( condition ) 
    { 
        do this ; 
        and this ; 
    }
 
(c) if ( condition ) 
        do this ; 
    else 
        do this ;
 
(d) if ( condition ) 
    { 
        do this ;
        and this ; 
    } 
    else 
    { 
        do this ; 
        and this ; 
    }
 
(e) if ( condition ) 
        do this ; 
    else 
    { 
        if ( condition ) 
            do this ; 
        else 
        { 
  
            do this ; 
            and this ; 
        } 
    } 

(f) if ( condition ) 
    { 
        if ( condition ) 
            do this ; 
        else 
        { 
            do this ; 
            and this ; 
        } 
  
     } 
     else 
         do this ;