Search

Admin

5.6 — Do while statements

By Alex, on June 25th, 2007

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:

do
statement;
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 CPU 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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

#include <iostream>

intmain()

{

// selection must be declared outside do/while loop

intselection;

do

{

std::cout<<"Please make a selection: \n";

std::cout<<"1) Addition\n";

std::cout<<"2) Subtraction\n";

std::cout<<"3) Multiplication\n";

std::cout<<"4) Division\n";

std::cin>>selection;

}

while(selection!=1&&selection!=2&&

selection!=3&&selection!=4);

// do something with selection here

// such as a switch statement

std::cout<<"You selected option #"<<selection<<"\n";

return0;

}

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.

Share this:

22 comments to 5.6 — Do while statements

In the above code, when I input an integer other than 1,2,3,4, the menu reappears as expected. However, when I input an ASCII character, such as ‘a’, the program goes into an infinite loop. Why is that?

This is happening because cin >> nSelection is trying to read a number, and ‘a’ isn’t a number. Consequently, the input into nSelection fails, the while loop conditional fails, and the loop repeats.

There are several ways that we could go about fixing this problem.

1) Read the input as a string (which can accept any type of input) and then convert the string into a number (we cover strings in chapter 6.6)
2) Detect that the input call has failed and remove the invalid characters from cin. We cover how to do this in chapter 13, when we delve into C++ I/O.
3) Restrict the user to entering only numeric data. Unfortunately, cin doesn’t have kind of input character filtering functionality, which makes this rather hard to do in C++.

Your example is misleading because until and while actually have OPPOSITE meanings in english language. Consider: If you do something UNTIL X, you stop when X is true. If you do something WHILE X, you continue when X is true.

Although C++ doesn’t have an until keyword, it’s not that hard to write the corresponding while statement. Simply write your statement as if it were an until statement, then replace until with while and flip the conditional using a logical not.

For example, if we wanted to get user input until x < y:

1

2

3

4

5

do

{

cin>>x;

}until(x<y);// C++ doesn't understand until

<!--formatted-->

Convert to while and flip conditional:

1

2

3

4

5

do

{

cin>>x;

}while(!(x<y));

<!--formatted-->

Now, assume y is 5, and the user enters x = 3. x < y is 3 < 5, which is true. !true is false, so this evaluates to while(false). That means the loop exits, which is what you wanted.

Remember to keep your comments outside of the <pre> tags, because it makes reading what you’re asking for difficult. As for your code, if you want to prompt the user to use the calculator again before exiting the program, you can put everything in a while loop. Take the below code as an example.

it’s not a prerequisite to use else if. it’s perfectly valid syntax. the difference is that when you use a variety of ‘if statements’ the program will run through each if statement. when using ‘else if statements’ the program will run through the statements until a statement is true, at that point it will skip all other ‘else if statement’.

It will only print 12 since the next time through the loop; x will be 11 and 11 is not less than 7. you need to use x>7. You should also use
cout << x << endl; or printf ("%d\n", x); You forgot the forward slash.