You are on page 1of 29

Presentation Copyright 2002, Bryan Meyers

Top-Down, Structured
Program Design
Chapter 5
Programming in RPG IV
Third Edition
2
Objectives
Determine how to design and write
structured RPG programs
Use structured RPG operations that
perform sequence, selection, and
iteration
Use subroutines
Design a control break program
Programming in RPG IV
Third Edition
3
Structured Design
A methodology that limits its control to 3
basic structures
Sequence
Selection (decision)
Iteration (repetition, looping)
Each of these control structures has a
single entry point and a single exit point
Programming in RPG IV
Third Edition
4
Sequence Control Structure
Statement
Statement
Statement
Programming in RPG IV
Third Edition
5
Selection Control Structure
Condition?
Statement(s) Statement(s)
True False
Programming in RPG IV
Third Edition
6
Iteration Control Structure
Condition? Statement(s)
True
False
Programming in RPG IV
Third Edition
7
Relational Codes
Symbol Code Meaning
> GT Greater than
< LT Less than
= EQ Equal to
<> NE Not equal to
<= LE Less than or equal to
>= GE Greater than or equal to
Programming in RPG IV
Third Edition
8
Collating sequence
Character by character comparison
EBCDIC
Digits are compared based on algebraic
value
Letters are smaller than digits
Lowercase letters are smaller then
uppercase letters
A blank is smaller than any other
displayable character
Programming in RPG IV
Third Edition
9
Selection Operations
IF Conditional expression
ELSE
ENDIF
AND/OR
Nested IFs
ELSEIF
SELECT
WHEN
ENDSL
Programming in RPG IV
Third Edition
10
IF / ENDIF
If the comparison is true all statements
between the IF and ENDIF are performed
If the comparison is false the statements
are bypassed
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
IF Age >= 65;
SeniorCnt = SeniorCnt + 1;
ENDIF;
/END-FREE
Programming in RPG IV
Third Edition
11
IF / ELSE / ENDIF
If the comparison is true all statements
between the IF and ELSE are performed
If the comparison is false all the
statements between the ELSE and the
ENDIF are performed
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
IF Hours <= 40;
EVAL(H) TotalPay = Hours * PayRate;
ELSE;
EVAL(H) TotalPay = 40 * PayRate + (Hours - 40) * PayRate * 1.5;
ENDIF;
/END-FREE
Programming in RPG IV
Third Edition
12
AND / OR
AND: Both relationships must be true
OR: One or the other or both relationships must
be true
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
IF Age >= 65 AND Status = 'R';
// This block of code is executed only if Age >=65 and Status =R.
ELSE;
// If one or both conditions are not met,this code is executed.
ENDIF;

