Decision making and Branching Statement in C | Decision making and Branching in C with examples

Generally C program execute its statements sequentially. But some situations may arise where we may have to change the order of execution of statements depending on some specific condition. So controlling the execution of statements based on certain condition or decision is called decision making and branching.

For example, when a particular condition exists one set of statements must be executed by the program and if the condition does not exist another set of statements must be executed by the program. In other words, our program must have the capacity to decide which set of statements to execute.

The decision making statements tests a condition and if the condition is true , control will be transferred to the set of statements to be executed, otherwise , control will not be transferred to that set of statements

The statements used for performing such tasks are also called control statements (because they control the flow of execution). The C provides a powerful set of control statements. The set includes if, if-else, if-else if-else, switch statements. Sometimes these statements are called branching statements.

To override the sequential flow of execution, branching is must. Branching must be done based on a test. Given below are the various constructs that achieve this effect.


One way branching:

One way branching means evaluating a condition and then branching. In accordance with the test condition, set of statements are executed.


If statement:

The if statement is one of the most simple decision making statement. It is used to decide whether a particular statement or block of statements will be executed or not. That is, if a certain condition is true then a block of statement is executed otherwise not.


Syntax:

if (test condition)

{

statement-block;

}

The if statement is always used with a condition. The condition is evaluated first before executing any statement inside the body of if.

The statement-block can be a single statement or a group of statements. If the condition evaluates to true then the statements in the block will be executed. Otherwise the control passes to the next statement following the if statement.


Example:

if(marks>=50)

{

printf(“Pass”);

}


Programming Example:


Output:



Two way branching:

Two way branching is used in situations, wherein we need to execute two mutually exclusive sets of actions. This is accomplished by if- else construct of C.


If-else statements:

The if…else statement is an extension of the if statement. It is used to perform two operations for a single condition. When condition is true it performs one action by executing a statement or a set of statements. When condition is false it skips that part of statements and executes other statement or set of statements. That is when two choices of actions are required if..else statement is useful.

 

Syntax:

 if (condition)

{

Statement-block 1;

}

else

 {

Statement-block 2;

}

If the condition evaluates to true, the statement-block1 is executed, If the condition evaluates to false then the statement-block1 is skipped and statement-block2 will be executed.


Example:

if(marks>=50)

{

printf(“Pass”);

}

else

{

printf(“Fail”);

}


Programming example:


Multi way branching:

Sometimes we wish to make a multi-way decision based on several conditions. The multi-way branching can be achieved by if..else-if..else clause. Thereby any number of mutually exclusive statement blocks can be accommodated.


If… else-if …else statements

The if...else-if...else statement is an extension of the if-else statement.  It is useful when we need to check multiple conditions within the program. After the first if branch, the program can have many other else if branches depending upon the condition that need to be tested.


Syntax:

 if (condition)

{

          Statement1;

}

else if (condition)

{

statement2;

}

else if (condition)

{

statement3;

}

else

{

statement4;
          }

 

Example:

if(marks>=80)

{

printf(“Passed: grade A”);

}

else if(marks>=60)

{

printf(“Passed: grade B”);

}

else if(marks>=50)

{

printf(“Passed: grade C”);

}

else

{

printf(“Fail”);

}


Switch...Case Statements:

The switch...case statement is used for efficient multi-way branching. It is an alternate to if-else if-else statement. The switch statement tests the value of the expression or variable against a list of case value and when a match is found, a block of statements associated with that case is executed.


The Syntax is shown below:

switch (expression)

 {      

        case value-1:                                     

                             block-1                

                             break;                  

        case value-2:                  

                             block-2;     

                             break;         

        ……..          …..

        ……..          …..

        case value-n:                  

                             block-n;

                             break;         

        default:                

                             default-block;       

 }                                    

Here, expression must be  integer or character type. Float is not allowed. Value-1, value-2, value-n are integer or character constant. Each of these values should be unique within a switch statement.

When the switch() is executed, the value of the expression is successively compared against the values value-1, value-2 etc. If a case is found whose value matches with the value of the expression, then the statements in that case are executed. When expression does not match with any of the case values then the statement of the default block is executed. The default is optional.

The break statement at the end of each case block indicates the end of a particular case block. If a break is not included in the end of each case block, then all the statements following the case block will also be executed. 


goto statement:

The goto statement is used to alter the normal flow of control in a program. This statement transfers control to any part of the program unconditionally. In certain cases, we may require to transfer control to the other part of the program. C provides goto statement to jump unconditionally from one part to another part of the program. This helps in controlling the flow of execution based on certain specified condition.

The goto statement required a label to identify the place where the control should be jumped to.


Syntax:     goto label;

Label should be valid variable name followed by a colon( : ). The label can be anywhere in the program, either before or after the goto statement. Goto breaks the normal sequential execution of the program. This is basically a control jump, which could be either forward or backward.      

The following example could show the usage of the goto statement:

int main()

{

int a;

read:  //label                                      

scanf(“%d”,&a);

if(a<0)

{

goto read;    //control jump to read    

}

printf(“The value read is %d”,x);

return 0;

}      



  

Post a Comment

0 Comments