C++ While Repetition Statement



The While Statement

The for loop is a pretest loop that combines the initialization, testing, and updating of a loop control variable in a single loop header. A loop that repeats a specific number of times is known as a count-controlled loop.

For example, if a loop asks the user to enter the sales amounts for each month in the year, it will iterate twelve times. In essence, the loop counts to twelve and asks the user to enter a sales amount each time it makes a count.

A count-controlled loop must possess three elements:

  1. It must initialize a counter variable to a starting value.
  2. It must test the counter variable by comparing it to a final value. When the counter variable reaches its final value, the loop terminates.
  3. It must update the counter variable during each iteration. This is usually done by incrementing the variable.

The while loop has two important parts: an expression that is tested for a true or false value, and a statement or block that is repeated as long as the expression is true.


    while (condition)
    {
    statement;
    statement;
    // Place as many statements
    // here as necessary
    }

Here’s how the loop works. The condition expression is tested, and if it is true, each statement in the body of the loop is executed. Then, the condition is tested again. If it is still true, each statement is executed again. This cycle repeats until the condition is false.

This program demonstrates a simple while loop:


    #include <iostream>
    using namespace std;
    int main()
    {
    int number = 1;
     while (number <= 3)
    {
     cout << "Hi! ";
     number++;
     }
     cout << "\nEnd !\n";
     return 0;
     }

    Output:
    Hi! Hi!  Hi! 
    End !

Each execution of a loop is known as an iteration. This loop will perform three iterations before the expression number <= 3 is tested and found to be false, causing the loop to terminate.

Another example with While Loop

The program uses while loops to validate the user’s input. This program calculates the number of soccer teams a youth league may create from the number of available players. Input validation is done with while loops.


    #include <iostream.h>
    using namespace std;
    int main()
    {
    // Number of available players
    int players,  teamPlayers, numTeams, leftOver; 
    
    // Get the number of players per team
    cout << "How many players do you wish per team?\n";
    cout << "(Enter a value in the range 9 - 15): ";
    cin >> teamPlayers;
    
    // Validate the input
    while ((teamPlayers) < 9) || (teamPlayers > 15))
    {
    cout << "Team size should be 9 to 15 players.\n";
    cout << "How many players do you wish per team? ";
    cin >> teamPlayers;
    }
    
    // Get the number of players available
    cout << "How many players are available? ";
    cin >> players;

    // Validate the input
    while (players <= 0)
    {
    cout << "Please enter a positive number: ";
    cin >> players;
    }
    
    // Calculate the number of teams
    numTeams = players / teamPlayers;
    
    // Calculate the number of leftover players
    leftOver = players % teamPlayers;
    
    // Display the results
    cout << "\nThere will be " << numTeams << " teams with ";
    cout << leftOver << " players left over.\n";
    return 0;
    }

    Output with Example Input Shown in Bold:
    How many players do you wish per team?
    (Enter a value in the range 9 - 15): 4

    Team size should be 9 to 15 players.
    How many players do you wish per team? 12
    How many players are available? –142

    Please enter a positive number: 142
    There will be 11 teams with 10 players left over.

This program calculates the number of soccer teams of a youth league may create, based on a given number of players and a maximum number of players per team.


Ads Right