The while statement


The second loop available in ̳C‘ is while loop.
The general format of while loop is:
while (expression)
statement

A while statement is useful to repeat a statement execution as long as a condition
remains true or an error is detected. The while statement tests the condition before
executing the statement.

The condition, can be any valid C languages expression including the value of a
variable, a unary or binary expression, an arithmetic expression, or the return value
from a function call.

The statement can be a simple or compound statement. A compound statement in a
while statement appears as:
while (condition)
{
statement1;
statement2;
}

With the if statement, it is important that no semicolon follow the closing parenthesis,
otherwise the compiler will assume the loop body consists of a single null statement.
This usually results in an infinite loop because the value of the condition will not change
with in the body of the loop.
Example:
main()
{
int j = 1;
double degC, degF;
clrscr ();
printf (―\n Table of Celsius and Fahrenheit degrees \n\n‖);
printf (―Celsius Degree \t Fahrenheit Degree \n―)
degC = -20.0;
while (j <= 6)
{
degC = degC + 20.0;
degF = (degC * 9.0/5.0) + 32.0;
printf (―\n %7.2lf\t\ %7.2lf ―, degC,
degF); j++;
}
}

Output:
Table of Celsius and Fahrenheit degrees
Celsius Degree Fahrenheit Degree
0.00 32.00
20.00 68.00
40.00 104.00
60.00 140.00
80.00 176.00
100.00 212.00

Because the while loop can accept expressions, not just conditions, the following are all
legal:
while(x--);
while(x = x+1);
while(x += 5);
Using this type of expression, only when the result of x--, x=x+1, or x+=5, evaluates
to 0 will the while condition fail and the loop be exited.

We can go further still and perform complete operations within the while expression:
while(i++ < 10);
The counts i up to 10.
while((ch = getchar())
!=`q') putchar(ch);

This uses C standard library functions: getchar () to reads a character from the
keyboard and putchar () to writes a given char to screen. The while loop will proceed to
read from the keyboard and echo characters to the screen until a 'q' character is read.


Nested while:


Example:
Program to show table of first four powers of numbers 1 to 9.
#include <stdio.h >
void main()

{
int i, j, k, temp;
printf("I\tI^2\tI^3\tI^4 \n");
printf("--------------------------------\n");
i = 1;
while (i < 10) /* Outer loop */
{
j = 1;

while (j < 5) /* 1st level of nesting */

{
temp = 1;
k = 1;
while (k < j)
{
temp = temp *
i; k++;
}
printf ("%d\t",
temp); j++;
}
printf
("\n"); i++;
}
}
Output to the screen:
I I ^

2 I ^ 3 I ^ 4
-----------------------------------
1 1 1 1
2 4 8 16
3 9 27 81
4 16 64 256
5 25 125 625
6 36 216 1296
7 49 343 2401
8 64 512 4096
9 81 729 6561


The do- while statement:


The third loop available in C is do – while loop.
The general format of do-while is:
do
statement;
while (expression);

Unlike for and while loops, which tests the condition at the top of the loop. The do –
while loop checks its condition at the bot tom of the loop. This means that the do –
while loop always executes first and then the condition is tested. Unlike the while
construction, the do – while requires a semicolon to follow the statement‘s conditional
part.

If more than one statement is to be executed in the body of the loop, then these
statements may be formed into a compound statement as follows:
do
{
statement1;
statement2;
} while (condition);

Example 1:
# include
<stdio.h> main()
{
do
{
printf("x = %d\n", x--
); } while(x > 0);
}
Output to the screen:

X = 3
X = 2
X = 1
Example 2:
#include <stdio.h>
void main()
{
char ch;

printf("T: Train\n");
printf("C: Car\n");
printf("S: Ship\n");
do
{
printf("\nEnter your choice:
"); fflush(stdin);
ch = getchar();
switch(ch)
{
case 'T' :

printf("\nTrain");
break;

case 'C' :

printf("\nCar");
break;

case 'S':

printf("\nShip");
break;

default:

printf("\n Invalid Choice");

}
} while(ch == 'T' || ch == 'C' || ch == 'S');
}
Output to the screen:
T: Train
C: Car
S: Ship
Enter your choice: T
Train
Distinguishing between while and do-while loops:

While loop And Do-while loop
The while loop tests the condition before each iteration. The do-while loop tests the condition after the first iteration.
If the condition fails initially the loop(while) is skipped entirely even in the first iteration.  Even if the condition fails initially the loop(Do-while) is executed once.

Custom Search

%d bloggers like this: