You are on page 1of 47

CONTROL

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

 Represents the default flow of statement


sequence construct flow chart

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

The Decision Control


Structure
if statement
 This is used to decide whether to do
something at a special point, or to
decide between two courses of action
 It tests a particular condition
if statement flow chart

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

 The nested if’s must terminate before an


outer if
nested if statement
Example
Syntax int i,j;
if (expression 1) printf(“\n Enter two numbers”);
scanf(“%d%d”,&i,&j);
{
if(i==1)
if (expression 2) {
statement 1; if(j==0)
[ else printf(“\n you would go to
heaven”);
statement 2;] else
} printf(“\n hell was created with you in
else mind”);
}
body – of – else else
printf(“”\n how about mother
earth?!);
if - else statement
 Sometimes we wish to make a multi-way
decision based on several conditions.
The most general way of doing this is by
using the else if variant on the if
statement.
 It tests an expression and depending
upon its truth value one of the two sets-
of action is executed
if-else statement flow chart

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

 The conditional operators are ? and :


 These are sometimes called ternary
operators
 They form a foreshortened if-then-else
 These offer more concise, clean and
compact code.
conditional operators
Syntax
(expression 1? expression 2: expression 3);

Example Its equivalent if


statement would be:
int x,y;
scanf(“%d”,&x); if(x>5)
y=3;
y=(x>5?3:4); else
y=4;
while loop
do – while loop
for loop

The Loop Control Structure


while loop
 The while loop keeps repeating an
action until an associated test returns
false.
 This is useful where the programmer
does not know in advance how many
times the loop will be traversed.
while-loop flow chart

false
Test exit
expression

true

Body of the loop


the while loop
Example
int p,n,count;
float r,si;
Syntax count=1;
while (expression)
while(count<=3)
loop body; {
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);
count=count+1;
}
do – while loop
 The do while loops is similar, but the test
occurs after the loop body is executed.
 This ensures that the loop body is run
at least once.
do-while loop flow chart

do

Body of the loop

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

Body of the loop

Update expression (s)


the for loop
Syntax
for(initialization;test;update)
body – of – the – loop;

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

The Case Control Structure


switch
 This is another form of the multi way
decision
 It is well structured, but can only be
used in certain cases where
Only one variable is tested
The variable must be an integral type. (int,
long, short or char).
switch statement flow chart
Start

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

 used to return value (s) from a function


to the main program

 used in functions with a return type


the return statement
Syntax
test expression;
………… Example
………. fun()
return 0; {
or char ch;
printf(“\n Enter any alphabet”) ;
return (variable);
scanf(“%c”,&ch);
if(ch>=65 && ch<=90)
return (ch);
else
return (ch+32);
}
the break statement
 It is used to exit from a loop or a switch,
control passing to the first statement
beyond the loop or a switch.
 It enables a program to skip over part of
the code
 It terminates the smallest enclosing
while, do-while, for or switch statements
the break statement
Example
Syntax while (i<=num-1)
test expression {
{ if(num%i==0)
statement 1; {
condition printf(“\n not a prime
number”);
break;
break;
…….
}
statement 2;
i++;
}
}
statement 3; if(i==num)
printf(“\n prime number”) ;
the continue statement

 It only works within loops where its


effect is to force an immediate jump to
the loop control statement.
 It abandons the current iteration of the of
the loop by skipping over the loop body
 It immediately transfers control to the
evaluation of the test-expression
the continue statement
Syntax
for (initialization;expression;update) Example
{ for( i=1;i<=2;i++)
statement 1; {
condition for(j=1;j<=2;j++)
continue; {
statement 2; if( i==j)
} continue;
Statement 3; printf(“\n %d%d”,i,j);
}
}
• Let Us C Yashavant P. Kanetkar
•Programing In ANSI C E. Balaguruswamy
• Computer Science with C++ Sumita Arora
•Computer Fundamentals & Programming in C S. K. Jha

BIBLIOGRAPHY
that was all about control statements…..

Thank You.

You might also like