Conditional statements are fundamental in programming as they allow you to execute different code blocks based on certain conditions. In Control Language (CL), conditional statements help control the flow of execution, making your programs more dynamic and responsive to different situations.

Key Concepts

  1. IF Statement: Executes a block of code if a specified condition is true.
  2. ELSEIF Statement: Provides an additional condition if the initial IF condition is false.
  3. ELSE Statement: Executes a block of code if none of the preceding conditions are true.
  4. DO and ENDDO: Used to group multiple statements within a conditional block.

Basic Syntax

IF Statement

The IF statement checks a condition and executes the following block of code if the condition is true.

IF COND(condition) THEN(do_something)

ELSEIF Statement

The ELSEIF statement provides an alternative condition if the initial IF condition is false.

ELSEIF COND(another_condition) THEN(do_something_else)

ELSE Statement

The ELSE statement executes a block of code if none of the preceding conditions are true.

ELSE(do_something_different)

DO and ENDDO

The DO and ENDDO statements are used to group multiple statements within a conditional block.

IF COND(condition) THEN(DO)
    /* Multiple statements */
    statement1
    statement2
ENDDO

Practical Examples

Example 1: Simple IF Statement

PGM
    DCL VAR(&AGE) TYPE(*DEC) LEN(3 0)
    CHGVAR VAR(&AGE) VALUE(25)

    IF COND(&AGE *GE 18) THEN(DO)
        SNDPGMMSG MSG('You are an adult.')
    ENDDO
ENDPGM

Explanation:

  • The program declares a variable &AGE and sets its value to 25.
  • The IF statement checks if &AGE is greater than or equal to 18.
  • If the condition is true, it sends a message "You are an adult."

Example 2: IF-ELSEIF-ELSE Statement

PGM
    DCL VAR(&SCORE) TYPE(*DEC) LEN(3 0)
    CHGVAR VAR(&SCORE) VALUE(85)

    IF COND(&SCORE *GE 90) THEN(DO)
        SNDPGMMSG MSG('Grade: A')
    ENDDO
    ELSEIF COND(&SCORE *GE 80) THEN(DO)
        SNDPGMMSG MSG('Grade: B')
    ENDDO
    ELSEIF COND(&SCORE *GE 70) THEN(DO)
        SNDPGMMSG MSG('Grade: C')
    ENDDO
    ELSE(DO)
        SNDPGMMSG MSG('Grade: F')
    ENDDO
ENDPGM

Explanation:

  • The program declares a variable &SCORE and sets its value to 85.
  • The IF statement checks if &SCORE is greater than or equal to 90.
  • If the condition is true, it sends a message "Grade: A".
  • If the first condition is false, the ELSEIF statement checks if &SCORE is greater than or equal to 80, and so on.
  • If none of the conditions are true, the ELSE statement sends a message "Grade: F".

Practical Exercises

Exercise 1: Age Category

Task: Write a CL program that categorizes a person's age into "Child", "Teenager", "Adult", or "Senior".

Solution:

PGM
    DCL VAR(&AGE) TYPE(*DEC) LEN(3 0)
    CHGVAR VAR(&AGE) VALUE(45)

    IF COND(&AGE *LT 13) THEN(DO)
        SNDPGMMSG MSG('Category: Child')
    ENDDO
    ELSEIF COND(&AGE *LT 20) THEN(DO)
        SNDPGMMSG MSG('Category: Teenager')
    ENDDO
    ELSEIF COND(&AGE *LT 65) THEN(DO)
        SNDPGMMSG MSG('Category: Adult')
    ENDDO
    ELSE(DO)
        SNDPGMMSG MSG('Category: Senior')
    ENDDO
ENDPGM

Exercise 2: Temperature Check

Task: Write a CL program that checks the temperature and sends a message indicating "Cold", "Warm", or "Hot".

Solution:

PGM
    DCL VAR(&TEMP) TYPE(*DEC) LEN(3 0)
    CHGVAR VAR(&TEMP) VALUE(75)

    IF COND(&TEMP *LT 60) THEN(DO)
        SNDPGMMSG MSG('Temperature: Cold')
    ENDDO
    ELSEIF COND(&TEMP *LT 80) THEN(DO)
        SNDPGMMSG MSG('Temperature: Warm')
    ENDDO
    ELSE(DO)
        SNDPGMMSG MSG('Temperature: Hot')
    ENDDO
ENDPGM

Common Mistakes and Tips

  • Missing ENDDO: Ensure every DO block has a corresponding ENDDO.
  • Incorrect Condition Syntax: Double-check the condition syntax, especially the comparison operators.
  • Uninitialized Variables: Always initialize variables before using them in conditions.

Conclusion

In this section, you learned about conditional statements in CL, including the IF, ELSEIF, and ELSE statements, as well as how to group multiple statements using DO and ENDDO. You also saw practical examples and exercises to reinforce your understanding. Mastering conditional statements is crucial for controlling the flow of your CL programs and making them more dynamic and responsive. In the next section, we will explore looping constructs to further enhance your programming skills.

© Copyright 2024. All rights reserved