The do while loop In addition to the while loop C also offers the do while loop

The do while loop in addition to the while loop c

This preview shows page 16 - 20 out of 28 pages.

The do-while loop In addition to the while loop, C++ also offers the do-while loop. Each loop is appropriate for different programming problems. The do-while loop looks similar to a while loop turned upside-down. Here is its format do { // Place as many statements here // as necessary. } while (expression);
Image of page 16
The do-while loop must be terminated with a semicolon after the closing parenthesis of the test- expression. Besides the way it looks, the difference between the do-while loop and the while loop is that do-while is a post-test loop. It tests its expression after each iteration is complete. This means do-while always performs at least one iteration, even if the test-expression is false from the start. For example, in the following while loop the cout statement will not execute at all: int x = ; while (x < ) cout << x << endl; But the cout statement in the following do-while loop will execute once because the do-while loop does not evaluate the expression x < until the end of the iteration. int x = ; do cout << x << endl; while (x < ); You should use do-while when you want make sure the loop executes at least once. The do- while loop is a good choice for repeating a menu. For example, the following example program echoes any text the user introduces until the user enters goodbye: // echo machine #include <iostream> #include <string> using namespace std; int main () { string str; Enter text: hello You entered: hello Enter text: who's there? You entered: who's there? Enter text: goodbye You entered: goodbye
Image of page 17
do { cout << "Enter text: "; getline (cin,str); cout << "You entered: " << str << '\n'; } while (str != "goodbye"); } The do-while loop is usually preferred over a while-loop when the statement needs to be executed at least once, such as when the condition that is checked to end of the loop is determined within the loop statement itself. In the previous example, the user input within the block is what will determine if the loop ends. And thus, even if the user wants to end the loop as soon as possible by entering goodbye, the block in the loop needs to be executed at least once to prompt for input, and the condition can, in fact, only be determined after it is executed. The for loop The third type of loop in C++ is the for loop. It is ideal for situations that require a counter because it has built-in expressions that initialize and update variables. Here is the format for loop: for (initialization; test; update) { // Place as many statements here // as necessary. } There are three expressions inside the parentheses, separated by semicolons . (Notice there is not a semicolon after the third expression.) Drill: Write a program segment with a do-while loop that asks the user to enter a number. The loop should keep a running total of the numbers entered, and stop when the total is greater than .
Image of page 18
The first expression is the initialization expression . It is typically used to initialize a counter or other variable that must have a starting value. This is the first action performed by the loop and it is only done once at the beginning of the loop The second expression is the test expression . Like the test expression in the while and do-
Image of page 19
Image of page 20

You've reached the end of your free preview.

Want to read all 28 pages?

  • Spring '16
  • mohamed attia
  • Control flow, loop

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes