Vous êtes sur la page 1sur 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
else if ladder

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
Series of decision to made

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
ELSE IF LADDER
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

True Con False


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:
grade = Honours ;
break;
case 7:
case 6:
grade = First Division ;
break;
case 5:
grade = Second Division ;
break;
default:
grade = Fail ;
break;
} 14
printf ( %s\n, grade);
TERNARY OPERATOR ( ? : )

conditional expression ? exp1 : exp2

y = ( x > 2) ? ( 2 * x + 5) : (1.5 * x + 3);

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

Execution of statement of loop

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

for (initialization ; test condition ; increment)


{
body of loop
}

24
sum = 0;

for (n = 1; n >= 10 ; n +=1)


{
sum = sum + n*n;

25
END

26