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
- IF Statement: Executes a block of code if a specified condition is true.
- ELSEIF Statement: Provides an additional condition if the initial
IF
condition is false. - ELSE Statement: Executes a block of code if none of the preceding conditions are true.
- 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.
ELSEIF Statement
The ELSEIF
statement provides an alternative condition if the initial IF
condition is false.
ELSE Statement
The ELSE
statement executes a block of code if none of the preceding conditions are true.
DO and ENDDO
The DO
and ENDDO
statements are used to group multiple statements within a conditional block.
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 correspondingENDDO
. - 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.
CL (Control Language) Course
Module 1: Introduction to CL
- What is Control Language?
- Setting Up Your Environment
- Basic Syntax and Structure
- Writing Your First CL Program
Module 2: Basic CL Commands
- Introduction to CL Commands
- File Management Commands
- Job Management Commands
- System Management Commands
Module 3: Variables and Expressions
Module 4: Control Structures
Module 5: Advanced CL Commands
- Advanced File Operations
- Advanced Job Scheduling
- System Configuration Commands
- Security and Permissions