The do-while loop looks something like an inverted while loop. The do-while loop is a posttest loop, which means its expression is tested after each iteration. Show The do-while loop syntax1. Here is the format of the do-while loop when it is used to repeat a single statement: do 2. Here is the format of the while loop when it is used to repeat a block: do
Exampledo-whileExample.cpp The do-while Loop is a Posttest Loop
Practice Program1. Write a do-while loop to read a deposit amount from the user, the valid deposit amount needs to be between 0 and 5000. It continues to prompt the user until a valid deposit amount is read. 2. Wrtie a do-while loop to prompt the user to read an upper-case letter. It continues to prompt until an upper-case letter is entered. In previous post, we went over the concept of control structures, which direct the flow of a program. A loop is a control structure that causes a statement or group of statements to repeat. C++ has three looping control structures: the while loop, the do-while loop, and the for loop. The difference between these structures is how they control the repetition. The while Loop The while loop has two important parts: (1) an expression that is tested for a true or false value, and (2) a statement or block that is repeated as long as the expression is true. The figure below shows the logic of a while loop. Here is the general structure of the while loop: In the general format, // This program demonstrates a simple while loop. This program tests the variable In this example, the number variable is referred to as the loop control variable because it controls the number of times that the loop iterates. The while loop is known as a pretest loop, which means it tests its expression before each iteration. Infinite Loops Loops must contain within themselves a way to terminate. This means that something inside the loop must eventually make the test expression false. The loop in the example above stops when the expression int number = 0; This is an infinite loop because it does not contain a statement that changes the value of the int number = 0; The semicolon at the end of the first line is assumed to be a null statement and disconnects the while statement from the block that comes after it. To the compiler, this loop looks like: while (number < 5); This while loop will forever execute the null statement, which does nothing. The program will appear to have “gone into space” because there is nothing to display screen output or show activity. The do-while Loop The do-while loop looks something like an inverted while loop. The figure below shows the logic of a do-while loop. Here is the do-while loop’s structure when the body of the loop contains multiple statements: The do-while loop is a posttest loop. This means it does not test its expression until it has completed an iteration. As a result, the do-while loop always performs at least one iteration, even if the expression is false to begin with. This differs from the behavior of a while loop, which you will recall is a pretest loop. The for Loop In general, there are two categories of loops: conditional loops and count-controlled loops. A conditional loop executes as long as a particular condition exists. For example, an input validation loop executes as long as the input value is invalid. When you write a conditional loop, you have no way of knowing the number of times it will iterate. Sometimes you know the exact number of iterations that a loop must perform. A loop that repeats a specific number of times is known as a count-controlled loop. For example, if a loop asks the user to enter the sales amounts for each month in the year, it will iterate twelve times. In essence, the loop counts to twelve and asks the user to enter a sales amount each time it makes a count. A count-controlled loop must possess three elements:
Count-controlled loops are so common that C++ provides a type of loop specifically for them. It is known as the for loop. The for loop is specifically designed to initialize, test, and update a counter variable. Here is the format of the for loop when it is used to repeat a block is: The first line of the for loop is the loop header. After the key word for, there are three expressions inside the parentheses, separated by semicolons. (Notice there is not a semi- colon after the third expression.) The first expression is the initialization expression. It is normally used to initialize a counter variable to its starting value. This is the first action performed by the loop, and it is only done once. The second expression is the test expression. This is an expression that controls the execution of the loop. As long as this expression is true, the body of the for loop will repeat. The for loop is a pretest loop, so it evaluates the test expression before each iteration. The third expression is the update expression. It executes at the end of each iteration. Typically, this is a statement that increments the loop’s counter variable. Here is an example of a simple for loop that prints “Hello” five times: for (int count = 0; count < 5; count++) In this loop, the initialization expression is Notice how the counter variable, Deciding Which Loop to UseEach of the three C++ loops is ideal to use in different situations. Here’s a short summary of when each loop should be used.
Optional Topics: Breaking and Continuing a LoopSometimes it’s necessary to stop a loop before it goes through all its iterations. The break statement, which was used with switch in Chapter 4, can also be placed inside a loop. When it is encountered, the loop stops, and the program jumps to the statement immediately following the loop. The while loop in the following program segment appears to execute 10 times, but the break statement causes it to stop after the fifth iteration. int count = 0; Using break in a Nested Loop In a nested loop, the break statement only interrupts the loop it is placed in. The following program segment displays five rows of asterisks on the screen. The outer loop controls the number of rows, and the inner loop controls the number of asterisks in each row. The inner loop is designed to display 20 asterisks, but the break statement stops it during the eleventh iteration. for (int row = 0; row < 5; row++)
The continue Statement The continue statement causes the current iteration of a loop to end immediately. When continue is encountered, all the statements in the body of the loop that appear after it are ignored, and the loop prepares for the next iteration. In a while loop, this means the program jumps to the test expression at the top of the loop. As usual, if the expression is still true, the next iteration begins. In a do-while loop, the program jumps to the test expression at the bottom of the loop, which determines whether the next iteration will begin. In a for loop, continue causes the update expression to be executed and then the test expression to be evaluated. The following program segment demonstrates the use of continue in a while loop: int testVal = 0; This loop looks like it displays the integers 1 through 10. When testVal is equal to 4, however, the continue statement causes the loop to skip the cout statement and begin the next iteration. The output of the loop is
What type of loop is the doThe do-while loop is a posttest loop, which means its expression is tested after each iteration.
Do While is a posttest loop?Do while loops check the condition after the block of code is executed. This control structure can be known as a post-test loop. This means the do-while loop is an exit-condition loop. However a while loop will test the condition before the code within the block is executed.
What are the pretest loops?pretest loop: A loop that tests the condition before each iteration. posttest loop: A loop that tests the condition after each iteration.
Is a doThe do/while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
|