Professional Documents
Culture Documents
Module Overview
This module on Modular Programming Constructs in RPG 400, has been to give you a basic understanding
of structured design.
After completing this module on Modular Programming Constructs in RPG 400 you should be able to:
Statement
False True
Condition ?
Statement
Condition ? Statement
Sequence
True
False Selection
Iteration
RPG 400 programming
Modular Programming Constructs in RPG Relational Comparisons
3
Relational Symbols and Codes
TABLE
› GT Greater than
‹ LT Less than
You have learned basic logic structures and relational comparison, now evaluate yourself by attending to
the exercise below. Select the correct option(s) and click Submit.
Complex IF
Usage of IF statement
statement
Simple IF
statement
An IFxx statement that includes only one
test is simple.
Simple IF Opcode
Usage of IF statement
Complex IF
statement
Simple IF Opcode
IF statement diagram.
General format of
IF statement
Simple IF Opcode
AGE IFLE (‹ =) 4
ADD 1 ChildCnt
General format of
IF statement
IF statement diagram.
Example Complex IF Opcode
Simple IF
statement
Simple IF Opcode
*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
CL0N01Factor1+++++++Opc.ode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
C
C FLDA IFEQ FLDB
C FLDA ANDGT FLDC
C FLDD OREQ FLDE
C FLDD ANDGT FLDF
C : An IFxx statement that includes more
than one test connected with an AND
C : or OR relationship is complex
C :
C ENDIF
RPG 400 programming
Modular Programming Constructs in RPG Primary decision operator : IF
6a4
Primary decision operator : IF Simple IF Opcode IF statement
General format of
IF statement
Complex IF
Usage of IF statement
Simple IF Opcode
Calculations to be
It includes only one
performed
Test, so the IFxx
between IFxx statement
Statement is simple
and its End IF
RPG 400 programming
Modular Programming Constructs in RPG Decision (or Selection) Operations
6b
IF…ELSE structure
The IFxx statement is used to evaluate a If the evaluation in the IFxx statement is
condition, and if the test is true, the IF false, control passes to the ELSE
block operation is processed and control statement and the evaluation
passes to the operation immediately immediately following is processed.
following the ENDIF statement.
RPG 400 programming
Modular Programming Constructs in RPG QUIZ
6c
Exercise on IF and IF…ELSE structures
Drag the boxes containing answers and drop them at the correct blank spaces on the green screen:
You are asked to calculate pay and wanted to pay time-and-a-half for any hours over 40, you could code.
*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
Given Value
CL0N01Factor1+++++++Opc.ode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
C HOURS IF EQ
1 40 A=HOURS-40
C HOURS MULT PAYRATE TOTALPAY B=PAYRATE*1.5
C 5
ELSE C=PAYRATE(A)
C 2
HOURS SUB 40 A
D=C*B
C 3
PAYRATE MULT 1.5 B TOTALPAY = 40*D
C 4
PAYRATE MULT A C
C C MULT B D
Logical operators
Programmers sometimes want are used to
to execute a series of When an AND relationship combine the
instructions based on multiple is included, all the conditions, when
conditions. RPG includes logical conditions should be true to several conditions
operators to allow such multiple execute the action need to be
conditions. checked.
ANDxx Opcodes
Logical operators
Drag the boxes containing answers and drop them at the correct blank spaces on the green screen:
*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
Submit
CL0N01Factor1+++++++Opc.ode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
C* If ACODE is equal to A and indicator 50 is on, the MOVE ELSE
C* and WRITE operations are processed.
C ACODE IFEQ 1'A' ANDEQ*ON
C *IN50 2
ANDEQ*ON
C MOVE 'A' ACREC
C WRITERCRSN
ANDEQ*OFF
C* If the previous conditions were not met but ACODE is equal
C* to A, indicator 50 is off, and ACREC is equal to D, the
C* following MOVE operation is processed.
ANDEQ'D'
C 3
ELSE
C ACODE IFEQ 'A'
C *IN50 4
ANDEQ*OFF
C ACREC 5
ANDEQ'D' IFEQ 'A'
C MOVE 'A' ACREC
C 6
ENDIF
Try Again
C ENDIF
RPG 400 programming
Modular Programming Constructs in RPG Decision (or Selection) Operations
6g
Case Logic
Within a SELEC group, control passes to After the instructions for the
One or more
the first instruction following the WHxx WHxx statement are executed
WHxx statements
statement tested as true. control branches to the
instruction following the ENDSL
SELEC group statement.
WHxx statements
OTHER statement
Factor 1, Factor 2 and Result
WHxx statements field are not used with the
SELEC statement
RPG 400 programming
Modular Programming Constructs in RPG Nested IFxx vs. SELEC operations
6h3
Comparison
Click on the screen shot to zoom out to display a larger area of the image.
RPG 400 programming
Modular Programming Constructs in RPG Exercise
7
Opcode on case logic and SELEC operations
Drag the boxes containing answers and drop them at the correct blank spaces on the green screen:
*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
CL0N01Factor1+++++++Opc.ode(E)Factor2+++ResultLenDHHiLoEqComments++++++ You have previously
C* seen codes using
Nested IF. Here, the
C 1
SELEC code on the left side
C X WHEQ uses SELEC to
2 1
express that if X
C Z-ADDA B equals 1, do the
C MOVE C D operations in
sequence 1; if X
C : seq 1 does not equal 1,
C Y 3 2
WHEQ and if Y=2 and
X<10, do the
C X ANDLT10 operations in
C : seq 2 sequence 2.
C OTHER
4
C : seq 3
Submit
C ENDSL
C*
Try again
OTHER WHEQ 2 ANDEQ*ON WHEQ 1 WHEQ 3
RPG 400 programming
Modular Programming Constructs in RPG Looping Operations
7
Operations for Iteration
Iteration Operations
The DOUxx, DOUxx, and DOxx operation is a structured instruction that controls
iterative processing.
RPG 400 programming
Modular Programming Constructs in RPG Operations for Iteration
7a
DOWxx (Do while) operation
A programmer uses
The statements between the DOW and DOW (DO…WHILE)
ENDDO would be repeated as long as for different kind of
the condition specified in the DOW repeat processing
statement is true. When the condition operation.
becomes false, control is transferred to DOWxx
the statement following the ENDDO
statement. <….>
ENDDO
DO…WHILE Structure
DOWxx
Like RPG decision
operations, the
DOW operation
<….>
lets a
programmer use xx operation tests that may be
AND or OR to specified are LT, GT, LE, GE, EQ or
form compound ENDDO NE.
condition to
control the
looping. Example
RPG 400 programming
Modular Programming Constructs in RPG DOWxx (Do while) operation
7a1
DOWxx operation: Example
DO…UNTIL Structure
DOUxx
Like RPG decision
operations, the
DOU operation
<….>
lets a
programmer use xx operation tests that may be
AND or OR to specified are LT, GT, LE, GE, EQ or
form compound ENDDO NE.
condition to
control the
looping. Example
RPG 400 programming
Modular Programming Constructs in RPG DOUxx (Do Until) operation
7b
DOUxx operation: Example
Statement (s)
Condition ? Statement (s)
True
False
False
Condition ?
True
The starting value of The limiting value of the A field to RPG gives the programmer the
the counter, the counter, for looping to serve as the option to specify the four values
default value is one.
continue. counter or omit any of them.
For
programmers
General layout of a DO loop DOxx operation
are designed for
count-controlled
CLONO1Factor1++++Opcode (E)+Factor2++++++Result loop.
Write parts of a programs to generate the operation provided below. Self check your program by clicking
the answer button. The operation given repeat a series of instructions in a program.
Employees in your Bank wants you to write a program where they would be able to add
all the numbers between 1 and 100. with a counter field, Number, and an accumulator,
Sum. Use DOWxx Opcode to accomplish the summation.
*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
CL0N01Factor1+++++++Opc.ode(E)Factor2+++ResultLenDHHiLoEqComments++++++
C
C
C
C
C
C
C
C
C
C
C
C Answer
RPG 400 programming
Modular Programming Constructs in RPG Looping Operations
8
Loops and Early Exits
It helps
programmers skip
LEAVE operation ITER operation
the remaining
instructions with a
loop or exit a
complete loop.
When a LEAVE The ITER statement passes
statement is control to the related ENDDO
executed, control is statement and causes the next
transferred to the iteration of the loop to be
statement following executed.
the related ENDDO
statement.
ITER = skip
the remaining Instructions
instructions in with the loop
The LEAVE (Leave a
the loop
DO Group) and ITER
(Iterate) operations
support additional
control within a DO, LEAVE = leave
DOUxx or DOWxx the looping
group. process Example
completely
RPG 400 programming
Modular Programming Constructs in RPG Looping Operations
8a
Opcode on LEAVE And ITER
RPG 400 programming
Modular Programming Constructs in RPG Exercise
8b
Opcode on LEAVE and ITER
Drag the blue boxes containing answers and drop them at the correct blank spaces on the green screen:
*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
CL0N01Factor1+++++++Opc.ode(E)+Factor2+++ResultLenDHHiLoEqComments++++++ Explanation
C
Here, the Opcode uses a
C DOU
1 FLDA = FLDB ENDDO DOU loop containing a
DOW loop. The IF
C :
statement checks indicator
C NUM DOWLT
2 10 ITER 01. If indicator 01 is ON, a
certain operation is
C IF *IN01
executed, transferring
C LEAVE
3 IFEQ control out of the
innermost DOW loop to the
C ENDIF
4 Z-ADD instruction. If
C EXSR PROC1 DOWLT indicator 01 is not ON,
subroutine PROC1 is
C *IN12 IFEQ
5 *OFF
processed. Then indicator
C ITER
6 DOU 12 is checked. If it is OFF,
operation transfers control
C ENDIF
to the innermost ENDDO
C EXSR PROC2 ENDIF and the condition on the
DOW is evaluated again. If
C ENDDO
7 indicator 12 is ON, another
C Z-ADD 20 RSLT 20 operation PROC2 is
LEAVE processed.
C :
C ENDDO
C : Submit
Try again
RPG 400 programming
Modular Programming Constructs in RPG Branching Operations
9
Branching Operation: Introduction
Branching
Operation
Traditional Syntax
Tag
TAG
For a
The GOTO statement transfers control to the line of code with
programmer
the corresponding TAG name specified
the GOTO
operation
diverts control
*...1.....+.....2.....+.....3.....+.....4.....+.....5.....+....6....+....7...+.... to a different
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq location within
a program.
C GOTO Label
C .......
C TAG
C ...... A non-
executable
C GOTO END
operation
C END TAG
Factor 2 must
contain a field
*...1.....+.....2.....+.....3.....+.....4.....+.....5.....+....6....+....7...+.... name or literal,
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq the same type as
C A CABxx B SKIP Factor 1.
C .......
A TAG operation
C SKIP TAG name must be in
A TAG operation names the
C ...... destination of a CABxx the Result field
C LOOP TAG operation. The TAG can either
precede or follow the CABxx.
Factor 1 must contain a If the condition returns true, control will be transferred
field name or literal. to the TAG statement mentioned on that line of code. Example
RPG 400 programming
Modular Programming Constructs in RPG Unstructured – Compare and Branch statement
Fill in the blanks with appropriate phrases. Write your answers in the blanks provided.
Try again
Submit
RPG 400 programming
Modular Programming Constructs in RPG Subroutines
10
Defining Subroutines
Example
BEGSR ENDSR EXSR
BEGSR includes
the name of the
subroutine SINIT
RPG 400 programming
Modular Programming Constructs in RPG Subroutines
10b
EXSR Opcode: Example
S
U
B
R
O
U
T
I
N
E
S
RPG 400 programming
Modular Programming Constructs in RPG Subroutines
10c
INZSR Subroutine
*INZSR is the initialization subroutine used to initialize variables at the beginning of the program.
*...1.....+.....2.....+.....3.....+.....4.....+.....5.....+....6....+....7...+....
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq
C *INZSR BEGSR
C -------
If you include INZSR within your
C ------- program, the subroutine is invoked
automatically after the program
C -------
completes its other tasks.
C ENDSR
This Subroutine is defined by specifying *INZSR in Factor 1 of the BEGSR operation in a C-Spec operation
Click on the button to view a sample *INZSR Opcode with a program Sample
RPG 400 programming
Modular Programming Constructs in RPG INZSR Subroutine
10c1
INZSR Subroutine: Sample
RPG 400 programming
Modular Programming Constructs in RPG Subroutines
10d
CASxx Operation
The CASxx opcode should be used, when a relational test CASxx also lets a
is required before exiting to an internal subroutine. programmer
specify a
subroutine to be
General layout of a CASxx Opcode
performed if none
*...1.....+......2......+......3......+.....4......+......5......+.....6.....+.....7...+.... of the tested
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq
condition prove
to be true. For
C FieldA CASGE FieldB Subr01 this option a
C FieldA CASEQ FieldC Subr02 CASxx is used
A required ENDCS without a
C CAS Subr03 indicates the end of a relational code,
single CASxx as the final
C ENDCS statement or group. operation within
the structure.
Both CAS and CAB statement branch to different parts of the program.
Example
RPG 400 programming
Modular Programming Constructs in RPG Subroutines
10d1
CASxx Opcode: Example
Your banking company wants you to write a payroll program. Each record in the input file,
MPR001 represents one day’s work for an employee. Records are accumulated doe a week,
Scenario
so there will be several records per employee. Records in the file are ordered by the Social
Security number.
The company uses both an hourly rate and a piece rate to pay its employees. Everyone
gets Rs. 50 per hour worked. Additionally is a person works for one hour extra during the
Addi week, (s) he receives Rs. 50 per hour; if (s) he works for 3 hours extra, (s) he receives Rs.
tion 100 per hour; if over 5hrs extra, he receives Rs. 500 per hour. After using these figures to
al calculate gross pay, a 4.6 percent profession tax and a 15 percent income tax must be
Inpu subtracted from the gross pay to obtain net pay.
t
Click next to view the MPR001: Work file and the desired report after the
program is run.
RPG 400 programming
Modular Programming Constructs in RPG Self-Evaluation
11a
Supplied Material: I
Programming
Scenario Additional Input
assignment
*…...1…........+.......2…...+......3.........+……....4.......+……..5…….+……..6…......+….....7......+......
CL0N01Factor1+++++++Opc.ode(E)+Factor2+++ResultLenDHHiLoEqComments++++++
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
Programming
Scenario Additional Input SUBMIT ANSWER
assignment
RPG 400 programming
Modular Programming Constructs in RPG End of the lesson
12
Summary
After completing this lesson on Modular Programming Constructs in RPG/400 have learned the
following leaning points and should be able to apply them in program design. This would help in
better maintenance and qualify of the program designed for the organization you work with.