The while
loop is similar to the do..while loop except that the condition
is tested first (before the block of instructions). This implies - unlike the do..while -
that the instruction block will not necessarily be executed at least once. In other words,
it is possible to jump the loop if the condition is false from the start.
The while
loop repeats a sequence of statements as long as the condition is true,
but the condition is evaluated first :
while (test) {
// ...
Instructions block
// ...
}
Here is the flowchart for the while
loop:
With the while
loop, the body of the loop may not be executed if the test is false at the begininng.
while
, unless the instruction block is empty: while(1);
.Chacun des paramètres est optionnel, on peut écrire for(;;);, ce qui est équivalent à while(1);
Here is an example of a while
loop that asks the user to enter a
negative number:
int n;
// First request of a number
printf ("Enter a number: ");
scanf ("%d", &n);
// If the number is positive, ask for the number again
while (n>=0) {
printf ("Enter a negative number: ");
scanf ("%d", &n);
}
// Display the valid number
printf ("You entered %d.\n", n);
If on the first request, the user enters a negative number, we do not enter the loop and the execution goes directly to the display.
Write a program that asks the user to enter a percentage. If the percentage is not between 0 and 100, the program asks for the percentage again but then specifies that it must be between 0 and 100 according to the following example:
Enter a percentage: -3.2
Enter a percentage between 0 and 100: 125.6
Enter a percentage between 0 and 100: 25.2
The discount is 25.20%.
float dicount;
// Request a percentage
// COMPLETE HERE
// Check if the percentage is between 0 and 100
// COMPLETE HERE
// Display the percentage
// COMPLETE HERE
Write a program with a while
loop that computes the following sequence:
$$ U_n = \dfrac{1}{U_{n-1}+1} $$
with \( U_0 = 1 \).
The user enters the index of the element he wants to calculate and the program displays this element:
Enter the index n: 0
U(0) = 1.000000
Enter the index n: 3
U(3) = 0.600000
Enter the index n: 100
U(100) = 0.618034
Here is the code to complete:
int i=0, n;
float U=1;
// Ask for the index
printf ("Enter the index n: ");
scanf ("%d", &n);
// Compute element Un
// COMPLETE HERE
// Display the result
printf ("U(%d) = %.6f\n", n, U);
Write a program with a while' loop that finds and displays the largest divisor of a number
Nentered by the user. The program does not consider the 1 and
Nwhich are always divisors of
N`.
If the number is 0 or 1, the program displays:
Enter a strictly positive integer n: 0
0 has no divisor.
Enter a strictly positive integer n: 1
1 has no divisor.
Si le programme trouve un diviseur, il affiche ce diviseur :
Enter a strictly positive integer n: 15
5 is the greatest divisor of 15.
If N
is prime (it has no divisor other than itself and 1), the program displays:
Enter a strictly positive integer n: 11
11 is prime.
For the analysis of the programs below, it is best to have a paper and pencil.
What does the following code display?
int x=2;
while(x<8) {
printf ("%d ", x++);
}
What does the following code display?
int x=2;
while(x>8) {
printf ("%d ", x++);
}
What does the following code display?
int a=0, b;
while(a<4) {
b=0;
while (b<2) printf ("%d", b++);
a++;
}
What does the following code display?
int a=0, b;
while(a<4) {
b=0;
while (b<a) printf ("%d", b++);
a++;
}
0
to a-1
.
Try again...
Which codes create an infinite loop?
What does the following code display?
int x=0;
while (++x<5);
{
printf ("%d", x);
}
while
. The instruction block is empty.
Try again...