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:

  1. Conditional Statements
    • IF/ELSE
    • SELECT/WHEN
  2. 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:

DO count;
   // Code to execute
ENDDO;

Example:

DCL-S i INT(3);

DO 5;
   DSPLY 'This is iteration number ' + %CHAR(i);
ENDDO;

FOR/ENDFOR

The FOR/ENDFOR loop is similar to the DO/ENDDO loop but provides more control over the loop variable.

Syntax:

FOR loop_variable = start_value TO end_value BY step_value;
   // Code to execute
ENDFOR;

Example:

DCL-S i INT(3);

FOR i = 1 TO 5 BY 1;
   DSPLY 'This is iteration number ' + %CHAR(i);
ENDFOR;

DOW/ENDDO

The DOW/ENDDO loop (Do While) repeats a block of code as long as a specified condition is true.

Syntax:

DOW condition;
   // Code to execute
ENDDO;

Example:

DCL-S i INT(3);
i = 1;

DOW i <= 5;
   DSPLY 'This is iteration number ' + %CHAR(i);
   i += 1;
ENDDO;

DOU/ENDDO

The DOU/ENDDO loop (Do Until) repeats a block of code until a specified condition becomes true.

Syntax:

DOU condition;
   // Code to execute
ENDDO;

Example:

DCL-S i INT(3);
i = 1;

DOU i > 5;
   DSPLY 'This is iteration number ' + %CHAR(i);
   i += 1;
ENDDO;

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:

DCL-S i INT(3);

FOR i = 1 TO 10 BY 1;
   DSPLY %CHAR(i);
ENDFOR;

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.

© Copyright 2024. All rights reserved