IF Age >= 65 OR Status = 'R';
// This block is executed if one or both conditions are true.
ELSE;
// This block is executed only if both conditions are false.
ENDIF;
/END-FREE
Programming in RPG IV
Third Edition
13
Nested IFs
Build IFs within IFs
Each IF requires an ENDIF
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
IF Sales <=5000;
Rate =.005;
ELSE;
IF Sales <=10000;
Rate =.0075;
ELSE;
IF Sales <=20000;
Rate =.01;
ELSE;
Rate =.015;
ENDIF;
ENDIF;
ENDIF;
/END-FREE
Programming in RPG IV
Third Edition
14
Nested IFs
ELSEIF combines function of ELSE and
IF statement
Single ENDIF ends IF/ELSEIF blocks
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
IF Sales <=5000;
Rate =.005;
ELSEIF Sales <=10000;
Rate =.0075;
ELSEIF Sales <=20000;
Rate =.01;
ELSE;
Rate =.015;
ENDIF;
/END-FREE
Programming in RPG IV
Third Edition
15
SELECT
Appears on a line alone to identify the start of a case
construct
SELECT is followed by one or more WHENs
Each WHEN specifies a condition to be tested
Each WHEN is followed by one or more statements to be
performed
As soon as it encounters a true condition, the computer
executes the operation(s) following a WHEN
Then control falls to ENDSL
WHEN conditions can be coupled with AND/OR
operations
OTHER means in all other cases
OTHER should be the final catch all
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
SELECT;
WHEN Sales <=5000;
Rate =.005;
WHEN Sales <=10000;
Rate =.0075;
WHEN Sales <=20000;
Rate =.01;
OTHER;
Rate =.015;
ENDSL;
/END-FREE
Programming in RPG IV
Third Edition
16
IF and Page Overflow
Built in overflow indicators
OA thru OG and OV
Use OFLIND File Specification keyword
to associate indicator with report file
Indicator will automatically come *ON
when printer reaches overflow line at
bottom of page
Programming in RPG IV
Third Edition
17
Iteration Operations
DOW Conditional expression - ENDDO
DOU Conditional expression - ENDDO
FOR - ENDFOR
Programming in RPG IV
Third Edition
18
Do While Loop
Leading decision loop
Condition?
Statement(s)
Stop
Start
False
True
Programming in RPG IV
Third Edition
19
DOW
Establishes a leading decision loop, based on a
comparison
Allows AND and OR to form compound conditions
Operations between DOW and ENDDO repeat
as long as condition remains true
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
Number = 0; // Initialize Number to zero.
Sum = 0; // Initialize Sum to zero.
DOW Number < 100; // Loop while Number is less than 100.
Number = Number + 1; // Increment Number by 1.
Sum = Sum + Number; // Add Number to accumulator Sum.
ENDDO;
/END-FREE
Programming in RPG IV
Third Edition
20
Do Until Loop
Trailing decision loop
Condition?
Statement(s)
Stop
Start
False
True
Programming in RPG IV
Third Edition
21
DOU
Establishes a trailing decision loop, based on a
comparison
DOU repeats until the condition becomes true
Statements always execute at least once
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
Number = 0; // Initialize Number to zero.
Sum = 0; // Initialize Sum to zero.
DOU Number = 100; // Loop until Number equals 100.
Number = Number + 1; // Increment Number by 1.
Sum = Sum + Number; // Add Number to accumulator Sum.
ENDDO;
/END-FREE
Programming in RPG IV
Third Edition
22
FOR
Initiates a count-controlled loop
Specify four things
Counter
Counter starting value
Optional, defaults to 1
Counter limit value (TO or DOWNTO)
Optional, defaults to 1
Counter increment value (BY)
Optional, defaults to 1
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
Sum = 0;
FOR Number = 1 TO 100;
Sum = Sum + Number;
ENDFOR;
/END-FREE
Programming in RPG IV
Third Edition
23
Early Exits from Loops
ITER skips the remaining instructions in the
loop and causes the next repetition to begin
LEAVE terminates the loop
Sends control to statements following ENDDO
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
DOW NOT %EOF;
READ CustFile;
IF %EOF;
LEAVE;
ELSE;
EXCEPT Detail;
ITER;
ENDIF;
ENDDO;
/END-FREE
Programming in RPG IV
Third Edition
24
Subroutines
Subroutine: a block of code with an identifiable
beginning and end
Subroutines must have a unique name formed
with the same rules that apply to fields
Subroutines may execute other subroutines,
but a subroutine should never execute itself
Subroutines cannot contain other subroutines
Subroutines appear after other calculation
specifications
Organize subroutines alphabetically
Programming in RPG IV
Third Edition
25
BEGSR/ENDSR
BEGSR begins a subroutine
Include subroutine name
ENDSR ends a subroutine
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
BEGSR CalcTax;
EVAL(H) FICA = Gross * .0751;
EVAL(H) StateTax = Gross * .045;
IF Gross > 5000;
EVAL(H) FedTax = Gross * .31;
ELSE;
EVAL(H) FedTax = Gross * .25;
ENDIF;
ENDSR;
/END-FREE
Programming in RPG IV
Third Edition
26
EXSR
Executes subroutine named in Factor 2
*.. 1 ...+... 2 ...+... 3 ...+... 4 ...+... 5 ...+... 6 ...+... 7 ...+... 8
/FREE
EXSR CalcTax; // EXSR causes control to drop to subroutine CalcTax.
// Control returns here when the subroutine finishes.
...
BEGSR CalcTax;
EVAL(H) FICA = Gross * .0751;
EVAL(H) StateTax = Gross * .045;
IF Gross > 5000;
EVAL(H) FedTax = Gross * .31;
ELSE;
EVAL(H) FedTax = Gross * .25;
ENDIF;
ENDSR;
/END-FREE
Programming in RPG IV
Third Edition
27
Points to Remember
Structured program design means
developing program logic with flow of
control tightly managed
Generally by using only structured operations
Top-down methodology requires
hierarchical program design
Work out broad logic first
Later, attend to detailed processing
Programming in RPG IV
Third Edition
28
Points to Remember
Decision logic operations
IF / ELSE / ELSEIF
SELECT / WHEN / OTHER
Iteration (looping) operations
DOW (leading decision)
DOU (trailing decision)
FOR (counter controlled)
Use ITER and LEAVE to provide an early exit
from a loop
Most of the structured operation of RPG require
the use relational operators (>, <, =, <=, >=, <>)
Programming in RPG IV
Third Edition
29
Points to Remember
Subroutines allow an identifiable block of
code to be executed
BEGSR and ENDSR begin and end a
subroutine
Coded after other calculations
EXSR executes a subroutine

You might also like