ProgrammerGuide.Net | Step by Step Programmer Guide

C-Decision Making & Branching

Decision Making & Branching,

INTRODUCTION
We have seen  that a C program is a set of statement which are normally executed sequentially in the order in which they appear. This happens when no option or no repetitions of certain calculation are necessary . However , in practice  , we have a number of situation where we many have to change the order of execution of statement based on certain conditions, or repeat group of statements until certain  specified condition are met.

C language possesses such decision – making capabilities by supporting the following statement

1. if statement
2. switch statement
3. conditional operator statement
4. goto  statement

These statements are popularly known as decision – making statements. Since these statements ‘control’ the flow of execution , they are also known as control statements.

DECISION MAKING WITH IF STATEMENT 
The if statement is a powerful decision – making system statement and it used to control flow of execution of statement . It is basically a two – way decision statement and in conjunction  with an expression .It allows the computer to evaluate the expression first and then , depending on whether the value of the expression (relation or condition) is ‘true’ ( or non-zero) or ‘false’ (zero), it transfers the control  to a particular statement.

Some examples of decision making, using  if statements are:

1.   if  (bank balance is zero)
borrow money

2.  if  (room is dark )  
put on lights

The  if  statement  may be implemented  in a diffrent form s depending on the complexity of condition to be tested. The different forms are:

1. Simple  if  statement
2. if ……else statement
3. Nested  if ……else statement
4. else if statement.

SIMPLE IF STATEMENT
The  general form simple if  statement is
if (test expression)
{
    statement – block;
}
statement – x;

The statement block may be single statement or a group statements if the test expression is true, the statement block will be executed : otherwise the statement block will be execution will jump to  the statement – x .

Consider the following  segment of a program that is written for processing of marks obtained in an entrance examinationif  (category  ==  SPORTS)
{
       marks  =  marks  +  bonus _ marks;
}
printf (“%f ” , marks);

The program tests the type of category of the student . If the student belongs to the SPORTS category , then additional bonus _ marks are added to his marks before they are printed. For other bonus_  marks are not added.

THE IF……ELSE STATEMENT
The if…..else statement is an extension of this simple if statement The general form isif (test  expression )
    {
        true-block statement (s)
    }
else
    {
        false-block statement(s)
    }
statement -x

If the test expression is true, then the true block statement (s), immediately following the if statement are executed; otherwise , the false – block statements (s) are executed . In either case, either true- block or false- block will be executed, non both

Let us consider an example  of counting the number of boys and girls in class .we use code 1 for a boys and 2 for a girls . The program is this:
if (code == 1)
    boy = boy +1 ;
if ( code == 2)
    girl  = girl +1;
The first test determines whether or not the student is a boy . If yes , the number of boy is increased by 1 and program continues to the  second test . The second test determines whether the student the boys or girls . This is unnecessary . Once a student is identified as boy, there no need to test for again for a girl .The else cause program is below:
if (code == 1)
    boy = boy +1 ;
else
    girl  = girl +1;
Consider the program given in fig 5.3 when the value (c-d) is zero , the ratio is not calculated and program  stop without any massage .In such  cases we may not know  whether the program stopped due to a zero value or some other error.
if (c – d  != 0)
{
      ratio  =  (float ) ( a + b ) / (float ) (c – d );
       printf (“ratio  = %f\n”, ratio);
 }
else
    printf (” c – d ” is zero \n “);

NESTING OF IF…ELSE STATEMENT
When a series of decision involved , we may have to use more then one if….else statement statement in nested form as shown below:
if   (test condition -1 )
    { (test condition -2);
        {
                statement – 1;
         }
else
 {
                statement – 2 ;
            }
        }  
else
        {
            statement – 3 ;
        }
        statement- x;
The logic of execution is illustrated in fig 5.7 if the condition – 1 is false , the statement -3 will be executed; otherwise it continues to perform the second test if the condition -2 is true , the statement-1 will be evaluated. and them the control is transferred  to the statement -x.

A commercial bank  has introduce an incentive policy is as follow: A bonus of 2 percent of balance held on 31st December is given to every one , irrespective their balance, and  5 percent is given to female account holder if their balance is more then 5000  this logic coded as follows:
if (sex as female)
{
    if ( balance > 5000)
        bonus = 0.05 * balance ;
    else 
        bonus =0.02 * balance;
}
else
        bonus = 0.02 * balance;
}
balance  = balance + bonus;

THE ELSE IF LADDER
There is another ways to putting ifs together when multipath  decision are involved . A multipath decision  if chain of if  in which the statement associated with each else is an if is take the following general form:

if (condition 1)
    statement 1;
Else if (condition 3)
    statement 2 ;  
Else if (condition 3)
    statement 3 ;  
Else if (condition 3)
    statement n ;
else
           default statement;
statement x   
This construct known as else if ladder . The condition are evaluated from the top (of the ladder), downwards . As soon as tree condition as found , the statement associated with it executed and control is transferred to the statement – x (skipping the rest of the ladder ). When all the n condition become false , then the final else containing the default statement will  be executed Let us consider an example of grading the student is an academic institution . The grading is done  according to the following rules :

Average marks        Grade
80 to 100                     Honours
60 to  79                     First Division
50 to 59                      Second Division
0   to 39                        Fail
if (marks > 79)
    grade = “Honours”;
else if (marks > 59)
    grade = “First Division”;
else if (marks > 49)
    grade = “Second  Division”;
else if (marks > 39)
    grade = “Third  Division”;
else
     grade =”Fail”;
printf (“%s\n”, grade);

Please click here for related products on Amazon!

Jayashee

Add comment

Want to Check Celebrities News?