The next statement to be described in this chapter is the while statement. This provides a means for repeated execution of controlled statements. The basic form is
while ( <expression> ) statement and the meaning is that the expression is evaluated and if the value of the expression is non-zero then the controlled statement is executed otherwise the statement after the while statement is executed. An example program is in order.
main()
{
int i = 0;
while(i < 10) /* OK to continue ? */
{
printf("%d %2d %3d\n",i,i*i,i*i*i);
i++;
}
}
The output it produced is 0 0 0 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729
The behaviour should be fairly obvious. This program is a looping or iterative program. When the program starts "i" has the value 0. The first value of the expression
i<10 is 1 since the value of "i" (0) is clearly less than 10. This continues to be the case until the expression
i<10 is evaluated after the execution of the printf() with i having the value 9. At this stage the value of "i" is 10 and the value of the controlling expression is now zero so the controlled printf() is not executed and the path of execution drops through to whatever follows, in this case the end of the program.
Of course the statement controlled by the while can be a compound statement and may include further while statements.
The following example shows nested while loops being used to print out multiplication tables.
main()
{
int i=1,j;
while(i <= 12) /* goes "down" page */
{
j = 1;
while(j <= 12) /* goes "across" page */
{
printf(" %3d",i*j);
j++;
}
printf("\n"); /* end of line */
i++;
}
}
as follows 1 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 14 16 18 20 22 24 3 6 9 12 15 18 21 24 27 30 33 36 4 8 12 16 20 24 28 32 36 40 44 48 5 10 15 20 25 30 35 40 45 50 55 60 6 12 18 24 30 36 42 48 54 60 66 72 7 14 21 28 35 42 49 56 63 70 77 84 8 16 24 32 40 48 56 64 72 80 88 96 9 18 27 36 45 54 63 72 81 90 99 108 10 20 30 40 50 60 70 80 90 100 110 120 11 22 33 44 55 66 77 88 99 110 121 132 12 24 36 48 60 72 84 96 108 120 132 144
Input operations can also be included within a loop. An interactive program using a while statement and illustrating this is shown below.
main()
{
double x = 1.0; /* non-zero to avoid immediate exit */
printf("Program to display reciprocals, "
"squares and cubes\nEnter 0 to"
" terminate\n");
while(!(x == 0)) /* i.e. while valid data */
{
printf("Enter a number ");
scanf("%lf",&x);
if(!(x==0)) /* valid data ?? */
{
printf("Reciprocal = %10.5lf\n",1.0/x);
printf("Square = %10.5lf\n",x*x);
printf("Cube = %10.5lf\n",x*x*x);
}
}
}
A typical dialogue is shown below Program to display reciprocals, squares and cubes Enter 0 to terminate Enter a number 4 Reciprocal = 0.25000 Square = 16.00000 Cube = 64.00000 Enter a number 0.25 Reciprocal = 4.00000 Square = 0.06250 Cube = 0.01562 Enter a number -3 Reciprocal = -0.33333 Square = 9.00000 Cube = -27.00000 Enter a number 0
Occasionally there is, apparently, no need for the controlled statement associated with a while , everything you want to do can be done via side-effects of the evaluation of the expression. Under these circumstances there must still be a statement after the expression but it will be a null statement that is represented simply by a semi-colon. An example is shown below.
main()
{
int i=2;
while(printf("%d\n",i++)!=3);
}
This produced the output 2 3 4 5 6 7 8 9 10
To understand what happened you need to know the little known fact that the value of the printf() function is the number of characters actually printed which may include a new-line. It is also worth noting that the production of output by the function printf() is a side-effect . Another simple example of a while statement whose controlled statement is a null statement is a simple spin delay.
int delay = 10000; while(delay--);
Another common use of the while statement is the forever loop which might look like
while(1)
{
.
.
.
}
The controlled statement is executed until the loop is broken by some means. The simplest and commonest way of doing this is by using a break statement. A break statement consists simply of the word break followed by a semi-colon. Its effect is to cause immediate exit from the enclosing while statement. (It has the same effect when used with do and for statements, these will be described in due course.) It provides a means for writing a simpler and tidier version of the interactive program that appeared earlier in these notes. The revised version is.
main()
{
double x;
printf("Program to display reciprocals, "
"squares and cubes\nEnter 0 to"
" terminate\n");
while(1)
{
printf("Enter a number ");
scanf("%lf",&x);
if(x == 0) break; /* end of valid data ? */
printf("Reciprocal = %10.5lf\n",1.0/x);
printf("Square = %10.5lf\n",x*x);
printf("Cube = %10.5lf\n",x*x*x);
}
}
The condition "x==0.0" might be called the termination condition as far the while controlled statement is concerned. The use of break avoids the need for testing for the condition twice with one of the tests being logically inverted and used in conjunction with an if statement to prevent the rest of controlled statement being executed when the termination condition is detected.
Closely associated with the break statement is the continue statement which like the break causes the rest of the loop to be skipped but unlike the break does not exit the loop. Its use is illustrated in the following example which sums all the numbers up to and excluding a user entered value excluding the numbers that are divisible by 4.
main()
{
int max;
int sum=0,count=0;
int i=0;
printf("Enter largest number ");
scanf("%d",&max);
while(1)
{
i++;
if(i == max) break;
if(i%4 == 0) continue;
count++;
sum += i;
}
printf("There are %d numbers not divisible by 4"
" and less than %d\nTheir total is %d\n",
count,max,sum);
}
This produced the output bash$ while5 Enter largest number 15 There are 11 numbers not divisible by 4 and less than 15 Their total is 81 bash$ while5 Enter largest number 16 There are 12 numbers not divisible by 4 and less than 16 Their total is 96