Loops are things that keep on repeating and repeating until you tell it to stop. There are two types of loops that we'll be learning: for-loops and regular loops.

For-loops increase the value of an integer i (or anything else) by a given amount (default: 1), until it has reached its upper limit.

for i : 1 .. 10 %Start with 1, end with 10, increase i by 1 (i.e. 1,2,3...10)
     put i
end for

That's all there is to it! Whatever's in the middle will be executed each time the loop is run. This program will print all numbers from 1 to 10.

Now there are a few things you may have noticed. Firstly, we haven't declared i. Actually, you're not supposed to declare for-loop variables. In fact, if you do, then it won't work!

What if we want to print all multiples of 5 between 1 and 103?

for i : 0 .. 103 by 5
    put i
end for

Excellent! You're now a for-loop guru! Loops are pretty simple, and the syntax is almost the same. So why would you want to use normal loops instead of for-loops? For-loops are used when you know how many times to repeat it. However, sometimes the amount of times you repeat depends on how many lines there are in a text file. More often, you'll want a program to loop until the user tells you to exit. This is what we'll be doing.

var word : string

     put "Enter any word you want, and I'll tell you what it is! (type 'exit' to exit)"
     get word
     exit when word = "exit"

     put "You entered ", word
end loop

That's pretty self explanatory. We're getting a string from the user, and if the string is exit, then we exit. Turing is a very easy program to use, and it avoids complicated syntax, which becomes handy in cases like this; 'exit when' means exactly that, you exit when the following case is true.

If statements, cases · Data files