Tutorial: Repeat

How to do things over and over and over and ...

repeat <expression> times

The Quorum programming language uses the word repeat to describe the idea of doing something 0 or more times. This process, sometimes called looping, is an important concept to learn and understand in programming. For example, here is a program that outputs the word Quorum 10 times:

repeat 10 times
     output "Quorum"

We can only repeat something a whole number of times, using integers. While this is the case, we can express the integer in both simple and complex ways. For example, we might try the following:

//create an integer named a with the value of 1
integer a = 1
//create an integer named b with the value of 4
integer b = 4
//repeat any number of times we wish, in this case by using an expression
repeat (b / a + (b - 5)) times
    //output the two values
   output a
   output b

repeat while <expression>

There are three ways we can repeat a construct: repeat times, repeat while, and repeat until. In the next version of repeat, repeat while, we are asking Quorum to continue executing as long as an expression is true, in this case while variable a is less than 15. Then, on each iteration of our repeat, we add one to a, which means it will eventually stop.The following example shows this idea:

//create an integer named a and set it to 0
integer a = 0
//keep looping, but only if a is less than 15
repeat while a < 15
    //add one to a. If we did not have this line, we would loop forever
   a = a + 1
    //send the value of a to the console
   output a

In this example, a will eventually become larger than 15 and the loop will stop executing.

repeat until <expression>

In this final version of the repeat construct, we will learn about repeat until. Repeat until is just like repeat while, except that the expression must evaluate to false in order to keep executing. As such, using a similar example, with repeat until, the loop will have the same behavior as before, except that the condition is flipped.

integer a = 0
//This will execute so long as a is not greater than 15
repeat until a >= 15
   a = a + 1
   output a

These various styles of loops can be used in our programs freely.

Next Tutorial

In the next tutorial, we will discuss conditionals, which describes how to use if statements to make decisions.