Going in Loops

In this section, we introduce another powerful concept in programming: looping. Like the conditional if statements we saw earlier, loops also contain blocks of code, but these blocks are repeatedly executed. Loops are easily spotted in Yas by the repeat keyword.

Times Loop

The simplest kind of loop in Yas is the times loop:

repeat expr times {
    # repeated block
}

where expr is any expression that evaluates to a numeric value.

For example, this loop prints 10 copies of "Hello" to the console:

console

Magic Variable

Inside the times loop, a special "magic" variable _ is set to the number of times that the loop has already completed. During the very first time through the loop, this variable is set to 0 (zero), and each time around the loop, its value is incremented by 1.

For example, here is a program that prints the table of squares and cubes of the first 10 integers starting at 0:

console

Nesting Loops and Magic Variables

You can nest a loop inside another loop, but the _ magic variable will always refer only to the innermost currently executing loop. When the inner loop exits, the _ will flip back to the value corresponding to the outer loop:

console

While Loop

Another type of loop in Yas is the while loop. It repeats for as long as the termination condition guarding the loop is equal to true:

repeat while condition {
    # execute this block
}

The condition must be a Boolean value, and is evaluated each time before the loop executes. If the condition is found to be true, the loop proceeds with the next round. Otherwise, the loop terminates and the flow continues with the next statement after the loop block.

console

The magic _ variable works the same way inside a while loop as inside a times loop: it keeps track of how many cycles the loop has already completed.

If the loop condition never assumes a false value, then that loop could continue forever, a situation known as the infinite loop. An infinite loop usually indicates some kind of logic error. The only way to break an infinite loop is to stop the program altogether.

Until Loop

Similarly to the while loop, the until loop executes its block until some condition becomes true.

repeat {
    # execute this block until the condition becomes true.
} until condition

The major difference is that in the until loop, the block will be executed at least once and the loop termination condition is executed after each time the block is executed. If the condition is found to be true, then the until loop will terminate and proceed to the next statement following the loop itself.

The magic _ variable can again be used:

console

Exercises

Repeat Until Even

Write a loop that would repeatedly prompt the user for a number (any number) and terminate when the provided number is even (divisible by 2).

console

Multiplication Table

Produce the multiplication table for the first twenty integers. Hint: You might want to use two nested times loops, and use string concatenation to form the rows of the table.

console

Back (040-making-decisions)
Forward (050-advanced-logic)