DECISION-MAKING OR BRANCHING IN C LANGUAGE
Branching means altering the flow of execution of a program. The C language programs follows a sequential
form of execution of statements. Many times, it is required to alter the flow of sequence of instructions. This
situation is called Branching and handled by branching statements.
Branching statements are classified into two types:
1. Conditional branching
2. Unconditional branching
Conditional Branching:
It includes the statements which works on checking the condition of a given expression to execute the code.
They are:
1. If
2. If-else
3. Nested if
4. Nested if-else
5. Else-if ladder
6. Switch Case
1. if statement
• if statement checks the given condition.
• if test expression is evaluated to true, statements inside the body of if are executed.
• if test expression is evaluated to false, statements inside the body of if are skipped.
General Syntax:
if (test condition)
{
statement-block; //Executes when condition is true
}
Flow Chart:
2. if else statement
• if else statement checks the given condition.
• if test expression is evaluated to true, statements inside the body of if are executed.
• if test expression is evaluated to false, statements inside the body of else are executed.
General Syntax:
if (test condition)
{
Statement-block1; // when test condition is true
}
else
{
Statement-block2; // when test condition is false
}
Flow Chart:
3. nested if statement:
Nested if statement means an if statement inside another if statement. The depth of nested if depends on the
complexity of a problem. We can have any number of nested ifs as required.
General Syntax:
if (test condition1)
if (test condition2) // Executes when condition1 is true
Statement-block; // Executes when condition2 is true
}
Flow Chart:
4. nested if-else statement:
When an if else statement is present inside the body of another “if” or “else” then this is called nested if else.
The depth of nested if depends on the complexity of a problem. We can have any number of nested ifs as
required.
General Syntax:
if (test condition1)
{
if (test condition2) //When condition1 is true
{
Statement-Block1; //When condition2 is true
}
else
{
Statement-Block2; //When condition2 is false
}
}
else
{
Statement-Block3; //When condition1 is false
}
Flow Chart:
Flow Chart for other variations:
i)
ii)
5. If else ladder:
• The if else ladder statement in C programming language is used to test set of conditions in sequence.
• An if condition is tested only when all previous if conditions in if-else ladder is false.
• If any of the conditional expression evaluates to true, then it will execute the corresponding code block
and exits whole if-else ladder.
• The last else is the default block of code which will gets executed if none of the conditional expression is
true. Default else block is optional in if-else ladder statement.
General Syntax:
if(condition_expression_One) {
statement-block1;
} else if (condition_expression_Two) {
statement-block 2;
} else if (condition_expression_Three) {
statement-block 3;
} else {
statement-block 4;
}
Flow Chart:
6. switch case:
• The switch statement is a multiway branch statement.
• The switch statement tests the value of a given variable against the list of case values.
• When a match is found, a block of statements associated with that case is executed.
• If a case match is NOT found, then the default statement is executed, and the control goes out of the
switch block. Default case is optional in switch-case.
• The expression can be integer expression or a character expression. The constant-expression for a case
must be the same data type as the variable in the switch.
• Duplicate case values are not allowed.
‘break’ statement:
1. The ‘break’ is a keyword in C. This statement is used inside loops or switch statement to terminate a
statement sequence. When a break statement is reached, the switch terminates, and the flow of control
jumps to the next line following the switch statement.
2. The break statement is optional. If omitted, execution will continue on into the next case. The flow of
control will fall through to subsequent cases until a break is reached. This condition is called ‘Fall Through’
condition.
‘default’ statement:
It is also a keyword in C. A switch statement can have an optional default case, which must appear at the
end of the switch. The default case can be used for performing a task when none of the cases is true.
No break is needed in the default case.
General Syntax:
switch(expression) {
case constant-expression1: statement(s);
break; /* optional */
case constant-expression2: statement(s);
break; /* optional */
case constant-expression N: statement(s); /* you can have any number of case statements */
break; /* optional */
default: statement(s); /* Default is optional and no Break is needed*/
}
Flow Chart:
Unconditional Branching: goto
These statements don’t check any condition to execute the code. These statements are pretty much useful to
transfer the control from one block to another block over the program. A goto statement in C programming
provides an unconditional jump from the 'goto' to a labeled statement in the same function.
NOTE − Use of goto statement is highly discouraged in any programming language because it makes difficult to
trace the control flow of a program, making the program hard to understand and hard to modify. Any program
that uses a goto can be rewritten to avoid them.
General Syntax:
The syntax for a goto statement in C is as follows −
goto label;
..
.
label: statement;
Here label can be any plain text except C keyword and it can be set anywhere in the C program above
or below to goto statement.
Flow Diagram: