Chapter Eight
Chapter Eight
Chapter Objectives
By the end of this chapter the learner should be able to
Describe and use appropriately the C decision making and Looping control structures
The while statement
The do statement
The for statement
Nesting for statements
Describe and use appropriately syntax to jumping in and jumping out of a Loop.
Break statement
8.1 Introduction
The looping capabilities in C enables us to develop concise programs containing repetitive processes
without the use of GOTO statements. In looping the sequence of statements are executed until some
conditions for termination of the loop is satisfied. A program loop consists of two segments, one known
as the body of the loop and the other as the control statement. The control statement test certain
conditions and then directs the repeated execution of the statement contained in the body of loop.
True
True
(a) Entry controlled loop flow chart (b) Exit controlled loop flow chart
The test-condition should be carefully stated in order to perform the desired number of loop executions.
A looping process in general would include the following four steps;
Setting an initialization of a condition variable.
Execution of the statements in the loop.
Test for a specified value of the condition variable for execution of the loop.
Incrementing or updating the condition variable.
The test may be either to determine whether the loop has been repeated the specified number of times or
to determine whether a particular condition has been met.
The C language provides for three constructs for performing loop operations;
The while statement
The do statement
The for statement
Based on the nature of control variable and the kind of value assigned to it for testing the control
expression, the loop may be classified into two general categories;
Counter-controlled loops: when in advance the number of time the loop is to be executed is known.
Control variable called Counter is used, which is initialized, tested and updated properly for the
desired loop operations. The number of times the loop is to be executed may be a constant or a
variable that is assigned a value. It is also called definite repetition loop.
Sentinel-controlled loops: a special value is used to change the loop control expression from true to
false. The number of repetitions are not known before the loop begins executing and is thus called
indefinite repetition loop.
The while is an entry-controlled loop statement. The test-condition is evaluated and if true the body of the
loop is executed. After the execution the test-condition is evaluated again and if true the body of the loop
is executed again. This process is repeated until the test-condition finally becomes false and the control is
transferred outside the loop where the program continues with the statements immediately after the body
of the loop.
Program example 8.2. Program to compute x to the power of n using while loop
#include<stdio.h>
#include<conio.h>
main()
{
int count, n;
float x, y;
printf(“Enter the values for x and b”);
scanf(“%f %d”, &x, &n);
y =1 ;
count = 1
while (count <= n)
{
y = y*x;
count++;
}
printf(“\nx = %f; n = %d; x to power n = %f\n”, x,n,y);
getch();
}
On reaching the do statement, the program proceeds to evaluate the body of the loop first, and at the end
of the loop the test-condition in the while statement is evaluated. If the test-condition is true the programs
evaluates the body of loop and these process is repeated until the test-condition is false. If the test-
condition is false the loop is terminated and the control goes to the statement that appears immediately
after the while statement.
Since the test-condition is evaluated at the bottom of the loop, the do…..while construct provides an exit–
controlled loop and thus the body of the loop is always executed at least once.
Program example 8.3. Program to print the multiplication table 1x1 to 12x12 using Nested do ..
while loops
#include<stdio.h>
#include<conio.h>
#define COLMAX 10
#define ROWMAX 12
main()
{
int row, column, y;
row = 1;
printf(" MULTIPLICATION TABLE \n");
printf(" ----------------------------------------- \n");
do /* Outer loop begins */
{
column = 1;
do /* inner loop begins */
{
y = row * column;
printf("%4d", y);
column = column + 1;
}
while(column <= COLMAX); /*Inner loop Ends*/
printf("\n");
row = row + 1;
}
while (row <= ROWMAX); /* Outer loop Ends*/
printf (" ---------------------------------------------\n");
getch();
}
Out put
The printf in the inner loop does not contain any new (\n) , thus allowing the printing of all row values in
one line. The empty printf in the outer loop initiates a new line to print the next row.
8.5. The For Statement
The for loop is an entry-controlled loop that provides a more concise loop control structure. The general
form for the for loop is
for (initialization ; test-condition; increment)
{
body of the loop
}
The execution of the for statement is as follows;
1. Initialization of the control variables is done first using assignment statements such as i =1 and count
= 0;. The i and count are called the loop control variables.
2. The value of the control variable is tested using the test-condition. The test-condition is a relational
expression such as i<10 that determines when the loop will exit. If the condition is true, the body of
the loop is executed; otherwise the loop is terminated and the execution continues with the statement
that immediately follows the loop.
3. 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 is incremented using an assignment statement
such as i = i + 1 and the new value of the control variable is again tested to see whether it satisfies the
loop condition. Example
#include<stdio.h>
#include<conio.h>
main()
{
int x;
for (x =0; x<=9; x= x+1)
{
printf("%d", x);
}
printf("\n");
getch();
}
In the above example, the for loop is executed 10 times and prints the digits 0 to 9.
Note the three sections in the for ( ) statement must be separated by semicolons(;). There is no (;) after
the increment section (x = x+1)
getch();
}
*************
*************
Program example 8.5. Program to print the Multiplication table using Nested for loop
#include<stdio.h>
#include<conio.h>
#define COLMAX 10
#define ROWMAX 12
main()
{
int row, column, y;
row = 1;
printf(" MULTIPLICATION TABLE \n");
printf(" ----------------------------------------- \n");
for (row=1; row<=ROWMAX; ++row)
{
for (column = 1; column <= COLMAX; ++column)
{
y = row * column;
printf("%4d", y);
}
printf("\n");
}
printf (" ---------------------------------------------\n");
getch();
}
Exit
******
From *******
loop
}
While ( …….. )
*****
Fig 8.5. Exiting a loop with break statement
The Goto statement can transfer the control to any place on a program, and thus it is useful in proving
branching in a loop. Use goto statement to exit from a deeply nested loops when an error occurs.
The use of structured programming techniques helps ensure well-designed programs that are easier to
write, read, debug and maintain compared to those that are unstructured. Structured programming
discourages the implementation of unconditional branching using jump statements such as goto, break
and continue.