Unit 3 Decision Making and Branching
Unit 3 Decision Making and Branching
Normally the statements in a program are execute in the order in which they appear in the program.
This type of execution is called a sequential execution.
In some situations, the order of execution of instructions may have to be changed depending on
certain conditions. This involves a kind of decision making (i.e., logical test to be conducted) to see
whether a particular condition has occurred or not and then direct the computer to execute certain
instructions accordingly. This is called conditional execution. The result of the conditional execution
is a Boolean value (i.e., either true or false). Conditional execution involves both decision making and
branching.
1. if statement
2. if-else statement
3. nested-if statement
4. if-else ladder
5. switch statement
if statement
This is used to execute statement(s) conditionally. It is a simple if statement and is also known as one-
way branching. Here the logical condition is tested to either true or false. The syntax is
if(boolean_expression) {
If the Boolean expression evaluates to true, then the block of code inside the 'if' statement will be
executed. If the Boolean expression evaluates to false, then the first set of code after the end of the
'if' statement (after the closing curly brace) will be executed.
Flow chart:
Example :
if(category == “sports”)
{
marks = marks + bonus marks;
}
printf(“%d”,marks);
Explanation: 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.
If-else statement
The if statement is used to execute only on action. If there are two statements to be executed
alternatively, then if-else statement is used. The if-else statement is a two way branching. The syntax
of if-else statement is:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false */
}
If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the else
block will be executed.
C programming language assumes any non-zero and non-null values as true, and if it is either zero or
null, then it is assumed as false value.
Flow chart:
Example :
if (code == 1)
{
boy = boy + 1;
}
else
{
girl = girl + 1;
}
printf (“no of boys is %d and girls is %d”, boy, girl);
Here if the code is equal to ‘1’ the statement boy=boy+1; is executed and the control is transferred
to the printf statement, after skipping the else part. If code is not equal to ‘1’ the statement boy
=boy+1; is skipped and the statement in the else part girl =girl+1; is executed before the
control reaches the printf statement.
Nested If statement
When a series of decisions are involved we may have to use more than one if-else statement in nested
form of follows. Nesting means you can use one if or else if statement inside another if or else if
statement(s). The syntax for a nested if statement is
if( boolean_expression 1)
{
/* Executes when the boolean expression 1 is true */
if(boolean_expression 2)
{
/* Executes when the boolean expression 2 is true */
}
else
{
/* Executes when the boolean expression 2 is false */
}
}
else
{
/* Executes when the boolean expression 1 is false */
}
St-3
If the condition is false the st-3 will be executed otherwise it continues to perform the nested If –else
structure (inner part ). If the condition 2 is true the st-1 will be executed otherwise the st-2 will be
evaluated and then the control is transferred to the st-x
Example:
/* local variable definition */
int a = 100;
int b = 200;
if else ladder
The else if ladder is an another way of putting ifs together when multipath decisions are involved. A
multipath decision is a chain of ifs in which the statement associated with each else is an if.
The if else ladder statement in C programming language is used to test set of conditions in sequence.
If any of the conditional expression evaluates to true, then it will execute the corresponding code
block and exits whole if-else ladder.
Syntax:
if(condition_expression_One) {
statement1;
} else if (condition_expression_Two) {
statement2;
} else if (condition_expression_Three) {
statement3;
} else {
statement4;
}
#include<stdio.h>
void main ()
{
int num = 10 ;
if ( num > 0 )
printf ("\n Number is Positive");
else if ( num < 0 )
printf ("\n Number is Negative");
else
printf ("\n Number is Zero");
}
switch Statement
The if..else..if ladder allows you to execute a block code among many alternatives. If you are
checking on the value of a single variable in if...else...if, it is better to use switch statement.
The switch statement is a multiway branching statement that tests the value of a given variable against
a list of case values and when a match is found, a block of statements associated with that case is
executed. The general form of switch statement is:
Syntax:
switch(expression)
{
case value1 :
statements;
break;
case value2 :
statements;
break;
:
:
case valuen :
statements;
break;
default :
statements;
break;
}
Flow Diagram
Program:
#include <stdio.h>
int main () {
char grade = 'B';
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
printf("Better try again\n" );
break;
default :
printf("Invalid grade\n" );
}
return 0;
goto statement
C supports an unconditional control statement, goto that provides an unconditional jump from the
'goto' to a labeled statement in a C program. The syntax of goto statement is as follows:
goto label;
………
label:
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. The label can be placed any where in the C program either before or after
the goto statement.
NOTE − Use of goto statement is highly discouraged in any programming language because it may
create an infinite loop.
Example: The following code checks whether an entered number is even or odd
if (num % 2 == 0)
goto even; // jump to even
else
goto odd; // jump to odd
even:
printf(“%d is an even number”,num); return 0;
odd:
printf(“%d is an odd number”,num); return 0;