Hour of Code: Code with Mary

Part 4: Clothing Picker

Repeat

Control Structures: Repeat

If we want the computer to repeat a task multiple times, we can use a loop in our program. This is usually a lot less work than typing the instruction repeatedly and more flexible because we can have the instructions executed a variable number of times.

In Quorum, we create a loop using the keyword repeat.

We mark the end of a loop in the same way we marked the end of an if statement, by using the keyword end.

In this example, we will explore three different ways to control loops using the statements: repeat {number} times, repeat while {condition} and repeat until {condition}.

Control Structures: Repeat Times

The most basic type of loop involves just telling the computer how many times to repeat something.

In Quorum, if we want to do something 5 times, we can type: repeat 5 times or if we have a numeric variable, we can place that in the "5" position like: repeat x times.

Try it!

Control Structures: Repeat While

An alternative type of loop is to repeat a block of code while a certain condition is met.

In Quorum we do this by using a repeat while statement. After the keyword while, we include a condition, just like we did with an if statement.

If we want to mimic the last example, we could just repeat while our variable is less than or equal to 10.

Note that the value of the variable will be 12, but the output statement is skipped when the repeat condition is false.

Try it!

Control Structures: Repeat Until

The final way of representing a loop in Quorum is to use a repeat until statement, which is very similar to the repeat while loop.

The difference is that the repeat until loop stops when a specific condition is met instead of repeating while a condition is met.

You can represent the same logical control with either format, so it is your choice which one you use, but sometimes one is more natural than the other.

Try it!

Control Structures: Additional Information

More documentation on repeat can be found here.

Try it Yourself