Decision Making and Branching
Decision Making and Branching
‘C’ language processes decision making capabilities supports the flowing statements
known as control or decision making statements
1. If statement
2. switch statement
3. conditional operator statement
4. Goto statement
If(test expression)
{ statement block;
} statement-x ;
The statement -block may be a single statement or a group of statement if the test
expression is true the statement block will be executed. Otherwise the statement -block will be
skipped and the execution will jump to the statement –X. If the condition is true both the
statement –block sequence .
Flow chart :
Ex : If(category = sports)
{ marks = marks + bonus marks;
} printf(“%d”,marks);
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 an extension of the simple If statement the general
form is
If (test expression)
{
true-block statements;
}
else
{
false-block statements;
}
statement – x;
If the test expression is true then block statement are executed, otherwise the false –
block statement are executed. In both cases either true-block or false-block will be executed
not both.
Flow chart :
Ex : If (code == 1)
boy = boy + 1;
else
girl = girl + 1;
st-x;
Here if the code is equal to ‘1’ the statement boy=boy+1; Is executed and the control
is transfered to the statement st-n, 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 statement st-n.
Nested If –else statement : When a series of decisions are involved we may have to
use more than one if-else statement in nested form of follows .
If(test expression)
{ if(test expression)
{ st –1;
}
else
{ st – 2;
}else
{
st – 3;
}
}st – x;
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
If ( test condition1)
{ if (test condition2)
st –1 ;
} else
if (condition 3)
{ if (condition 4)
st – 2;
}st – x;
Else-If ladder : A multi path decision is charm of its in which the statement associated
with each else is an If. It takes the following general form.
If (condition1)
St –1;
Else If (condition2)
St –2;
Else if (condition 3)
St –3;
Else
Default – st;
St –x;
This construct is known as the wise-If ladder. The conditions are evaluated from the
top of the ladder to down wards. As soon as a true condition is found the statement associated
with it is executed and the control the is transferred to the st-X (i.e.., skipping the rest of the
ladder). when all the n-conditions become false then the final else containing the default – st
will be executed.
Switch Statement : Instead of else –if ladder, ‘C’ has a built-in multi-way decision
statement known as a switch. The general form of the switch statement is as follows.
Switch (expression)
{
case value1 : block1;
break;
case value 2 : block 2;
break;
When the switch is executed the value of the expression is successively compared
against the values value-1,value-2------- If a case is found whose value matches with the of the
expression then the block of statements that follows the case are executed .
The break statement at the end of each block signals the end a particular case and causes
an exist from the switch statement transfering the control to the st-x following the switch. The
default is an optional case . If will be executed if the value of the expression doesn’t match
with any Of the case values then control goes to the St-x.
Ex : switch (number)
{
case 1 : printf(“Monday”);
break;
case 2 : printf(“Tuesday”);
break;
case 3 : printf(“Wednesday”);
break;
case 4 : printf(“Thursday”);
break;
case 5 : printf(“Friday”);
break;
default : printf(“Saturday”);
break;
}
The Conditional ( ? : ) Operator : These operator is a combinations of question and
colon and takes three operands this is also known as conditional operator. The general form of
the conditional operator is as follows
Ex : flag = ( x<0) ? 0 : 1
If ( x<0)
Flag = 0;
Else
Flag = 1;
Goto Statement : The goto statement is used to transfer the control of the program from
one point to another. It is something reffered to as unconditionally branching. The goto is used
in the form
Goto label;
Label statement : The label is a valid ‘C’ identifier followed by a colon. we can precode
any statement by a label in the form
Label : statement ;
This statement immediately transfers execution to the statement labeled with the label
identifier.
Ex : i = 1;
bc : if(1>5) Output : 1
goto ab; 2
printf(“%d”,i); 3
++i; 4
goto bc; 5
ab : {
printf(“%d”,i); }