# Looping Instructions

After completing this chapter, you will be able to describe the Looping and Iteration and Stem variables available in REXX to develop simple applications.

There are two types of looping instructions, repetitive loops and conditional loops. Repetitive loops allow you to repeat instructions a certain number of times, and conditional loops use a condition to control repeating. All loops, regardless of the type, begin with the DO keyword and end with the END keyword.

### Repetitive Loops:

The simplest loop tells the language processor to repeat a group of instructions a specific number of times using a constant following the keyword DO.

 ```DO 5 SAY 'Hello!' END```

When you run this example, you see five lines of Hello!.

 ```Hello! Hello! Hello! Hello! Hello!```

You can also use a variable in place of a constant as in the following example, which gives you the same results.

 ```number = 5 DO number SAY 'Hello!' END```

A variable that controls the number of times a loop repeats is called a control variable. Unless you specify otherwise, the control variable increases by 1 each time the loop repeats.

 ```DO number = 1 TO 5 SAY 'Loop' number SAY 'Hello!' END SAY 'Dropped out of the loop when number reached' number```

This example results in five lines of Hello! preceded by the number of the loop. The number increases at the bottom of the loop and is tested at the top.

 ```Loop 1 Hello! Loop 2 Hello! Loop 3 Hello! Loop 4 Hello! Loop 5 Hello! Dropped out of the loop when number reached 6```

You can change the increment of the control variable with the keyword BY as follows:

 ```DO number = 1 TO 10 BY 2 SAY 'Loop' number SAY 'Hello!' END SAY 'Dropped out of the loop when number reached' number```

This example has results similar to the previous example except the loops are numbered in increments of two.

 ```Loop 1 Hello! Loop 3 Hello! Loop 5 Hello! Loop 7 Hello! Loop 9 Hello! Dropped out of the loop when number reached 11```

### Infinite Loops:

What happens when the control variable of a loop cannot attain the last number? For example, in the following exec segment, count does not increase beyond 1.

 ```DO count = 1 to 10 SAY 'Number' count count = count - 1 END```

The result is called an infinite loop because count alternates between 1 and 0 and an endless number of lines saying Number 1 appear on the screen.

#### IMPORTANT - Stopping an Infinite Loop:

When you suspect an exec is in an infinite loop, you can end the exec by pressing the attention interrupt key, sometimes labeled PA1. You will then see message IRX0920I. In response to this message, type HI for halt interpretation and press the Enter key. If that doesn't stop the loop, you can press the attention interrupt key again, type HE for halt execution, and press the Enter key.

### DO FOREVER Loops:

Sometimes you might want to purposely write an infinite loop; for instance, in an exec that reads records from a data set until it reaches end of file, or in an exec that interacts with a user until the user enters a particular symbol to end the loop.

You can use the EXIT instruction to end an infinite loop when a condition is met, as in the following example. More about the EXIT instruction appears in â€•EXIT

#### Example Using a DO FOREVER Loop:

 ```/******************************* REXX *********************************/ /* This This exec prints data sets named by a user until the */ /* user enters a null line. */ /**********************************************************************/ DO FOREVER SAY 'Enter the name of the next data set or a blank to end.' PULL dataset_name IF dataset_name = '' THEN EXIT ELSE DO "PRINTDS DA("dataset_name")" SAY dataset_name 'printed.' END END```

This example sends data sets to the printer and then issues a message that the data set was printed. When the user enters a blank, the loop ends and so does the exec. To end the loop without ending the exec, use the LEAVE instruction, as described in the following topic.

### LEAVE Instruction:

The LEAVE instruction causes an immediate exit from a repetitive loop. Control goes to the instruction following the END keyword of the loop. An example of using the LEAVE instruction follows:

#### Example Using the LEAVE Instruction:

 ```/******************************** REXX ********************************/ /* This exec uses the LEAVE instruction to exit from a DO FOREVER loop*/ /* that sends data set to the printer */ /**********************************************************************/ DO FOREVER SAY 'Enter the name of the next data set.' SAY 'When there are no more data sets, enter QUIT.' PULL dataset_name IF dataset_name = 'QUIT' THEN LEAVE ELSE DO "PRINTDS DA("dataset_name")" SAY dataset_name 'printed.' END END SAY 'Good-bye.' EXIT```

### ITERATE Instruction:

