Control structures are fundamental in any programming language, including RPG. They allow you to control the flow of your program, making decisions, repeating actions, and branching out to different parts of your code based on certain conditions. In this section, we will cover the following control structures in RPG:
- Conditional Statements
- IF/ELSE
- SELECT/WHEN
- Looping Constructs
- DO/ENDDO
- FOR/ENDFOR
- DOW/ENDDO
- DOU/ENDDO
Conditional Statements
IF/ELSE
The IF/ELSE statement is used to execute a block of code if a specified condition is true. If the condition is false, another block of code can be executed using the ELSE statement.
Syntax:
IF condition; // Code to execute if condition is true ELSE; // Code to execute if condition is false ENDIF;
Example:
DCL-S age INT(3); age = 20; IF age >= 18; DSPLY 'You are an adult.'; ELSE; DSPLY 'You are a minor.'; ENDIF;
SELECT/WHEN
The SELECT/WHEN statement is used to execute one block of code among many based on different conditions.
Syntax:
SELECT;
WHEN condition1;
// Code to execute if condition1 is true
WHEN condition2;
// Code to execute if condition2 is true
OTHER;
// Code to execute if none of the above conditions are true
ENDSL;Example:
DCL-S grade CHAR(1);
grade = 'B';
SELECT;
WHEN grade = 'A';
DSPLY 'Excellent!';
WHEN grade = 'B';
DSPLY 'Good job!';
WHEN grade = 'C';
DSPLY 'You passed.';
OTHER;
DSPLY 'Better luck next time.';
ENDSL;Looping Constructs
DO/ENDDO
The DO/ENDDO loop is used to repeat a block of code a specific number of times.
Syntax:
Example:
FOR/ENDFOR
The FOR/ENDFOR loop is similar to the DO/ENDDO loop but provides more control over the loop variable.
Syntax:
Example:
DOW/ENDDO
The DOW/ENDDO loop (Do While) repeats a block of code as long as a specified condition is true.
Syntax:
Example:
DOU/ENDDO
The DOU/ENDDO loop (Do Until) repeats a block of code until a specified condition becomes true.
Syntax:
Example:
Practical Exercises
Exercise 1: Age Category
Write a program that categorizes a person's age into "Child", "Teenager", "Adult", or "Senior" using IF/ELSE statements.
Solution:
DCL-S age INT(3); age = 45; IF age < 13; DSPLY 'Child'; ELSEIF age < 20; DSPLY 'Teenager'; ELSEIF age < 65; DSPLY 'Adult'; ELSE; DSPLY 'Senior'; ENDIF;
Exercise 2: Grade Evaluation
Write a program that evaluates a student's grade using SELECT/WHEN statements.
Solution:
DCL-S grade CHAR(1);
grade = 'C';
SELECT;
WHEN grade = 'A';
DSPLY 'Excellent!';
WHEN grade = 'B';
DSPLY 'Good job!';
WHEN grade = 'C';
DSPLY 'You passed.';
OTHER;
DSPLY 'Better luck next time.';
ENDSL;Exercise 3: Looping through Numbers
Write a program that prints numbers from 1 to 10 using a FOR/ENDFOR loop.
Solution:
Summary
In this section, we covered the essential control structures in RPG, including conditional statements (IF/ELSE, SELECT/WHEN) and looping constructs (DO/ENDDO, FOR/ENDFOR, DOW/ENDDO, DOU/ENDDO). These control structures are crucial for managing the flow of your program and making it more dynamic and responsive to different conditions. Practice these concepts with the provided exercises to reinforce your understanding. In the next module, we will delve into working with data, including file handling and database access.
RPG Programming Course
Module 1: Introduction to RPG Programming
Module 2: Core Concepts
Module 3: Working with Data
Module 4: Advanced Programming Techniques
Module 5: RPG IV and Beyond
Module 6: Integrating RPG with Modern Technologies
Module 7: Real-World Applications
- Building a Simple Application
- Case Study: Inventory Management System
- Case Study: Payroll System
- Best Practices and Code Review
