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
- DO Loop: Used to repeat a block of code a specific number of times or while a condition is true.
- 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.
Explanation:
DO i = 1 TO 5
: Initializesi
to 1 and increments it by 1 until it reaches 5.SAY 'Iteration:' i
: Prints the current value ofi
.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.
Explanation:
i = 1
: Initializesi
to 1.DO WHILE i <= 5
: Continues the loop as long asi
is less than or equal to 5.SAY 'Iteration:' i
: Prints the current value ofi
.i = i + 1
: Incrementsi
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.
Explanation:
i = 1
: Initializesi
to 1.DO UNTIL i > 5
: Continues the loop untili
is greater than 5.SAY 'Iteration:' i
: Prints the current value ofi
.i = i + 1
: Incrementsi
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
: Initializesi
to 1 and increments it by 1 until it reaches 10.IF i = 5 THEN LEAVE
: Exits the loop wheni
equals 5.SAY 'Iteration:' i
: Prints the current value ofi
.END
: Marks the end of the loop.SAY 'Loop exited at iteration' i
: Prints the value ofi
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 2: DO WHILE Loop
Write a REXX program that prints the numbers from 1 to 10 using a DO WHILE
loop.
Solution:
Exercise 3: DO UNTIL Loop
Write a REXX program that prints the numbers from 1 to 10 using a DO UNTIL
loop.
Solution:
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
andDO 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.
REXX Programming Course
Module 1: Introduction to REXX
- What is REXX?
- Setting Up the REXX Environment
- Hello World in REXX
- Basic Syntax and Structure
- Variables and Data Types
Module 2: Basic Programming Concepts
- Operators and Expressions
- Control Structures: IF/THEN/ELSE
- Loops: DO and LEAVE
- Input and Output
- Basic String Manipulation
Module 3: Intermediate REXX Programming
Module 4: Advanced REXX Programming
- Advanced String Manipulation
- Parsing Techniques
- Interfacing with External Programs
- REXX Macros
- Performance Optimization