Another instruction, ITERATE, stops execution from within the loop and passes control to the DO instruction at the top of the loop. Depending on the type of DO instruction, a control variable is increased and tested and/or a condition is tested to determine whether to repeat the loop. Like LEAVE, ITERATE is used within the loop.

 ```DO count = 1 TO 10 IF count = 8 THEN ITERATE ELSE SAY 'Number' count END```

This example results in a list of numbers from 1 to 10 with the exception of number 8.

 ```Number 1 Number 2 Number 3 Number 4 Number 5 Number 6 Number 7 Number 9 Number 10```

### Conditional Loops:

There are two types of conditional loops, DO WHILE and DO UNTIL. Both types of loops are controlled by one or more expressions. However, DO WHILE loops test the expression before the loop executes the first time and repeat only when the expression is true. DO UNTIL loops test the expression after the loop executes at least once and repeat only when the expression is false.

Use a DO WHILE loop when you want to execute the loop while a condition is true. DO WHILE tests the condition at the top of the loop. If the condition is initially false, the loop is never executed.

You can use a DO WHILE loop instead of the DO FOREVER loop in the example using the LEAVE Instruction. However, you need to initialize the loop with a first case so the condition can be tested before you get into the loop. Notice the first case initialization in the beginning three lines of the following example.

#### Example Using DO WHILE:

 ```/********************************* REXX *******************************/ /*This exec uses DO WHILE loop to send data sets to the system printer*/ /**********************************************************************/ SAY 'Enter the name of a data set to print.' SAY 'If there are no data sets, enter QUIT.' PULL dataset_name DO WHILE dataset_name \= 'QUIT' "PRINTDS DA("dataset_name")" SAY dataset_name 'printed.' SAY 'Enter the name of the next data set.' SAY 'When there are no more data sets, enter QUIT.' PULL dataset_name END SAY 'Good-bye.'```

### DO UNTIL Loops:

Use DO UNTIL loops when a condition is not true and you want to execute the loop until the condition is true. The DO UNTIL loop tests the condition at the end of the loop and repeats only when the condition is false. Otherwise the loop executes once and ends.

#### Example Using DO UNTIL:

 ```/****************************** REXX ***********************************/ /*This exec uses a DO UNTIL loop to ask for a password. If the password*/ /* is incorrect password three times, the loop ends */ /***********************************************************************/ password = 'abracadabra' time = A DO UNTIL (answer = password) | (time = 3) SAY 'What is the password?' PULL answer time = time + 1 END```

### Combining Types of Loops:

You can combine repetitive and conditional loops to create a compound loop. The following loop is set to repeat 10 times while a certain condition is met, at which point it stops.

 ```quantity = 20 DO number = 1 TO 10 WHILE quantity < 5A quantity = quantity + number SAY 'Quantity = 'quantity ' (Loop 'number')' END```

The result of this example is as follows:

 ```Quantity = 21 (Loop 1) Quantity = 23 (Loop 2) Quantity = 26 (Loop 3) Quantity = 3A (Loop 4) Quantity = 35 (Loop 5) Quantity = 41 (Loop 6) Quantity = 48 (Loop 7) Quantity = 56 (Loop 8)```

You can substitute a DO UNTIL loop, change the comparison operator from < to >, and get the same results.

 ```quantity = 20 DO number = 1 TO 10 UNTIL quantity > 5A quantity = quantity + number SAY 'Quantity = 'quantity ' (Loop 'number')' END```

### Nested DO Loops:

Like nested IF/THEN/ELSE instructions, DO loops can also be within other DO loops. A simple example follows:

 ```DO outer = 1 TO 2 DO inner = 1 TO 2 SAY 'HIP' END SAY 'HURRAH' END```

#### The output from this example is as follows:

 ```HIP HIP HURRAH HIP HIP HURRAH```

If you need to leave a loop when a certain condition arises, use the LEAVE instruction followed by the control variable of the loop. If the LEAVE instruction is for the inner loop, you leave the inner loop and go to the outer loop. If the LEAVE instruction is for the outer loop, you leave both loops.

To leave the inner loop in the preceding example, add an IF/THEN/ELSE instruction that includes a LEAVE instruction after the IF instruction.

 ```DO outer = 1 TO 2 DO inner = 1 TO 2 IF inner > 1 THEN LEAVE inner ELSE SAY 'HIP' END SAY 'HURRAH' END```

The result is as follows:

 ```HIP HURRAH HIP HURRAH```

### Summary:

• The following model of DO..END may be used for looping purpose

 ```DO modifiers instruction(s) END```

• Modifiers can be WHILE, UNTIL, FORVER, VARIABLE or a NUMBER.

If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!