5.4: The 'while' and 'do-while' Loops
Please read this chapter. Make sure to click on the arrows to navigate through these webpages. You may also click on the sound icon at the top of the webpage to listen to the MP3. This chapter explains while loop.
After reading the tutorial, you will understand how a do-while loop works. In a do-while loop, the body of the loop is executed at least one time, whereas in a while loop, the loop may never execute if the loop condition is false. Please read the pages starting with the heading "Bottom Driven Loop" to understand the advantages of this loop.
At the end of this tutorial, you will understand the difference between while and do-while loops. They both are essentially the same except that the while loop is a pre-test loop and may never execute if the condition is false to begin with. On the other hand, the do-while loop is a post-test loop and always executes at least one time, even if the condition is false.
Please complete the fill in the blank exercise. Think of the correct response to fill in the blank for each question, or write your response down on a separate piece of paper. Then, click on the blank to reveal the correct answer.
Please read this chapter. This chapter discusses the do loop, which is a variation of the while loop.
Attempt this ungraded quiz.
Please watch this 6-minute lecture. This lecture explains loops by applying them to a simple application, manipulating lists. Though the video uses Python programming language to explain the while loop, the basic concept of the while loop remains the same. The while loop requires a statement in the body of the loop that updates the control variable. In the next section, we will learn about the 'for' loop, which is another implementation of a loop where the control is automatically updated.