c++

C++ while and do-while loop

In this article, we are going to learn about while and do-while loops with help of some examples.

In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The while loop can be thought of as a repeating if statement.

C++ while loop

The syntax of the while loop is:

while (condition) { 
// body of the loop 
}

Algorithm Flowchart of while Loop

Let’s look at some examples for more clarity.

Example 1:

The below program prints numbers from 1 to 6.

#include <iostream> 
using namespace std; 
int main() { 
    int i = 1; 
    
    // while loop from 1 to 6 
    while (i <= 6) { 
        cout << i << " "; 
        ++i; 
    } 
    return 0; 
}

Output :

1 2 3 4 5 6

Following is detailed tracing of the above program

Iteration Variable i <= 10 Action
1st i = 1 true 1 is printed. i is increased to 2.
2nd i = 2 true 2 is printed. i is increased to 3.
3rd i = 3 true 3 is printed. i is increased to 4.
4th i = 4 true 4 is printed. i is increased to 5.
5th i = 5 true 5 is printed. i is increased to 6.
6th i = 6 true 6 is printed. i is increased to 7.
7th i = 7 false Loop is terminated.

Example 2:

The following program prints the sum of positive numbers. If user enters negative number the loop terminates and the number is not added to sum, and sum of rest numbers got added and displayed.

#include <iostream> 
using namespace std; 
int main() { 
    int number; 
    int sum = 0; 

    // take input from the user 
    cout << "Enter a number: "; 
    cin >> number; 

    while (number >= 0) { 
        // add all positive numbers
        sum += number; 
        
        // take input again if the number is positive 
        cout << "Enter a number: "; 
        cin >> number; 
    } 
    
    // display the sum 
    cout << "\nThe sum is " << sum << endl; 
    return 0; 
}

Output :

Enter a number: 5 
Enter a number: 1 
Enter a number: 73 
Enter a number: 0 
Enter a number: -2 

The sum is 79


Explanation :-

In this program, the user is prompted to enter a number, which is stored in the variable number.

In order to store the sum of the numbers, we declare a variable and initialize it to the value of 0.

The while loop continues until the user enters a negative number. During each iteration, the number entered by the user is added to the sum variable.

When the user enters a negative number, the loop terminates. Finally, the total sum is displayed.


C++ do-while loop

do-while loop is slightly different from while loop, the only difference between them is that, in do-while loop the condition is examined after the execution of code block present in its body.

Thus do-while are called exit controlled loops. Whereas, for and while loops are called entry controlled loops.

Following is the syntax of do-while loop.

do { 
// body of loop; 
} 
while (condition);

Here, in the above syntax :-

Algorithm Flowchart of do-while Loop

Example 1:

Following program print numbers from 1 to 6 using do-while loop

#include <iostream> 
using namespace std;
int main() { 
    int i = 1; 
    
    // do...while loop from 1 to 6 
    do { 
        cout << i << " "; 
        ++i; 
    } while (i <= 6); 
    return 0; 
}

Output :

1 2 3 4 5 6
Iteration Variable i <= 10 Action
1st i = 1 true 1 is printed. i is increased to 2.
2nd i = 2 true 2 is printed. i is increased to 3.
3rd i = 3 true 3 is printed. i is increased to 4.
4th i = 4 true 4 is printed. i is increased to 5.
5th i = 5 true 5 is printed. i is increased to 6.
6th i = 6 true 6 is printed. i is increased to 7.
7th i = 7 false Loop is terminated.

Example 2:

The following program prints the sum of positive numbers using do-while loop. If user enters negative number the loop terminates and the number is not added to sum, and sum of rest numbers got added and displayed.

#include <iostream> 
using namespace std; 
int main() { 
    int number = 0; 
    int sum = 0; 
    
    do { 
        sum += number; 
        
        // take input from the user 
        cout << "Enter a number: "; 
        cin >> number; 
    } while (number >= 0); 
    
    // display the sum
    cout << "\nThe sum is " << sum << endl;

    return 0; 
}

Output :

Enter a number: 5 
Enter a number: 1 
Enter a number: 73 
Enter a number: 0 
Enter a number: -2 

The sum is 79

Here, the do-while loop continues until the user enters a negative number. When the number is negative, the loop terminates; the negative number is not added to the sum variable.