The first thing I did when I got my Sinclair Spectrum personal computer back in the 1980s was to write a basic program. It went something like this. Ten, print, hello, 20, go to 10. The second thing I ever did on my spectrum was to figure out how to escape from a running program which brings us nicely to the topic of loops. We've seen that Do keyword instruction used to identify Do-End groups that follow there not else keywords. The do instruction is also used to create looping structures in Rexx. A loop is a set of Rexx instructions that can be executed over and over again. There must obviously be some type of control structure to tell Rexx when to stop executing the loop. There are five types of loop in Rexx. Loop while a logical expression remains true, Loop until a logical expression becomes true, controlled repetitive loop, loop a specific number of times, and the ominous loop forever. In a Do While loop, a logical expression is evaluated at the Start and is re-evaluated for each iteration of the loop. If the expression returns a value of one which is true, the loop instructions are performed. If the expression returns a value of zero or false the loop ends and control passes to the instruction following the end instruction. Because the expression is evaluated before the first time the loop is executed, the loop may not even run once. Here's the syntax for the Do While loop. The logical expressions tested at the beginning of the loop each time and if the result is one, true, the loop instructions are executed until you reach the end instruction. Then the logical expression is evaluated again. As long as the result of the expression is true, the loop will keep on executing. When the result of the logical expression test is false, control is passed to the next instruction following the end instruction. The Do While loop in this example, starting with a varl 1 and 12 and subtracting one each time will run for a total of six iterations. The Do Until loop is the opposite of Do While. Where the Do While performances its test first before processing the loop instructions, the Do Until processes the loop instructions first then performances its test. This means that the Do Until will always perform its loop instructions at least once. Also where the Do While performs the loop instructions if the logical expression result is true, the Do Until will perform the loop instruction again, if the logical expression result is false. When the logical expression result is true, the Do Until loop ends and control is passed on to the next instruction after the end of the loop. Even though the Do Until is coded in much the same way as the Do While, it works in the opposite way. Do Until always executes its loop instructions at least once and then tests the logical expression. The example here does the following. A variable is set to the value 1, if value is displayed on the screen. The variable is incremented, then the variable is tested to see whether it's greater than six. If not, then the loop executes again. When the value tested is greater than six, then loop ends. The output produced will be 1, 2, 3, 4, 5 then 6. Again, six iterations. Controlled repetitive is the most useful looping construct but it's also the most difficult to code. It's useful because it gives you a self incrementing variable that is usable within the loop and after the loop is ended. The variable may have any name you want, but you'll often see the variable name I used as an example. The init value is the initial value to assign to the variable. This value must be a number. The value of the control variable is compared to the final value each time through the loop. When the value of the control variable exceeds the final value the loop ends. The default increment value is one, but you may change it to any numeric value you wish to use. As the control variable is just a Rexx variable usable within the loop itself It can be reset to any value while the loop executes. This could lead to infinite loops but fortunately we have a Max loops to a rescue. The Max loops value sets another loop counter. When the loop has executed this many times, the loop will end. But wait, there's more. In the first construct loop a specific number of times, the Do instruction may be followed by any Rexx expression which results in a positive whole number or zero. The second structure is similar to the Do I equals 1 instruction. The difference is that Do Forever does not give you the self incrementing Rexx variable. Once again, though there must be some type of control structure inside the loop that will cause its termination. Analysis of programming errors long ago revealed that the Go To instruction was used by programmers who like to create spaghetti-like code and was a source of the worst bugs and program failures. For that reason, Rexx does not have a Go To instruction. Instead, it might show identify the main users of the Go To instruction and design specific instructions to perform its tasks. Those instructions are shown here. There is a difference between return and exit and we'll discuss that later but for now we'll just say that return and exit do the same thing. They terminate the currently running routine and return control to the corner of the routine. The Leave instruction allows you to leave a loop but not the entire exec. When the Leave instruction is encountered, the rest of the loop instructions have bypassed and control this pass to the next instruction following the end instruction. The Iterate instruction allows you to go to the end of a loop and re-execute the loop control part. It feels like you go to the start of a loop but strictly speaking, you go to the End instruction and perform end of loop processing. In other words, the rest of the loop instructions bypass and control is passed to the End instruction. Variables that incremented if necessary, logical expressions are tested, and Rexx determines whether to execute the next iteration of the loop. You may include a loop control variable name if one exists on the Leave or Iterate construction to control which loop has been affected by those instructions. Without the control variable name you will leave or iterate the currently active loop. In the example on the visual, the logic that controls the loop only occurs inside the loops not on a Do instructions hence the While or Until methods of loop control would only lead to more complex programming and more potential errors. The use of leave and iterate lead to a simpler program. The use of the loop control variable allows us to explicitly identify to which loop the end, lead, and iterate instructions refer. We've now reached the end instruction on this video unless you want to loop back, you can watch the whole thing again.