170 Programming and Data Structures
The execution of the loop can be followed by the following flow chart.
Entry
Steps of while loops are as follows
1) The test condition is evaluated and if it is
true, the body of the loop is executed.
2) On execution of the body, test condition is
repetitively checked and if it is rue the body
is executed.
3) The process of execution of the body will be
continue till the test condition becomes false.
The control is transferred out of the loop.
4) The control is transferred out of the loop
The block of the loop may contain a single statement or a number of statements. The same block can be
repeated.
The braces are needed only if the body of the loop contains more than one statement. However, it is
a good practice to use braces even if the body of the loop contains only one statement.
6.53 Write a program to print the string "You have learnt C program" 9 times using while
loop.
mainQ
{
int x*l;
while (x<10)
{
printf ("tt You have learnt Cprogram");
x++;
I
OUTPUT:
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
You have learnt C program
Explanation The parenthesis after 'w hile' contains a condition. As long as condition remains true
all the statements within the body of the loop gets executed repeatedly. The variable ' x • is initialized