Unit1-Conditional Branching

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

10.

Conditional Branching:

Selection / Conditional/ Decision Making and Branching Statement


1. We have seen that a C program is a set of statements that are normally executed
sequentially in the order in which they appear.
2. However, in practice, we have a number of situations where we may have to change the
order of execution of statements based on certain conditions, or repeat a group of
statements until certain specific conditions are met.
3. This involves a kind of decision making to see whether a particular condition has
occurred or not and then direct the computer to execute certain statements accordingly.
4. C language possesses such decision making capabilities by supporting the following
statements:
l if statement
l switch statement
l goto statement
These statements are popularly known as decision making statements.
These statements control the flow of execution they are also known as control statements
If Statement
The if statement is a powerful decision making statement and is used to control the flow of
execution of statements.
It is basically a two-way decision statement and is used in conjunction with an expression
takes the following form
The if statement may be implemented in different forms depending on the complexity of
conditions to be tested.
1. The different forms are
simple if statement
if …. else statement
nested if… else statement
else if ladder.
Simple if statement
The general form of simple if statement is
if (test expression)
{
statement-block;
}
statement-X
If test expression is true , then statement block will be executed; otherwise the statement block
will be skipped and the execution will jump to the statement-X
if…else statement
The if…else statement is an extension of the simple if statement. It takes the following general
form:
if ( test expression)
{
true-block statement(s)
}
else
{
false-block statement(s)
}
statement-x
If the test expression is true, then the true block statements are executed.
If the test expression is false, then the false block statements are executed.
It never executes both blocks at a time.
In both cases the control is transferred
to the statement-X
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.
if (test condition-1)
{
if (test condition-2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
statement-X
1. Once we start nesting if….else statements, we encounter a classical problem known as
the dangling else.
2. This problem is created when there is no matching else for every if.
3. else is always paired with the most recent unpaired if.

The else if Ladder

1. There is another of putting ifs together when multipath decisions are involved .
2. A multipath decision is a chain of ifs in which the statement associated with each else is
an if.
3. It takes general form, as shown below.
if(condition-1)
statement-1;
else if (condition-2)
statement-2;
else if(condition-3)
statement-3
else if(condition n)
statement-n;
else
default statements;
statement-X

Multiway selection statement

1. In addition to two-way selection, most programming languages provide another selection


concept known as multiway selection.
2. Multiway selection chooses among several alternatives.
3. C has two different ways to implement multiway selection:
4. The switch statement and
5. else-if construct.(difficult if no of alternatives are more).
6. The switch statement can be used only when the selection condition reduces to an integral
expression.
7. Many times, when the selection is based on a range of values , the condition is not an
integral. In these cases we use else-if which we already discussed.
switch(expression)
{
case constant-expression:
statement(s);
break; /* optional */

case constant-expression :
statement(s);
break; /* optional */
….....
default : /* Optional */
statement(s);
}

You might also like