Programming Fundamentals/Loops/Do Loops
A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given Boolean condition at the end of the block.[1] This activity introduces do loops. This activity will help you understand how to use do loops in a program.
Objectives
edit- Understand do loop concepts.
- Understand how do loops are specified in a program.
- Understand the difference between while loops and do loops and when to use each one.
- Single-step through a program to observe do loop execution.
Prerequisites
editLearners should already be familiar with while loops.
Introduction
editReview the flowchart example on the right.
Questions
edit- What flowcharting symbol is used to indicate a do loop?
- How are do loops structured?
- What is the difference between a while loop and a do loop?
- When would you want to use a while loop vs. a do loop?
Activity
editWith a partner, perform the following:
- Using a visual programming language, create a program similar to the flowchart on the right based on the following steps.
- Start by creating a do loop that counts from 1 to 5 by 1.
- Save the program.
- Test the program to verify that it works correctly.
- Modify the program to count from 1 to 10 by 1.
- Modify the program to count from 5 to 10 by 1.
- Modify the program to count from 0 to 10 by 2.
- Trade places, so that both partners have an opportunity to "drive" the visual programming environment.
- Modify the program to count from any start value the user enters to 10 by 1.
- Modify the program to count from any start value the user enters to any stop value the user enters by 1.
- Modify the program to count from any start value the user enters to any stop value the user enters by any increment value the user enters.
- Change the environment to use a slow run speed and/or step through the program one shape at a time.
- Working together, create a list of changes that might be made to use do loops in programs you've already written. What parts could be repeated and how would the loop terminate?
Applications
edit- Identify specific steps which must be followed when creating a program using do loops.
- Discuss your activity experience with your classmates. What surprised you? What have you learned that you can apply to your own school or work environment?