Lesson 6.2. While loop in C

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.

Syntax

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:

Flowchart of the while loop in C

With the while loop, the body of the loop may not be executed if the test is false at the begininng.

There is no semicolon at the end of the 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);

Example

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.

Exercise 1

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

Exercise 2

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);

Exercise 3

Write a program with a while' loop that finds and displays the largest divisor of a numberNentered by the user. The program does not consider the 1 andNwhich are always divisors ofN`.

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.

Quiz

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++);
} 
Check Bravo! On the last iteration, x is 7, but it is incremented in the body of the loop (after the display). Try again...

What does the following code display?

int x=2;
while(x>8) {
    printf ("%d ", x++);
} 
Check Bravo! The test is false from the start, the body of the loop does not execute. Try again...

What does the following code display?

int a=0, b;
while(a<4) {
    b=0;
    while (b<2) printf ("%d", b++);
    a++;
} 
Check Bravo! The first loop counts from 0 to 3, so it executes 4 times the second one which counts from 0 to 1. Try again...

What does the following code display?

int a=0, b;
while(a<4) {
    b=0;
    while (b<a) printf ("%d", b++);
    a++;
} 
Check Bravo! The first loop counts from 0 to 3, so it executes 4 times the second loop which counts from 0 to a-1. Try again...

Which codes create an infinite loop?

Check Bravo! To create an infinite loop, the test must always be true. Try again...

What does the following code display?

int x=0;
while (++x<5);
{
    printf ("%d", x);
}
Check Bravo! A classic beginner's mistake: there is a semicolon at the end of the while. The instruction block is empty. Try again...

See also


Last update : 11/22/2022