Chapter 5 - Looping
Chapter 5 - Looping
LOOPS
5.1
To know the function of break and continue statement in loop control structure
INTRODUCTION
A loop is a group of instructions the computer executes repeatedly while some loop-continuation
condition remains true. Also known as repetition structure, it repeatedly processes (a) program
instruction(s) until a condition is met. C has three types repetition, i.e. while, do-while, for and nested
for.In while structure it allows for an action that has to be repeated while some condition remains true.
The dowhile repetition statement is similar to the while statement. In the while statement, the loopcontinuation condition is tested at the beginning of the loop before the body of the loop is performed. But
the dowhile statement tests the loop-continuation condition after the loop body is performed. In while,
there are two means of repetition i.e. Counter-controlled repetition and Sentinel-controlled repetition.
Counter-controlled repetition is sometimes called definite repetition because we know in advance exactly
how many times the loop will be executed. Sentinel-controlled repetition is sometimes called indefinite
repetition because its not known in advance how many times the loop will be executed.
The for repetition statement handles all the details of counter-controlled repetition. Nested for
loops consist of an outer loop with one or more inner loops. Each time the outer loop is repeated, the
inner loops are reentered, their loop control expressions are reevaluated and all required iteration are
performed. There are two types of statement related to loop, i.e. break and continue. break causes a loop
to terminate only the inner loop in nested loop. continue skips the remaining statements in the body of a
control statement and performs the next iteration of the loop (for while, for or dowhile statement).
5.2
GENERAL INFORMATION
5.2.1 Syntax
1) while
while(expression)
statement
E.g.
i = 0;
while (i<5)
{
printf(%d,i);
i = i++;
}
2) do while
do
statement
while(expression);
E.g.
i = 0;
do
{
printf(%d,i);
i = i++;
} while (i<5);
3) for
for ( expression1; expression2; expression3 )
statement
E.g.(1)
int i = 0;
for(i=0;i<5;i++)
{
printf(%d,i);
}
E.g.(2)
int i,j;
for(i=0,j=0;i<10&&j>0;i++,j0){
printf(i=%d\n,i);
printf(j=%d\n,j);
4) nested for
for ( expression1; expression2; expression3 )
statement
for ( expression1; expression2; expression3 )
statement
E.g.
int i,j;
for (i=1;i<=3;i++)
{
printf("Row %d :",i);
for (j=1;j<= 5;j++)
printf("%3d",j);
printf("\n");
}
5.3
EXERCISES
special
input
value
3. The syntax for for loop is: for (expression1; expression2; expression3).
Explain expression1, expression2, and expression3.
are
6. Detect the error of below program? Correct the code so it displays all multiples of 4 from 0 to 100.
for mult4=0;
mult4<100;
mult4+=4;
printf(%d\n,mult4);
7. What is the output of below program:
j=10;
for(i=1;i<=5;++i){
printf(%d
%d\n,i,j);
j-=2;
}
8. How many lines the output of below program will display
for(i = 0; i < 5; i = i + 1)
for(j = 0; j < i; j = j + 1)
printf("%d %d\n", i, j);
9. What is displayed by the following program segments, assume m is 3, n is 5:
a. for (i=1,i<=n;++i){
for(j=0;j<1;++j){
printf((*);
}
printf(\n);
}
b. for (i=n,i>0;--i){
for(j=m;j>0;--j){
printf((*);
}
printf(\n);
}
10. What is the output of below program:
i=0;
while(i<=5) {
printf(%d %2d\n,i,10-i);
i=i+1;
}
14. Write a program that computes and displays the sum of collection of celcius temperature entered
at the terminal until a sentinel value of -100 is entered. Write pseudocode and draw the flowchart
of your program first.
15. Write a program using a loop to printout the following sequence including commas:
*
**
***
****
*****
******
Reference:
1. Deitel, Paul and Deitel, Harvey. 2010. C How To Program, Sixth Edition. Perason, 2010.
2. Hanly, Jeri R. and Koffman, Elliot B. 2010. Problem Solving and Program Design in C, Sixth
Edition. Pearson, 2010.
3. Cheng, H. Harry. 2010. C for engineers and scientists: An Interpretive Approach. McGrawHill, 2010.