How is the Do While Loop structure different?
In light of the work we’ve looked at here with the Do While structure, we might consider the following questions:
- How is the Do While loop structure different than the other loops we’ve studied on the TI-Nspire™ CX II graphing calculator (specifically, the For loop and the more general case of the While loop)?
- What role do If conditional statements play in this Do While loop structure?
Note that the Do While loop structure is always processed at least once, since it doesn’t have a condition that is checked prior to entry into the loop (here, we model this with a While loop where the initial condition is set to True). This is quite different than the For and (more general) While loops we studied in the workshop, as these loops have conditions that are checked before the loop is ever entered into (if at all).
Since there isn’t an initial condition that is checked for a Do While loop, the programmer must build a break command into the loop, or the loop will act as an infinite loop (running again and again and again …). An If conditional statement houses this break command to end the loop. The condition on the If statement determines whether the loop will run again or whether the loop will exit.
Note that this loop structure is slightly different than Do While loop structures that appear in other programming languages. Python doesn’t have a built-in Do While loop structure, but the behavior of a Do While loop can be modeled with programs that use While, If and break commands as we’re exploring here.
Formalizing Do While loops
Let’s use an interactive notes page to formalize the Do While loop behavior that these structures model.
Before we do that, take a minute to trace through the program outlined in the screenshot below. What do you think the output of this program will be if the program is executed with a method call of multiples(50)? Take a minute to trace through the loop and write down your prediction.
Here are two pictures of the Do While Loops notes page designed to capture our important learnings about Do While loops. This notes page uses the program multiples(x) that you just traced through above.
How does the output shown in the notes above match with your earlier prediction?
In this notes page, we’ve used a table to trace through the loop, noting the value of x, actions taken within the loop, and the check that is run during each iteration of the loop. Tracing a loop with a table might seem unnecessary in a loop such as the one shown here. However, this is an important strategy that can be very helpful when tracing increasingly complex loops.
Also note the identification of the four parts of the loop: the start of the loop (or initial value of x), the check (to see if the loop will exit or run again), the action (within the body of the loop), and the step (to increment x to its next value, moving closer and closer to the loop eventually exiting).
Lastly, consider how different initial values of x would have affected the output of this program. What if we had executed the program with a method call of multiples(0)? In this case, the initial value of x would have already been less than 10 (the If condition that leads to the break of the loop). However, since this condition isn’t checked until after the loop has already been entered (and, specifically, after the step and action commands in this particular loop), the loop would still run one time and would display an output of -10 before the loop exited. (Do you see why?) This is a great example of the fact that post-test loops (such as Do While loops, modeled here with a While loop) always run at least one time.
Reflecting on Do While loops
In this activity, we explored Do While Loops, a form of post-test loop that always runs at least one time.
- When might we use a While loop with a built-in break command? Why?
- When might the fact that a post-test loops always executes at least once be helpful?
The next activity in this series will consider all three loop structures (For, While, and Loop) in contrast to each other. We will explore how the Do While loop structure used here is similar to and different than a For loop and a While loop. Additionally, we will explore the use of counters and accumulators.
This series includes: