A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/cpp-do-while-loop/ below:

C++ do while Loop - GeeksforGeeks

C++ do while Loop

Last Updated : 12 Dec, 2024

In C++, the do-while loop is an exit-controlled loop that repeatedly executes a block of code at least once and continues executing as long as a given condition remains true. Unlike the while loop, the do-while loop guarantees that the loop body will execute at least once, regardless of whether the condition is true or false.

Let’s take a look at an example:

C++
#include <iostream>
using namespace std;

int main() {

    // do-while loop to print "Hi" 5 times
    int i = 0;
    do {
        cout << "Hi" << endl;
        i++;
    } while (i < 5);

    return 0;
}

Explanation: The above do-while loop prints the text "Hi" 5 times. It first executes the body of the loop and then checks the condition. Since the condition i < 5 is true for the first few iterations, the loop continues running. Once i reaches 5, the condition becomes false, and the loop exits.

Syntax of do-while Loop

do {
// Body of the loop
// Update expression
} while (condition);

We have to define the loop variable beforehand and update it manually in the body of the loop. Notice the semicolon (";") in the end of loop. It is compulsory to terminate the do while loop after the semicolon.

The various parts of the do-while loop are: 

  1. Condition: The condition is checked after the loop body executes. If the condition evaluates to true, the loop continues. If false, the loop exits.
  2. Update Expression: Update the loop variable bringing it closer to termination condition.
  3. Body: Body: It is a group of statement that will be executed surely for the first time and then till the condition remains true.
Working of do while Loop in C++

Let's understand the working of the do while loop using the given image:

  1. Control falls into the do-while loop.
  2. The statements inside the body of the loop get executed.
  3. Updation takes place.
  4. The flow jumps to Condition
  5. Condition is tested. 
  6. The flow goes back to Step 2.
  7. The do-while loop has been ended and flow has gone outside the loop.
Flow Diagram of do-while loop Examples of do while Loop in C++

The below examples demonstrate the use of do while loop in different cases and situations:

Print Numbers Less than 0 C++
#include <iostream>
using namespace std;

int main() {

    // do-while loop to print "Hi" 5 times
    int i = 1;
    do {
        cout << i << endl;
        i++;
    } while (i < 0);

    return 0;
}

Explanation: As we can see, even though the condition is false from the start, the body is still executed once.

User Input Validation with do-while Loop

Let’s consider an example where a user is prompted to enter a positive number. The program will continue to prompt the user until they enter a valid number.

C++
#include <iostream>
using namespace std;

int main() {
    int n;

    // Do-while loop to ensure user enters a positive number
    do {
        cout << "Enter a positive number: ";
        cin >> n;
    } while (n <= 0);

    cout << "Entered number: " << n << endl;

    return 0;
}


Output

Enter a positive number: -1
Enter a positive number: -999
Enter a positive number: 2
Entered number: 2

This is one of the primary applications of do while loop in C++.

Print a Square Pattern using Nested Loops

Just like other loops, we can also nest one do while loop into another do while loop.

C++
#include <iostream>
using namespace std;

int main() {
    int i = 0;

    // Outer loop to print each row
    do {
        int j = 0;
      
        // Inner loop to print each character
        // in each row
        do {
            cout << "* ";
            j++;
        } while (j < 4);
        cout << endl;
        i++;
    }while (i < 4);

    return 0;
}

Output
* * * * 
* * * * 
* * * * 
* * * * 
Infinite do while Loop C++
#include <iostream>
using namespace std;

int main() {
  
      // Infinite loop
    do {
        cout << "gfg" << endl;
    }while (true);

    return 0;
}


Output

gfg
gfg
.
.
.
infinite times


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4