5.6 — Do while statements

One interesting thing about the while loop is that if the loop condition is initially false, the while loop will not execute at all. It is sometimes the case that we know we want a loop to execute at least once, such as when displaying a menu. To help facilitate this, C++ offers the do-while loop:

while (condition);

The statement in a do-while loop always executes at least once. After the statement has been executed, the do-while loop checks the condition. If the condition is true, the path of execution jumps back to the top of the do-while loop and executes it again.

Here is an example of using a do-while loop to display a menu to the user and wait for the user to make a valid choice:

One interesting thing about the above example is that the selection variable must be declared outside of the do block. Why do you think that is?

If the selection variable were to be declared inside the do block, it would be destroyed when the do block terminates, which happens before the while conditional is executed. But we need the variable to use in the while conditional -- consequently, the selection variable must be declared outside the do block.

Generally it is good form to use a do-while loop instead of a while loop when you intentionally want the loop to execute at least once, as it makes this assumption explicit -- however, it’s not that big of a deal either way.

5.7 -- For statements
5.5 -- While statements

64 comments to 5.6 — Do while statements

  • Star Light

    Your website is a treasure that I believe it's must-known for every C++ learner. I always recommend it for anyone how wanna begin with C++. Really love it.

    Btw, I have an idea. It is accepting a number arguments/parameters when ran program. This one is easy. But I want to make the program auto-restart when it take a wrong parameter. Anyway, let check the code:

    I've read the previous lesson about GOTO statement. But, in the end, those args only fetch when program starts. However, I don't know the return code to make program auto-restart. Or do we have other function for this case? And I would like to avoid: (replace "cls" with my name software)

    Because people said antivirus don't like it and I have to register path to window var environment. And if user change software name then it die :p

    I hope you could gimme some advice for this.

    • nascardriver

      Hi Star Light!

      There are ways to restart you program, but there's no point in doing so programmatically, because you need the user to pass new arguments.
      If you want your program to keep running even if the user entered an erroneous command line you need to manually ask the user for input (@std::cin, @std::getline) and parse it.
      This will require the user to re-enter the entire command line whereas they'd only need to press the up arrow and correct their mistake if you let them restart the application.

      • Star Light

        Yeah, you have a point. But I really want input as an array. std::cin or std::getline could do that, but it isn't as good as taking arguments right from the start.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]