DECISION MAKING AND LOPPING
CONTENT
LOOP CONTROL STRUCTURES
THE WHILE STATEMENT
THE DO-WHILE STATEMENT
THE FOR STATEMENT
JUMPING OUT OF A LOOP
SKIPPING A PART OF A LOOP
LOOP CONTROL STRUCTURES
LOOPING: The program may require that a group
of instructions be executed repeatedly, until some
logical condition has been satisfied. This is known
as looping.
entry
Test
false
condition Body of the
? loop
true
Body of the
loop
Test
condition
?
Entry control Exit control
KEY POINTS
In the entry-controlled loop, the control conditions are
tested before the star of the loop execution.
If the conditions are not satisfied, then the body of
the loop will not be executed.
For exit-control loop, the test is performed at the end
of the body of the loop and therefore the body is
executed unconditionally for the first time.
LOOPING SEQUENCE
Setting and initialization of a counter.
Execution of the statements in the loop.
Test for a specified condition for execution of the loop.
Increment the counter.
TYPES
The c program provides three loop constructions
The while statement
the do-while statement
the for statement
THE WHILE STATEMENT
GENERAL FORMAT
while (test condition)
{
body of the loop;
}
KEY POINTS
The while is an entry-controlled loop statement.
First the test condition is evaluated.
If the test condition is true, then body of the loop is
executed.
The body of the loop may have one or more statements.
It is a good practice to use braces even if the body has
one statement.
EXAMPLE
#include<stdio.h>
int main(){
int sum=0,n=1;
while(n>=1)
{
sum=sum+n;
n=n+1;
}
printf(“%d”, sum);
return 0;
}
THE DO-WHILE STATEMENT
GENERAL FORMAT
do
{
body of the loop;
}
while (test condition);
KEY POINTS
Body of the loop is placed before the test condition.
Since the test-condition is evaluated at the bottom of the
loop, the do-while statement provides exit-controlled loop.
The body of the loop is always executed at least once.
The test condition is terminated with a semicolon.
EXAMPLE
#include<stdio.h>
int main(){
int sum=0,n=1;
do
{
sum=sum+n;
n=n+1;
}
while(n>=1);
printf(“%d”,sum);
return 0;
}
THE FOR STATEMENT
The for loop is another entry-controlled loop that
provides a more concise loop control structure.
GENERAL FORMAT
for (initialization; test-condition; increment or decrement)
{
body of the loop;
}
EXECUTION OF FOR STATEMENT
Initialization of the control variables is done first, using
assignment statements such as i=1.
The value of the control variable is tested first using test
condition.
If the test condition is true, body of the loop is executed.
When the body of the loop is executed, the control is
transferred back to the for statement after evaluating the
last statement in the loop.
The control variable is increment or decrement using
assignment statement i=i+1 or i=i-1.
EXAMPLE
#include<stdio.h>
#include<conio.h>
main(){
int sum=0,a,n;
clrscr();
scanf(“%d”,&n);
for (a=1;a<=n;a++){
sum=sum+a;}
printf(“%d”,sum);
getch();}
ADDITIONAL FEATURES OF FOR LOOP
The for loop in C has capabilities that are not found in
other loop construction.
More than one variable can be initialized at a time in the
for statement.
p=1;
for(n=1;n<=17;n++)
Same as
for(p=1,n=1;n<=17;n++)
NESTING OF FOR LOOPS
When one for statement within another for statement,
then it is called nesting of for loops.
GENERAL FORMAT
for (i=1;i<=5;i++)
{
……………… Outer loop
for (j=1;j<=5;j++)
{
………. Inner loop
}
…..
}
……..
KEY POINT
It is useful when constructing pyramid .
Example
for(row=1;row<=5;row++)
{
for (column=1;column<=5;column++)
{
y=row*column;
The outer loop controls row
printf(“%d”,y); while inner loop control column.
}
printf(“\n”);
}
JUMPING OUT OF A LOOP
An early exit from a loop can be performed by using the
break statement or goto statement.
GENERAL FORMAT
break;
KEY POINT
When beak statement is encountered inside
the loop, the loop is immediately exited.
The break will exit only a single loop.
The break statement is end with a semicolon.
EXITING A LOOP WITH BREAK STATEMENT
while (test condition) for ( a=1;a<=5;a++)
{ {
…….. Break;
Exit from if (condition) ……..
loop break; for (____________)
…….. {
} ………
……. ……..
}
Exit from …….
loop
}
Break: Example
Output:
0
1
2
3
SKIPPING A PART OF A LOOP
Under certain condition it is necessary to skip a part of
the body of the loop. This job can be performed by
continue statement.
GENERAL FORMAT
Continue;
KEY POINTS
The continue statement tells the compiler
“skipping the following statements and continue
with the next iteration”.
The continue statement is end with a
semicolon.
EXAMPLE
while (test condition) do
{ {
…….. ……..
if (condition) if (condition)
continue; continue;
…….. ……..
…….. ……..
} }
…….
while (test condition);
Example: Continue Statement
Output:
0
1
2
3
5
6
7
8
9
Practice Problem
Output:
0
Only this will appear! Really??
Practice Problem
Output:
0
1
2
3
4
Hurrah! Only this will appear!
Practice Problem
Output:
Sum = 25
PROGRAMS & OUTPUT
1
X=1
PROGRAMS & OUTPUT
1 0 3 2 7 6 13 12 21
X=21