In this section, we will explore the concepts of loops in REXX, focusing on the DO and LEAVE constructs. Loops are fundamental in programming as they allow you to execute a block of code multiple times. Understanding how to use loops effectively will enable you to write more efficient and concise code.

Key Concepts

  1. DO Loop: Used to repeat a block of code a specific number of times or while a condition is true.
  2. LEAVE Statement: Used to exit a loop prematurely.

DO Loop

The DO loop in REXX can be used in several ways:

  • Simple DO Loop: Repeats a block of code a fixed number of times.
  • DO WHILE Loop: Repeats a block of code while a condition is true.
  • DO UNTIL Loop: Repeats a block of code until a condition becomes true.

Simple DO Loop

A simple DO loop repeats a block of code a specified number of times.

/* Simple DO Loop Example */
DO i = 1 TO 5
    SAY 'Iteration:' i
END

Explanation:

  • DO i = 1 TO 5: Initializes i to 1 and increments it by 1 until it reaches 5.
  • SAY 'Iteration:' i: Prints the current value of i.
  • END: Marks the end of the loop.

DO WHILE Loop

A DO WHILE loop repeats a block of code as long as a condition is true.

/* DO WHILE Loop Example */
i = 1
DO WHILE i <= 5
    SAY 'Iteration:' i
    i = i + 1
END

Explanation:

  • i = 1: Initializes i to 1.
  • DO WHILE i <= 5: Continues the loop as long as i is less than or equal to 5.
  • SAY 'Iteration:' i: Prints the current value of i.
  • i = i + 1: Increments i by 1.
  • END: Marks the end of the loop.

DO UNTIL Loop

A DO UNTIL loop repeats a block of code until a condition becomes true.

/* DO UNTIL Loop Example */
i = 1
DO UNTIL i > 5
    SAY 'Iteration:' i
    i = i + 1
END

Explanation:

  • i = 1: Initializes i to 1.
  • DO UNTIL i > 5: Continues the loop until i is greater than 5.
  • SAY 'Iteration:' i: Prints the current value of i.
  • i = i + 1: Increments i by 1.
  • END: Marks the end of the loop.

LEAVE Statement

The LEAVE statement is used to exit a loop prematurely. This can be useful when you need to break out of a loop based on a specific condition.

/* LEAVE Statement Example */
DO i = 1 TO 10
    IF i = 5 THEN LEAVE
    SAY 'Iteration:' i
END
SAY 'Loop exited at iteration' i

Explanation:

  • DO i = 1 TO 10: Initializes i to 1 and increments it by 1 until it reaches 10.
  • IF i = 5 THEN LEAVE: Exits the loop when i equals 5.
  • SAY 'Iteration:' i: Prints the current value of i.
  • END: Marks the end of the loop.
  • SAY 'Loop exited at iteration' i: Prints the value of i when the loop was exited.

Practical Exercises

Exercise 1: Simple DO Loop

Write a REXX program that prints the numbers from 1 to 10.

Solution:

/* Exercise 1 Solution */
DO i = 1 TO 10
    SAY i
END

Exercise 2: DO WHILE Loop

Write a REXX program that prints the numbers from 1 to 10 using a DO WHILE loop.

Solution:

/* Exercise 2 Solution */
i = 1
DO WHILE i <= 10
    SAY i
    i = i + 1
END

Exercise 3: DO UNTIL Loop

Write a REXX program that prints the numbers from 1 to 10 using a DO UNTIL loop.

Solution:

/* Exercise 3 Solution */
i = 1
DO UNTIL i > 10
    SAY i
    i = i + 1
END

Exercise 4: LEAVE Statement

Write a REXX program that prints the numbers from 1 to 10 but exits the loop when the number is 7.

Solution:

/* Exercise 4 Solution */
DO i = 1 TO 10
    IF i = 7 THEN LEAVE
    SAY i
END
SAY 'Loop exited at iteration' i

Common Mistakes and Tips

  • Off-by-One Errors: Ensure your loop conditions are correctly set to avoid running one iteration too many or too few.
  • Infinite Loops: Be cautious with DO WHILE and DO UNTIL loops to ensure the condition will eventually become false or true, respectively.
  • LEAVE Usage: Use LEAVE judiciously to avoid making your code harder to read and maintain.

Conclusion

In this section, we covered the basics of loops in REXX, focusing on the DO and LEAVE constructs. We explored different types of DO loops and how to use the LEAVE statement to exit loops prematurely. By practicing the provided exercises, you should now have a solid understanding of how to implement loops in your REXX programs. In the next section, we will delve into input and output operations in REXX.

© Copyright 2024. All rights reserved