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

75 comments to 5.6 — Do while statements

  • Envy

    I just skimmed the comments and there were only several questions if we couldn't replace the && with || in do-while loop, which is obviously wrong. But what I thought about is why won't we use

    instead of

    If we would add a 5th option to choose from you had to add another "&& selection != 5", whereas in my example you just had to change the 4 into a 5. I know this only works here because we use consecutive numbers but it just bothered me a bit :D

    • Walter

      You're right, this would work, because we use numbers as stand-in for explicit options. Selection 2 is 'Subtraction', but it's also the number 2. It makes sense to say "2 + 1 is 3", but you can't say "Subtraction + Addition is Multiplication".

      We should be using an enum for the options here, but for brevity, this has been excluded. The following doesn't really make sense, for example:

      I believe this is the reason the conditional is written the way it is.

  • SamiraFerdi

    Alex and nascardriver, can you check this for me?

    • * Initialize your variables with uniform initialization
      * Unnecessary forward declarations. Move @main below the other functions
      * @isEvenOrOdd can be done in 1 line. Give it another try
      * You don't need to switch a bool. Use an if-statement
      * @displayEvenOrOddNumberResult can also be done in 1 line

      • SamiraFerdi

        Thank you for reply!

        what about this?

  • I'm in love with alex's tutor

    Good day! Dear Mr Alex/NAS
       Can we change nested while statements to a do..while ? I kindly & respectfully  ask you to show me how to change nested while loops that you previously showed up in section 5.5 as shown below.I have been trying my best but i cant execute what i need.

    God Bless you all!!!!

    • Alex

      • I'm in love with alex's tutor

        My hero Mr Alex!
        I very++ thank you to open my eyes to see the path of programming world. I am addicted to your tutorial really.

  • Hi Alex,

    Borrowed your menu and made a simple calculator with it:

  • 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]