What is Decision Making & Branching? Explain decision making & branching statements in ‘C’?
![](http://img2.blogblog.com/img/icon18_edit_allbkg.gif)
https://www.computersprofessor.com/2016/06/what-is-decision-making-branching.html
C program is a set of
statements which are normally executed sequentially in the order in which
they appear.
In some situations we
may have to change the order of execution of statements based on certain
conditions, or repeat a group of statements until certain specified
conditions are met.
C language possesses
such decision making capabilities by supporting the following statements.
1. if statement.
2. Switch statement
3. Conditional operator
statement.
4. goto statement
These statements control
the flow of execution of a program. That’s why there are also called as
control statements.
These statement tests
the condition basing on the result make a decision to execute the statements.
If statement:–
The if statement is a
powerful decision making statement & is used to control the flow of
execution of statements.
Ex:– if (test expression)
It allows the computer
to evaluate the exp first and then depending on whether the value of the exp
is true or false, it transfers the control to a particular statement.
The if statement may be
implemented in different forms depending on the complexity of conditions to
be tested. The different forms are:
1. simple if statement
2. If….. else statement.
3. nested if ….else
statement.
4.else if ladder
1. simple if statement :–
The general form of a simple if statement is
if (test expression)
{
Statements block;
}
Statement–X;
Flow chart
The statement may be a
simple statement or a group of statements. If the test exp is true, the
statement block will be executed. Otherwise the statement block will be
skipped & the execution will jump to the statement x when the condition
is true both the statement block & the statement –x are executed in
sequence.
Ex:– if (category = = sports)
{
Marks = marks+bonus;
}
printf (“f”, marks);
2. The if…else statement:
It is an extension of
the simple if. The general form is
if(test expression)
{
true–block statements
}
else
{
False–block statements
}
Statement–x;
Flow Chart
If the test exp. is true
, then the true block statements, immediately following the if statements are
executed otherwise, the false–block statements are executed. In either case,
either true–block or false–block will be executed, not both.
In both cases, the
control is transferred subsequently to the statement.
Ex:– if (cose==1)
Boy=boy+1;
Else
girl =girls+1;
- - - - -
- - - -
3. nesting of if ….. else
statement:–
When a series of
decisions are involved, we may have to use more than one if…… else statement
in nested form as shown below.
Syntax:–
if (test condition1)
{
Statement1
}
else
{
Statement2
}
}
else
{
Statement=3;
}
Statement = x;
If the con1 is false,
the statement 3 will be executed otherwise it continues to perform the 2nd
test. If the con2 is true, the statement1will evaluated & then the
control is transferred to the statement.
Flow Chart
4.the
else if ladder:
There is another way of putting if’s
together when multipath decisions are involved. A multipath decision is a chain
of if’s in which the statement associated with each else is an if. The
general form is :
Syntax: if (con1)
Statement–1;
else
if(con–2)
statement–2
else
if(con–3)
statement–3
else
if(con.n)
statement–n
else
default–statement;
statement
;
This construction is known as the
else if ladder. The conditions are evaluated the top, downward as soon as a
tree con is found, the statement associated with it is executed & the
control is transferred to the
statement–x (skipping the rest of the ladder). When all the n conditions
become false then the final else containing the default statement will be
executed.
Flow chart
Ex:–
If(code==1)
Colour =”red”;
else if (code = =2)
colour=”green”;
else if (code==3)
colour=”while”;
else
colour = “yellow”;
2. The switch statement:–
C has a built–in multiway decision statement
known as a switch. The switch statement tests the value of a given variable
(or exp) against a list of case values & when a match is found , a block
of statements announced with that case is executed. The general form is
Syntax:
switch(expression)
{
case value–1:
block–1;
break;
case value–2:
block–2;
break;
----
---
default: default–block;
break;
}
Statement–x;
The expression is an integer
expression or characters.
value–1, value–2….. are constants
(or) constants expressions and are known as case labels. Each of these values
should be unique with in a switch statement.
block–1, block–2,….. are statement
list & may contain 0 or more statements. There is no need to put braces
around blocks.
Note that case labels end with a
colon(:)
When the switch is executed, the
value of the exp is successfully compared against the values value–1,
value–2,……
If a case is found whose value
matches with the value of the exp, then the block of statements that follow
the case are executed.
The break statement at the end of
each block signals the end of a particular case and causes an exit from the
switch statement transforming
the control to the statement x following the switch.
The default is an optional case.
When present, it will be executed if the value of the exp. does not present
no action takes place if all matches
fail & the control goes to the statement .
Flow Chart
Ex:–switch(op)
{
case 1:
printf(“one”);
break;
case 2:
printf(“two”);
break;
.
.
.
default:
printf(“not entered a digit”);
}
3. The ?: operator:–
Conditional op is useful for making
2–way decisions. This op is a combination of ? and :, & takes 3 operands.
The general form is :
Syntax:– conditional exp?exp–1:exp–2
The conditional exp. is evaluated
first. It the result is nonzero, exp–1 is evaluated & its value is
returned.
Ex:–
if(a>b)
big=a;
else
big=b;
can be written as
big=(a>b)?a:b
4.Goto statement :–
C supports the goto statement to
branch unconditionally from one point to another in the program.
The goto requires a label in order
to identify the place where the branch is to be made. A label is any valid
variable name, & must be followed by a colon.
The label is placed immediately
before the statement where the control is to be transferred. The general
forms of goto & label statements are :
Syntax:
The label cab be anywhere in the
program either before or after the goto label; statement.
If the label: is before the statement
goto label; a loop will be formed & some statements will be executed
repeatedly. Such a jump is known as a backward jump.
On the other hand, if the label: is
placed after the goto label; some statements will be skipped & the jump
is known as forward jump.
|