Control Statements: Ritu Raj Shreya Chakraborty
Control Statements: Ritu Raj Shreya Chakraborty
STATEMENTS
Ritu Raj
Shreya Chakraborty
control statements
A program consists of a number of
statements which are usually executed
in sequence. Programs can be much
more powerful if we can control the
order in which statements are run.
sequence construct
selection construct
iteration construct
jump statements
Classification of control
statements
sequence construct
Statements are executed sequentially
Statement 1
Statement 2
Statement 3
selection construct
Also known as decision construct
Helps in making decision about which set –
of – statements is to be executed
Execution of statements depends upon
a condition – test
Types of selection statements
if statement
if – else statement
switch
selection construct flow chart
One course of action
Condition True
?
Statement 1 Statement 2
False
Statement 1
Another
course
of
action
Statement 2
iteration construct
Also known as looping construct
a set – of – statements is repeated again
and again till the time a condition is true
Repetition of a set – of – statements
depends upon a condition – test
Types of iteration statements
for loop
while loop
do – while loop
iteration construct flow chart
Condition False
?
The exit condition
True
Statement 1
The loop body
Statement 2
jump statements
Unconditionally transfers program
control within a function.
Control statements that perform
unconditional branch are:
the goto statement used anywhere
the return statement in the program
the break statement used inside
the continue statement smallest enclosing
like loops
Types of Control Statements
The Decision Control Structure
if statement
If - else statement
The Loop Control Structure
while loop
do – while loop
for loop
The Case Control Structure
switch
if statement
if - else statement
conditional operators
Test False
expression
?
True
Body of if
the if statement
Syntax
if (expression)
statement;
Example
int num;
printf(“\n Enter a number less than
10”);
scanf(“%d”,&num);
if (num>=10)
printf(“\n WRONG ENTRY!!”);
nested if statement
It is an if that has another if in its if’s
body or in its else’s body
false
Test
expression Body of else
?
true
Body of if
the if – else statement
Example
Syntax float bs, gs, da, hra;
if(expression) printf(“\n Enter basic
salary”);
statement 1;
scanf(“%d”,&bs);
else if(bs<1500)
statement 2; {
hra=bs*10/100;
da=bs*90/100;
}
else
{
hra=500;
da=bs*98/100;
}
conditional operators
false
Test exit
expression
true
do
test false
exit
expression
true
the do – while loop
Syntax
do
{ Example
statement; do
} {
while (test – expression); printf(“\n Hello there!”);
}
while ( 4<1);
for loop
The for loop is frequently used, usually
where the loop will be traversed a fixed
number of times.
It is very flexible.
for-loop flow chart
Initialization expression (s)
Test false
expression
exit
true
Example
int p,n,count;
float r,si;
for(count=1,count<=3;count=count+1)
{
printf(“\n Enter values of p,n and r”);
scanf(“%d%d%f”,&p,&n,&r);
si=p*n*r/100;
printf(“\n Simple Interest=Rs.%f”,si);
}
switch statement
yes
Case 1 Statement 1
no
yes
Case 2 Statement 2
no
yes
Case 3 Statement 3
no
Exit
the switch statement
Syntax Example
switch(expression) int x;
printf("\n ENTER CHOICE::");
{ scanf("%d",&x);
case constant1:statement1; switch(x)
{
break;
case 1:
case constant2:statement2; printf("\n DIAMOND");
break; break;
case 2:
case constant(n-1):statement (n-1); printf("\n SPADE");
break; break;
case 3:
[default: statement n];
printf("\n HEART");
} break;
default:
printf("\n ERROR WRONG CHOICE!");
}
goto statement
return statement
break statement
continue statement
Jump Statements
the goto statement
transfers the program control anywhere
in the program
the target destination of a goto
statement is marked by a label
the target label and goto must appear in
the same function
the goto statement
Example
Syntax int goals;
goto label: printf(“\n Enter the number of
…….. goals”);
…….. scanf(“%d”,&goals);
label: if(goals<=5)
goto sos:
……..
else
printf(“\n about time soccer players
learnt C”);
sos:
printf(“\n to learn is human!”);
the return statement
BIBLIOGRAPHY
that was all about control statements…..
Thank You.