You are on page 1of 26

# DECISION MAKING, BRANCHING AND

LOOPING
Programming languages for Bioinformatics
BIF507

By Neeru Redhu
CCS HAU, Hisar
C language possess such decision making capabilities
by supporting the following statements

if statement
switch statement
Conditional operator statement
goto statement

2
DECISION MAKING WITH IF STATEMENT
Used to control the flow of Entry
execution of statements

if (test expression)

Test False
Expression
?

True

3
Different forms of if statements

Simple if statement
if..else statement
Nested if..else statement

4
SIMPLE IF STATEMENT
Entry
Single statement block
May contain multiple
statements Test True statement -
Expres block
sion ?
if ( test expression )
{ False
statement - block;
} statement -
x
Statement - x;

Next
statement
5
..
..
if ( category == SPORTS)
{
marks = marks + bonus_marks;
}
printf ( %f , marks);
..
..

6
IF..ELSE STATEMENT
if ( test expression )
{
True statement - block;
}
else
{
False statement block;
}
Statement - x;

7
Entry

## False Test True

False statement True statement
Expres
- block - block
sion ?

statement -
x

8
NESTED IF..ELSE STATEMENT

if ( test expression 1 )
{
if ( test expression 2 )
{
statement block 1;
}
else
{
statement block 2;
}
}
else
{
statement block 3;
} 9
Statement - x;
Entry

False Test
True
Expressi
on 1 ?

## False Test True

Expressi
on 2?

statement 2 statement 1
statement 3

statement
-x
10
Multipath decisions
if ( condition 1 )
statement 1;
else if ( condition 2 )
statement 2;
else if ( condition 3 )
statement 3
else if ( condition 4 )
statement 4;
else
statement 5;
statement x;

11
Entry

ditio
n1

## True Con False

ditio
n2
statement Cond
1 True False
ition
statement 3
2

statement
3
statement
4

12
statement - x
SWITCH STATEMENT
Test the value of given variable (or expression) against
the list of cases values

switch (expression )
{
case value-1:
block -1;
break;
case value-2:
block -2;
break;
..
..
default:
defalut block;
break;
} 13
Statement - x;
.
.
index = marks / 10;
switch (index )
{
case 10:
case 9:
case 8:
break;
case 7:
case 6:
break;
case 5:
break;
default:
break;
} 14
TERNARY OPERATOR ( ? : )

## some = ( x != 40) ? ( (x < 40) ? (4* x + 100) : (4.5 *x +15.) ) : 300

15
GOTO STATEMENT
For branching unconditionally

## Requires label in order to identify the places where

the branch is to be made

## goto label; label:

. .

. .
label: goto label;
statement; statement;
16
LOOPS
..
...............
sum = 0;
n = 1;
loop :
sum = sum + n*n;
if (n == 10)
goto print;
else
{ n +=1;
goto loop;
}
print:
17
LOOPS
C language loop operations can be done with

While statement
Do statement
For statement

18
LOOPING PROCESS
Setting and initialization of conditional variable

## Test for specified value of the condition variable for

execution of the loop

## Incrementing or updating the condition variable

19
WHILE STATEMENT
entry controlled loop

## while (test condition)

{
body of loop
}

20
sum = 0;
n = 1; Initialization

## while ( n >= 10) Testing

{
sum = sum + n*n;
n +=1; Incrementing
}
printf ( sum = %d\n , sum);

21
DO STATEMENT
exit controlled loop

do
{
body of loop
}
while (test condition)

22
sum = 0;
n = 1; Initialization
do
{
sum = sum + n*n;
n +=1; Incrementing
}
Testing
while ( n >= 10)
printf ( sum = %d\n , sum);

23
FOR STATEMENT
entry controlled loop

{
body of loop
}

24
sum = 0;

{
sum = sum + n*n;

25
END

26