This lesson demonstrates how a slight manipulation of a conditional statement can allow for the creation of a new and powerful tool in constructing programs, the "repeat while" and "repeat until" loops. Students are introduced to "repeat while" and "repeat until" loops by analyzing real-life scenarios in which tasks are repeated until they are completed based on conditional statements. They will design their own scenarios to represent a real-world situation that could be represented through "repeat while" and/or "repeat until" loops, and will also learn how to recognize common looping mistakes, most notably "infinite loops." Students will then move to an IDE, creating a "loop structure" that runs exactly some predetermined number of times. While learning about creating "loop structures," students will be introduced to many of the common mistakes early programmers make with loops, and will be introduced to debugging concepts. As they become accustomed to the logical process of the "repeat while structure," students will be introduced to the "repeat until structure," which uses the opposite logical process. Students will finally progress to putting conditional if statements inside a "repeat while" loop to count the number of times an event occurs while repeating the same action. This activity will also remind students of the need for counter variables and foreshadow their further use in the following lessons.



Students will be able to:


The "repeat while" and the "repeat until" loops allow us to set a certain conditional for the loop to either continue repeating or to stop repeating. Thus, both loops use boolean expressions to determine if they will run and how many times. One of the biggest problems a programmer can run into with "repeat while" and "repeat until" loops is the creation of an infinite loop. As such, there are several defensive programming strategies introduced in this lesson to help prevent creating infinite loops.


Introduced Code

Getting Started

This is an optional unplugged activity. You may skip to the Activity if you don't think it would be useful. It may also be something worth returning to after writing code to reinforce the concepts behind loops.

Distribute the "Repeat While Activity Guide" to students. This worksheet gives a few real-life examples of while loops, as well several templates for students to create their own real-life scenarios.

If possible, pair students together for this activity. Once they have created their own real-life "repeat while" and "repeat until" scenarios, have them share their scenario with their work partner and ask themselves the following questions:

Discuss the results of this exchange as a class, keeping the primary focus on whether or not students are properly structuring their loops. These early activities are primarily designed to get students familiar with the structure of the "repeat while" and "repeat until" loops. Common misconceptions include:


In this activity, students will complete a series of Coding Challenges that introduce them to "repeat while" and "repeat until" loops. The lesson begins with the general form of the "repeat while" loop. Next, the concepts of counter variables and "infinite loops" are introduced. Then, the subsequent Coding Challenge demonstrates how the "repeat until" loop works. The activity then gives an example of Defensive Loop Conditions: conditionals that catch more cases than you think you need, helping to avoid infinite loops. Finally, the activity gives us an idea of how loops can become more complex by incorporating if-statements within a loop.

Student Instructions

The "repeat while" loop uses a boolean condition to repeatedly run a block of code. It checks the expression, and if it is TRUE it runs the block of code contained within it. This process of checking the condition and running the block of code is repeated as long as the boolean condition remains TRUE. Once the boolean expression becomes FALSE it will stop.

We are going to start exploring a "repeat while" by modifying the condition on which a "repeat while" runs and using output statements to ensure it is correctly evaluating its condition.

Repeat While loop structure

Add the line of code below in three different places in the code; First - outside of the "repeat while" structure, after the "end," Second - inside of the structure below the "counter = counter + 1," Third - inside of the structure right below the "repeat while counter < 10."

output counter

Repeat While loop structure

Creating an Infinite Loop

Create a "repeat while" statement that is logically impossible to ever become true by modifying the "repeat while" statement or by modifying the counter calculation. Close the tab for the output screen when you find the program does not to stop.

Creating an Infinite Loop

Counters and Infinite Loops

The two exercises you just completed are designed to demonstrate two common concepts in the computer programming:

Repeat Until loop structure

The "repeat until" uses a boolean condition to repeatedly run a block of code. It checks the expression, and if it is FALSE it runs the block of code contained within it. This process of checking the condition and running the block of code is repeated as long as the boolean condition remains FALSE. Once the boolean expression becomes TRUE it will stop. Almost always, "repeat while structure" can be re-written by "repeat until structure." The previous example for the "repeat while" can be re-written in the following way, and both loops do the same thing. Do the same modification you did in the first exercise to the code block below, and observe how the counter works in this case.

Repeat Until loop structure

Defensive Loop Conditions

Review the following code.

integer counter = 0

repeat until counter = 30
   counter = counter + 4
   output counter

This code will run into a significant problem. Can you understand why?

We wanted to stop counting at 30, but when you count by 4's you will actually never hit 30. It would go... 24, 28, 32, 36... What we really want to do is stop when the number is greater than 30. This leads to an important defensive programming strategy: making the conditional catch more cases than you think you need, so that if something does not go exactly as planned the loop will hit the stop condition. This helps avoid creating an infinite loop.

Defense Conditioning

Change the mathematical operator in the line "repeat until counter = 30" so that this code does not create an infinite loop.

Defensive Conditioning

Using an "if-statement" in a Loop

As we have learned throughout this lesson, we can control how many times the loop would repeat by using a counter variable. This is a very common way to use an "iterator."

Scenario: If you roll a pair of dice, rolling a 12 (two sixes) is rare. How rare? If you were to roll a pair of dice 1,000 times, on average, how many times would it come up as 12?

Conditionals in a Loop

To figure this out, we could write code to run an experiment. It would go something like this: declare another counter that counts the occurrences of a sum of 12 (and initialize it to 0). Make a loop that simulates rolling a pair of dice 1,000 times. Inside the loop, add an if statement: if die1 + die2 = 12, then add 1 to the "sum12Counter." * Display the result after the loop, as displaying it for each iteration of the loop could print the result up to 1,000 times, depending on luck.

Hint: You will need to include the use statement of "use Libraries.Compute.Random" to create a variable of type "Random," and then simulate the roll of each individual die using "random:RandomIntegerBetween(1, 6)."

Conditionals in a Loop

Wrap Up

After students finish the activity, start a class dicussion using the following prompt.

In your own words, describe how a "repeat while" and/or a "repeat until" works. Explain two things to pay attention to when creating these. In your response, justify why the name "repeat while" and "repeat until" accurately describes the behavior of these new programming constructs. Also think of some tips that help us to construct a "valid" loop structure, but does not negatively impact the code.

Sample Responses

Standards Alignment