Control structures are fundamental in any programming language as they allow you to control the flow of your program based on certain conditions. In REXX, the IF/THEN/ELSE statement is used to execute code conditionally.

Key Concepts

  1. IF Statement: Evaluates a condition and executes the code block if the condition is true.
  2. THEN Clause: Specifies the code to execute when the IF condition is true.
  3. ELSE Clause: Specifies the code to execute when the IF condition is false.

Basic Syntax

The basic syntax for an IF/THEN/ELSE statement in REXX is as follows:

IF condition THEN
    statement
ELSE
    statement
  • condition: A logical expression that evaluates to true or false.
  • statement: The code to execute based on the evaluation of the condition.

Practical Examples

Example 1: Simple IF/THEN

/* Simple IF/THEN example */
age = 20

IF age >= 18 THEN
    SAY "You are an adult."

Explanation:

  • The variable age is set to 20.
  • The IF statement checks if age is greater than or equal to 18.
  • Since the condition is true, the program prints "You are an adult."

Example 2: IF/THEN/ELSE

/* IF/THEN/ELSE example */
age = 16

IF age >= 18 THEN
    SAY "You are an adult."
ELSE
    SAY "You are a minor."

Explanation:

  • The variable age is set to 16.
  • The IF statement checks if age is greater than or equal to 18.
  • Since the condition is false, the program executes the ELSE clause and prints "You are a minor."

Example 3: Nested IF/THEN/ELSE

/* Nested IF/THEN/ELSE example */
score = 85

IF score >= 90 THEN
    SAY "Grade: A"
ELSE IF score >= 80 THEN
    SAY "Grade: B"
ELSE IF score >= 70 THEN
    SAY "Grade: C"
ELSE
    SAY "Grade: F"

Explanation:

  • The variable score is set to 85.
  • The first IF statement checks if score is greater than or equal to 90. This condition is false.
  • The second IF statement (nested within the ELSE clause) checks if score is greater than or equal to 80. This condition is true, so the program prints "Grade: B."

Practical Exercises

Exercise 1: Determine Voting Eligibility

Write a REXX program that determines if a person is eligible to vote based on their age.

Code:

/* Determine voting eligibility */
age = 17

IF age >= 18 THEN
    SAY "You are eligible to vote."
ELSE
    SAY "You are not eligible to vote."

Solution:

  • Set the variable age to 17.
  • Use an IF/THEN/ELSE statement to check if age is greater than or equal to 18.
  • Print the appropriate message based on the condition.

Exercise 2: Grade Classification

Write a REXX program that classifies a student's grade based on their score.

Code:

/* Grade classification */
score = 75

IF score >= 90 THEN
    SAY "Grade: A"
ELSE IF score >= 80 THEN
    SAY "Grade: B"
ELSE IF score >= 70 THEN
    SAY "Grade: C"
ELSE IF score >= 60 THEN
    SAY "Grade: D"
ELSE
    SAY "Grade: F"

Solution:

  • Set the variable score to 75.
  • Use nested IF/THEN/ELSE statements to classify the grade based on the score.
  • Print the appropriate grade.

Common Mistakes and Tips

  • Missing THEN: Ensure you always include the THEN keyword after the IF condition.
  • Proper Indentation: While REXX does not require indentation, it is good practice to indent your code for better readability.
  • Logical Conditions: Make sure your conditions are logical expressions that evaluate to true or false.

Conclusion

In this section, you learned how to use the IF/THEN/ELSE control structure in REXX to execute code conditionally. You practiced writing simple and nested conditional statements and understood common mistakes to avoid. This knowledge is essential for controlling the flow of your programs and making decisions based on different conditions. In the next section, you will learn about loops in REXX, which will further enhance your ability to control program flow.

© Copyright 2024. All rights reserved