Loop

LOOP Statement Introduction

The LOOP statement is the simplest loop structure in Turing. It enables the programmer to loop as many times as they wish. If you even want to loop indefinitely.

LOOP Syntax

The LOOP statement has very basic syntax:

loop
    %Your Code Goes Here
end loop

This will loop until you close the program, the power turns off, or until your program crashes.

Exit When

In order to get more use out of our loop, we can type:

loop
    %Your Code Goes Here
    exit when <BooleanCondition>
    %Your Code Goes Here
end loop

This will constantly execute whatever is in the loop, but when the Boolean Condition is true, then the loop will exit.
It is interesting to note that you can place the exit when anywhere in the LOOP, and you can have as many exit when conditions as you wish.
An example of a LOOP with an EXIT statement would be:
iTotal := 0
loop
    put "Please input an integer (Enter 0 to quit) "..
    get iUserInput
    iTotal := iTotal + iUserInput
    put "The current total is: ",iTotal
    exit when iUserInput = 0   
end loop

This code will always loop, asking the user to input a number. It will take that number and add it to iTotal. When the user wants to quit, they simply enter 0.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License