English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ While and do...while loops

Use loops to repeat specific code blocks in programming. In this article, you will learn how to use loops in C ++Create while and do...while loops in programming.

In computer programming, loops repeatedly execute a specific code block until certain termination conditions are met.

C ++There are many types of loops in programming:3There are many types of loops:

C ++ While loop

The syntax of the while loop is:

while (testExpression) 
{
     // The code to be executed
}

In the while loop, the testExpression is checked at each entry.

How does the while loop work?

  • The while loop evaluates the test expression (testExpression).

  • If the test expression (testExpression) is true, the code within the while loop body will be evaluated.

  • Then, the test expression (testExpression) is evaluated again. This process continues until the test expression (testExpression) is false.

  • The while loop terminates when the test expression (testExpression) is false.

While loop flowchart

Example1: C ++ while loop statement

// C ++Program to calculate the factorial of a number
// The factorial of n = 1 * 2 * 3 ... * n
#include <iostream>
using namespace std;
int main() 
{
    int number, i = 1, factorial = 1;
    cout << "Enter a positive integer: ";
    cin >> number;
    
    while (i <= number) {
        factorial *= i;      //factorial = factorial * i;
        ++i;
    }
    cout << "Calculate " << number << " factorial = " << factorial;
    return 0;
}

Output Result

Enter a positive integer: 4
Calculate 4 factorial = 24

In this program, the user is asked to enter a positive integer stored in the variable number. Assume the user enters4.

Then, the while loop starts executing the code. This is how the while loop works:

  1. Initially, i = 1The test expression i <= number is true, and the factorial becomes1.

  2. The variable i is updated to2, the test expression is true, and the factorial becomes2.

  3. The variable i is updated to3, the test expression is true, and the factorial becomes6.

  4. The variable i is updated to4, the test expression is true, and the factorial becomes24.

  5. The variable i is updated to5, the test expression is false, and the loop terminates.

C ++ do... while loop statement

The do ... while loop is a variant of the while loop, but there is an important difference. The body of the do ... while loop is executed once before checking the test expression (testExpression).

The syntax of the do..while loop is:

do {
   // Execute the code;
}
while (testExpression);

How does the do... while loop work?

  • The code within the loop is executed at least once. Then, the test expression (testExpression) is checked again.

  • If the test expression (testExpression) is true, then the loop body is executed. This process continues until the test expression (testExpression) becomes false.

  • The do ... while loop terminates when the test expression (testExpression) is false.

do ... while loop flowchart

Example2: C ++ do ... while loop

// C++The program adds numbers until the user enters 0
#include <iostream>
using namespace std;
int main() 
{
    float number, sum = 0.0;
    
    do {
        cout << "Enter a number: ";
        cin >> number;
        sum += number;
    }
    while(number != 0.0);
    cout << "Sum = " << sum;
    
    return 0;
}

Output Result

Enter a number: 2
Enter a number: 3
Enter a number: 4
Enter a number: -4
Enter a number: 2
Enter a number: 4.4
Enter a number: 2
Enter a number: 0