Basic RPG400 Programming
Basic RPG400 Programming
RPG/400
PROGRAMMING
Indicators
Arithmetic Operations
Move Operations
Compare Operations
RPG Coding Exercise 3 :
Compare Operations
GOTO and TAG Operations
BASIC RPG400 PRO
GRAMMING
E - Extension Specification
describes tables and arrays used
L - Line Counter Specification
describes the form or page used in printed output
I - Input Specification
describes the file formats and fields of an input file
used in the program
C - Calculation Specification
describes how data is manipulated within the program
controls input and output functions
O - Output Specification
describes the records, fields and conditioning of
data output from
the program
BASIC RPG400 PRO
GRAMMING
PACKED DECIMAL
type 'P'
up to two characters on one byte
maximum length is 16 bytes giving a maximum
field size of 30
FILE
SPECIFICATIONS
HANDS-ON
EXERCISES
Hands-on
Create the following file and declare it to the FSpecs of the program source XXXRPG1;
1.
2.
3.
4.
Hands-on
2
0
0
TEXT('COURSE CODE')
COLHDG('Course Code')
TEXT('COURSE DESCRIPTION')
COLHDG('Course Description')
TEXT('COORDINATOR LAST NAME')
COLHDG('Coordinator' 'Last Name')
TEXT('COORDINATOR FIRST NAME')
COLHDG('Coordinator' 'First Name')
TEXT('COORDINATOR MIDDLE NAME')
COLHDG('Coordinator' 'Middle Name')
TEXT('COORDINATOR SEX CODE')
COLHDG('Coordinator Sex Code')
TEXT('COORDINATOR CATEGORY CODE')
COLHDG('Coordinator' 'Category')
TEXT('SALARY')
COLHDG('Salary')
TEXT('START DATE')
COLHDG('Date Start')
TEXT('END DATE')
COLHDG('Date End')
TEXT('USER ID')
COLHDG('User ID')
TEXT('TIME STAMP')
COLHDG('Time Stamp')
Hands-on
ANMSTD
60A
ARTEG1
3S 0
ARTEG2
3S 0
ARTEG3
3S 0
ARTEG4
3S 0
ARTEG5
3S 0
AIDUSR
ATMSTP
K ACDRS
K ANMSTD
R
R
TEXT('STUDENT NAME')
COLHDG('Student Name')
TEXT('EXAMINATION # 1 GRADE')
COLHDG('Grade for Exam # 1')
TEXT('EXAMINATION # 2 GRADE')
COLHDG('Grade for Exam # 2')
TEXT('EXAMINATION # 3 GRADE')
COLHDG('Grade for Exam # 3')
TEXT('EXAMINATION # 4 GRADE')
COLHDG('Grade for Exam # 4')
TEXT('EXAMINATION # 5 GRADE')
COLHDG('Grade for Exam # 5')
TEXT('USER ID')
TEXT('TIME STAMP')
Hands-on
Create Printer File XXXRPG1P. Use DATE Function for the date, PAGNBR function for the page and
TIME function for the time. Be sure to use EDTCDE for the page no. and EDTCDE(L) for all the rates in
the report.
Page Size:
Length lines per page> 66
Width positions per line.> 132
Characters per inch> 10
Overflow line number.> 60
Form Type...> *STD
Hold spooled file.> *YES
Save spooled file ...> *YES
For page Heading and Column Heading, use the following:
Record Format Name = HEADING
Hands-on
Hands-on
*
A
*
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
Hands-on
Source Member: XXXRPG1P
R HEADING
SKIPB(001)
HIGHLIGHT
SPACEA(001)
1
'Program ID'
13
'XXXRPG1'
47
'IBM Solutions Delivery'
119
'Date'
125
DATE
EDTCDE(Y)
1
PAGNBR
SPACEB(001)
Hands-on
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
7
'Page:'
EDTCDE(3)
47
'S T U D E N T'
63
'G R A D E'
75
'R E P O R T'
119
'Time'
125
TIME
70
'<------Examination Grade------>
SPACEB(004)
106
'Final'
114
'Highest'
Hands-on
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
123
'Lowest'
29
'Student Name'
SPACEB(001)
123
'Grade'
71
'#1'
78
'#2'
85
'#3'
92
'#4'
79
'#5'
105
'Average'
Hands-on
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
'Grade'
5
'============================================================'
SPACEB(001)
70
'==='
+4
'==='
+4
'==='
+4
'==='
+4
'==='
+5
'===='
+5
'==='
+5
'==='
Hands-on
A
R DETAIL
*
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
SPACEB(001)
SPACEA(001)
1
'Course'
HIGHLIGHT
#CDCRS
#DSCRS
#NMSTD
6A
40A
60A
O
O
O
9
16
5
SPACEB(001)
#RTEG1
3S 0O
70
EDTCDE(L)
#RTEG2
3S 0O
77
EDTCDE(L)
#RTEG3
3S 0O
84
EDTCDE(L)
#RTEG4
3S 0O
91
EDTCDE(L)
#RTEG5
3S 0O
98
EDTCDE(L)
Hands-on
A
A
A
A
A
A
#RTFAV
4S 0O
106
EDTCDE(L)
#RTHGR
3S 0O
116
EDTCDE(L)
#RTLGR
3S 0O
124
EDTCDE(L)
*
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
R SUMARRY
SPACEB(001)
HIGHLIGHT
70
'===='
77
'===='
84
'===='
91
'===='
98
'===='
106
'====='
Hands-on
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
47
'Examination Averages:'
SPACEB(002)
RTEA1
4S 1O
+2
EDTCDE(L)
RTEA2
4S 1O
+1
EDTCDE(L)
RTEA3
4S 1O
+1
EDTCDE(L)
RTEA4
4S 1O
+1
EDTCDE(L)
RTEA5
4S 1O
+1
EDTCDE(L)
RTEAF
5S 2O
+2
EDTCDE(L)
************************ End of Source ************************************
EXTENSION
SPECIFICATIONS
INPUT
SPECIFICATIONS
CALCULATION
SPECIFICATIONS
Operations
Tests
Comments
Position 6
All calculation instructions must have a C in column 6.
Conditions (Positions 7-17)
Conditioning indicator/s are specified (only when
needed in this logical area).
BASIC RPG400 PRO
GRAMMING
Entry
C
Explanation
Identification for a calculation specification
7-8
Blank
L0
L1-L9
LR
AN/OR
Control Level
Position Name
9-17
Indicators
Entry
Blank
Explanation
The operation is performed if the condition in
position 7 & 8 is met.
The following indicators are valid:
01-99
General indicators
KA-KN &
KP-KY
L1-L9
U1-U8
Factor 1
Symbolic
name or
literal
Position Name
28-32
Operation
Entry
Operation
code
Explanation
The operation to be performed.
33-42
Factor 2
Symbolic
name or
literal
43-48
Result Field
Field name
49-51
Field length
Blank
1-30
1-256
52
Position Name
53
Half adjust
54-59
Result indicators
Entry
Blank
Explanation
Half adjust is not performed.
Blank
No resulting indicators
01-99
General indicators
KA-KN &
KP-KY
L1-L9
LR
60-80
Comments
Comments
75-80
Amendment
number
Amendment
number
Amendment number
(MKI Standard Only!)
maximum BASIC
of seven
RPG400
(7) PRO
And/Or lines per group
GRAMMING
9-17
Opt.
Opt.
Result
Factor 1
Blank
Factor 2
Blank
Field
Blank
Result Indicators
54-55
56-57
58-59
Result
7-8
9-17
Factor 1
Factor 2
Opt.
Opt.
Blank
Blank
Field
Blank
Result Indicators
54-55
56-57
58-59
Add
SUB
Subtract
MULT
Multiply
DIV
Divide
MVR
Move Remainder
Z-ADD
Z-SUB
Result
Field
7-8
9-17
Factor 1
Opt.
Opt.
Factor 2
Result Indicators
54-55
56-57
+ve
-ve
58-59
Zero
Result
Field
7-8
9-17
Factor 1
Opt.
Opt.
Factor 2
Result Indicators
54-55
56-57
+ve
-ve
58-59
Zero
Result
7-8
9-17
Factor 1
Opt.
Opt.
Blank
Factor 2
Field
Required Require
d
Result Indicators
54-55
56-57
+ve
-ve
58-59
Zero
Result
7-8
9-17
Factor 1
Opt.
Opt.
Blank
Factor 2
Field
Required Require
d
Result Indicators
54-55
56-57
+ve
-ve
58-59
Zero
Result
Field
7-8
9-17
Factor 1
Opt.
Opt.
Factor 2
Result Indicators
54-55
56-57
+ve
-ve
58-59
Zero
Result
Field
7-8
9-17
Factor 1
Opt.
Opt.
Factor 2
Result Indicators
54-55
56-57
+ve
-ve
58-59
Zero
Result
7-8
9-17
Factor 1
Factor 2
Opt.
Opt.
Blank
Blank
Field
Require
d
Result Indicators
54-55
56-57
+ve
-ve
58-59
Zero
1
2
3
4
5
6
7
8
9
10
11
12
13
14
678901234567890123456789012345678901234567890123456789012345678901234
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComment++++++++
C
ADD 1
A
30
C
B
ADD C
V
52
C
B
ADD D
V
C*
C
Z-ADDC
V
C*
C
SUB 1
E
30
C
C
SUB B
W
51
C
C
SUB D
W
C*
C
Z-SUBC
W
C*
C
MULT E
F
30
C
B
MULT G
X
84
C
B
MULT D
X
C*
C
DIV B
H
30
C*
C
C
DIV J
Y
62
C*
C
MVR
Z
53
Result
7-8
9-17
Factor 1
Factor 2
Opt.
Opt.
Blank
Required
Field
Required
Result Indicators
54-55
56-57
58-59
+ve
-ve
Zero/Blank
Character
Result Field
P H 4 S N
P H 4 S N
Before MOVE
After MOVE
+
1 2 3 4 5 6 7 8 4
1 2 3 4 P H 4 S N
Character
Before MOVE
+
1 2 3 4 5 6 7 8 _4
Numeric
1 2 3 4 5 6 7 8 9
Numeric
After
Numeric
1 2 7 8 4 2 5
Before MOVE
After
Numeric
1 2 7 84 2 5
MOVE
MOVE
Before MOVE
After
MOVE
Note : 4 = letter D, and 5 = letter N
1 2 3 4 7 8 4 2 5
1 2 1 2 7 8 4 2 5
A C F G P H 4 S N Character
A C 1 2 7 8 4 2 5
Result Field
A C FG PH 4 S N
A C F G PH 4S N
Before MOVE
After MOVE
5 6 7 8 4
PH 4SN
+
Character
Before MOVE
After MOVE
5 6 7 84
7 8 42 5
Numeric
Numeric
12 78 42 5
Before MOVE
After MOVE
5 67 8 4
7 8 425
Numeric
Numeric
12 78 42 5
Before MOVE
After MOVE
PH 4SN
78 42 5
Character
Result Field
Character
PH4 SN
Before MOVE
After MOVE
5 6 784
P H4 SN
Character
Character
P H4 SN
Before MOVE
After MOVE
5 6 784
7 8 425
Numeric
Numeric
7 8 4 2 5
Before MOVE
After MOVE
A L T5F
_
7 8 4 25
Numeric
Numeric
7 8 4 2 5
Before MOVE
After MOVE
AL T 5 F
78 42 N
Character
Result
7-8
9-17
Factor 1
Factor 2
Opt.
Opt.
Blank
Required
Field
Required
Result Indicators
54-55
56-57
58-59
+ve
ve
Zero/Blank
Result Field
Before MOVEL B R W C X H 4 S A
After
Character
Numeric
C P T 5 N
7 8 4 25
7 8 4 25
C PT 5 N H 4SA
+
Before MOVEL
1 3 0 9 4 32 1 0
After
3 7 3 5 5 32 1 0
MOVEL
Before MOVEL
After
Numeric
MOVEL
+
+
1 30 9 4 3 2 1 0
MOVEL
7 8 4 2 5 3 2 1 0
Before MOVEL
BRW C XH4SA
After
MOVEL
Character
7 8 4 2 NH 4S A
is decimal point
Numeric
Numeric
Character
Character
Result Field
BRW C XH4SN
BRW C XH4SN
Before MOVEL
A K T 4D
After
BRW CX
MOVEL
Before MOVEL
+
5 6 7 8 4
Character
Numeric
After
Numeric
Numeric
00 02 5 842 5
90 31 784 2 5
MOVEL
2 9 6 3 7
Before MOVEL
+
5 6 7 8 4
After
MOVEL
0 0 0 2 5
Before MOVEL
A KT 4 D
After
9 0 3 1 7
MOVEL
Numeric
Character
Result Field
Before MOVEL A K T 4 N
After
Character P H 4 S N
Numeric
Numeric
7 8 425
7 8 425
MOVEL
PH 4 S N
Before MOVEL
5 6 7 8 4
After
7 8 4 2 5
MOVEL
Before MOVEL
5 6 7 8 4
After
MOVEL
78 4 2 5
Before MOVEL
A K T 4 D
After
7 8 4 2 N
MOVEL
Character
Numeric
Numeric
Character
Compare
CABxx
TAG
Tags
GOTO
Go to a Tag
9-17
Opt.
Opt.
Result
Factor 1
Factor 2
Required Required
Field
Blank
Result Indicators
54-55
56-57
GT
LT
58-59
EQ
1
2
3
4
FLDE = ABCDE
BASIC RPG400 PRO
GRAMMING
FLDC = 100.00
9-17
Opt.
Opt.
Result
Factor 1
Factor 2
Field
Result Indicators
54-55
56-57
GT
LT
58-59
EQ
C*
CFLDACABLEFLDBTAGX16
2
C*
3 CFLDACABFLDBTAGX1718
C*
CFLDACABEQFLDBTAGX20
4
C*
FLDB = 105.00
BASIC RPG400 PRO
GRAMMING
9-17
Opt.
Opt.
Result
Factor 1
Factor 2
Blank
Required
Field
Blank
Result Indicators
54-55
56-57
Blank
Blank
58-59
Blank
9-17
Result
Factor 1
Blank Required
Factor 2
Blank
Field
Blank
Result Indicators
54-55
56-57
Blank
Blank
58-59
Blank
HANDS-ON
EXERCISES
Hands-on
Use RPGXXX1 and files previously created in this exercise. Populate first
the physical files of sample data.
1.
2.
3.
Print the page heading and the column heading on every page.
Read all records of XXX02K. For each record read, determine the
course description from the corresponding XXX01K0 record, the
final average of the 5 examination grades {(ARTEG1 + ARTEG2 +
ARTEG3 + ARTEG4 + ARTEG5) / 5}, the highest grade and the
lowest grade, then print the detail line.
Determine the total number of XXX02K records and the
summations of examination grades for #s 1, 2, 3, 4 & 5.
If the end of XXX02K file is reached, determine examination grade
averages (Summation of Examination Grades/ Total No. of XXX02K
records) and the final average of the five examination averages,
then print the summary.
Hands-on
Source Member: XXXRPG1
678901234567890123456789012345678901234567890123456789012345678901234
/TITLE RPG EXERCISE # 1
*
** File Declaration
*
FXXX02K IF E
K
DISK
FXXX01K0 IF E
K
DISK
FXXXRPG1PO E
01
PRINTER
*
E
EGR
5 3 0A
E
EAV
5 10 1A
*
/EJECT
********************************************************************
* Main Program
********************************************************************
C
EXSR PRCRTN
C
EXSR ENDRTN
********************************************************************
/SPACE 5
Hands-on
678901234567890123456789012345678901234567890123456789012345678901234
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComment++++++++
********************************************************************
* Initialization To initialize all program variables used
********************************************************************
C
*INZR
BEGSR
C
Z-ADD*ZEROS
VNOREC 60
C
Z-ADD*ZEROS
VACEGR 40
C
Z-ADD*ZEROS
VACEAV 51
C
CLEAREAV
C
ENDSR
********************************************************************
* Process all records of Student Grade File
********************************************************************
C
PRCRTN
BEGSR
C
EXSR HDGRTN
C
*LOVAL
SETLLXXX02K
C
READ XXX02K
60
C
*IN60
DOWEQ*OFF
C
EXSR DTLRTN
C
READ XXX02K
60
C
ENDDO
C
ENDSR
********************************************************************
Hands-on
678901234567890123456789012345678901234567890123456789012345678901234
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComment++++++++
********************************************************************
* Printing of Page Heading and Column Heading
********************************************************************
C
HDGRTN
BEGSR
C
WRITEHEADING
C
MOVE 0
*IN01
C
ENDSR
********************************************************************
* Printing of detail Line
********************************************************************
C
DTLPRN
BEGSR
C
ADD 1
VNOREC
C
ADD ARTEG1
EAV,1
C
ADD ARTEG2
EAV,2
C
ADD ARTEG3
EAV,3
C
ADD ARTEG4
EAV,4
C
ADD ARTEG5
EAV,5
C
MOVELACDCRS
#CDCRS
C
#CDCRS
CHAINXXX01K0
61
C
*IN61
IFEQ *OFF
Hands-on
678901234567890123456789012345678901234567890123456789012345678901234
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComment++++++++
C
MOVELADSCRS
#DSCRS
C
ELSE
C
MOVE *BLANKS
#DSCRS
C
ENDIF
C
MOVELANMSTD
#NMSTD
C
Z-ADDARTEG1
EGR,1
C
Z-ADDARTEG2
EGR,2
C
Z-ADDARTEG3
EGR,3
C
Z-ADDARTEG4
EGR,4
C
Z-ADDARTEG5
EGR,5
C
Z-ADDEGR,1
#RTEG1
C
Z-ADDEGR,2
#RTEG2
C
Z-ADDEGR,3
#RTEG3
C
Z-ADDEGR,4
#RTEG4
C
Z-ADDEGR,5
#RTEG5
C
XFOOTEGR
VACEGR
C
VACEGR
DIV 5
#RTFAV
H
C
SORTAEGR
C
Z-ADDEGR,5
#RTHGR
C
Z-ADDEGR,1
#RTLGR
Hands-on
678901234567890123456789012345678901234567890123456789012345678901234
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComment++++++++
C
*IN01
IFEQ 1
C
EXSR HDGRTN
C
ENDIF
C
WRITEDETAIL
C
ENDSR
********************************************************************
* End program
********************************************************************
C
ENDRTN
BEGSR
C
EXSR SMRRTN
C
SETON
LR
C
RETRN
C
ENDSR
********************************************************************
* Printing of Summary
********************************************************************
C
SMRRTN
BEGSR
C
VNOREC
IFNE *ZEROS
C
DIV VNOREC
EAV,1
H
C
DIV VNOREC
EAV,2
H
Hands-on
678901234567890123456789012345678901234567890123456789012345678901234
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComment++++++++
C
DIV VNOREC
EAV,3
H
C
DIV VNOREC
EAV,4
H
C
DIV VNOREC
EAV,5
H
C
ENDIF
C
Z-ADDEAV,1
#RTEA1
C
Z-ADDEAV,2
#RTEA2
C
Z-ADDEAV,3
#RTEA3
C
Z-ADDEAV,4
#RTEA4
C
Z-ADDEAV,5
#RTEA5
C
XFOOTEAV
VACEAV
C
VACEAV
DIV 5
#RTEAF
H
C
WRITESUMMARY
C
ENDSR
*******************************************************************
OUTPUT
SPECIFICATIONS