Decision making and Branching statement in C

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:

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:

Nested If Else

In C programming, a nested if-else statement is an if or else statement inside another if or else statement. This allows us to check multiple conditions and create more complex decision-making structures.

A nested if-else statement is when we have an if-else block inside another if or else block. It’s like placing one decision within another decision. By using nested if-else statements, we can create a clear and structured approach to handle multiple conditions in our C programs.

Syntax of Nested If else Statement

if (condition1) {

         if (condition2) {

        // Statements to execute if condition1 and condition2 are both true

        } else {

        // Statements to execute if condition1 is true and condition2 is false

     }

 } else {

    // Statements to execute if condition1 is false

}

Programming Example:

Let us take an example, where the program needs to determine if a number is less than 3, between 3 and 5, or above 5.

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”);

}

Programming Example:

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 allows for multi-way branching based on the value of an expression. This is particularly useful when there are multiple cases to consider, and different code needs to be executed for each case. The switch statement is useful when we have multiple conditions that depend on the value of a single variable.

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 case is optional but recommended to handle unexpected or invalid input values.

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. The break statement is crucial to prevent fall-through to subsequent cases.

Programming Example:

This program will prompt the user to enter a number (1-7) corresponding to a day of the week and will print out the name of that day.

goto statement:

The goto statement in C provides a way to jump to another part of the program. It’s generally advised to avoid using goto because it can make code harder to understand and maintain.

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:     

label:   

//some part of the code;   

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.      

Output:

  

Leave a Comment