You are on page 1of 126

BASIC

RPG/400
PROGRAMMING

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


AGENDA
Introduction
Specification Sequence
Fields in RPG/400
File Specifications Summary
Hands-on
Calculation Specifications Summary
BASIC RPG400 PRO
GRAMMING

BASIC RPG/400 PROGRAMMING


AGENDA
(Continued)

Indicators
Arithmetic Operations
Move Operations
Compare Operations
RPG Coding Exercise 3 :
Compare Operations
GOTO and TAG Operations
BASIC RPG400 PRO
GRAMMING

BASIC RPG/400 PROGRAMMING


INTRODUCTION
most common HLL used on the AS/400
Report Program Generator
has more powerful database and device
handling facilities than CL
source code is positional
made up of seven specification types

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


SPECIFICATION SEQUENCE - DIAGRAM
H Control Specification
F File Specification
E Extension Specification
L Line Counter Specification
I Input Specification
C Calculation Specification
O Output Specification

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


SPECIFICATION SEQUENCE
H - Control Specification
controls the generation and execution of the
program
company standards dictate that a control (or header)
specification must be included in every
RPG program with a '1' in position 15
controls the production of a symbols table when the
program is compiled
allow DEBUG to perform and to show the contents of
F - File
Description
Specification
fields
when a DUMP
is produced.
describes the files to be included and how they will
be used
BASIC RPG400 PRO
GRAMMING

BASIC RPG/400 PROGRAMMING


SPECIFICATION SEQUENCE
(Continued)

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

BASIC RPG/400 PROGRAMMING


FIELDS IN RPG/400
ALPHANUMERIC
type 'A'
one character is held in one byte
maximum length is 256 bytes
ZONED DECIMAL
type 'S'
one numeric character is held in one byte
maximum length is 15 bytes
BASIC RPG400 PRO
GRAMMING

BASIC RPG/400 PROGRAMMING


FIELDS IN RPG/400
(Continued)

PACKED DECIMAL
type 'P'
up to two characters on one byte
maximum length is 16 bytes giving a maximum
field size of 30

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


DEFINING FIELDS
Fields can be defined in any one of the following ways:
from an externally described file
from a data structure
from the input specification in the case of internally
described files
within the calculation specification
within the extension specification
Note: RPG/400 assigns an area of storage to each
field.
The maximum length of a field name in
RPG/400 is six characters
BASIC RPG400 PRO
GRAMMING

BASIC RPG/400 PROGRAMMING

FILE
SPECIFICATIONS

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


F - SPECS SUMMARY

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


F - SPECS SUMMARY cont.

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


F - SPECS SUMMARY cont.

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


F - SPECS SUMMARY cont.

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


F - SPECS SUMMARY cont.

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


F - SPECS SUMMARY cont.

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


F - SPECS SUMMARY cont.

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


F - SPECS SUMMARY cont.

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING

HANDS-ON
EXERCISES

BASIC RPG400 PRO


GRAMMING

Hands-on
Create the following file and declare it to the FSpecs of the program source XXXRPG1;
1.
2.
3.
4.

XXX01REC physical file


XXX01K0 logical file
XXX02K physical file
XXXRPG1P printer file

BASIC RPG400 PRO


GRAMMING

Hands-on

Physical file XXX01REC

* This id the dds source of the


A
R XXX01REC
A
ACDCRS
6A
A
A
ADSCRS
40A
A
A
ANMLCR
20A
A
A
ANMFCR
20A
A
A
ANMMCR
20A
A
A
ACDSCR
1A
A
A
ACDCCR
1A
A
A
A
ACSSAL
15P
A
A
ADTSTR
8S
A
A
ADTEND
8S
A
A
AIDUSR
10A
A
A
ATMSTP
14S
A

physical file XXX01K

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')

BASIC RPG400 PRO


GRAMMING

Hands-on

Physical file XXX01K0

* This id the dds source of the logical file XXX01K0


A
UNIQUE
A
R XXX01REC
PFILE(XXX01K)
A
K ACDCRS

Physical file XXX01K0

* This id the dds source of the physical file XXX02K


A
UNIQUE
A
REF(XXX01K)
A
R XXX02REC
A
ACDCRS
R
6A
TEXT('COURSE CODE')
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A

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')

BASIC RPG400 PRO


GRAMMING

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

BASIC RPG400 PRO


GRAMMING

Hands-on

For Detail Line, use the following:


Record Format Name = DETAIL
Course Code Field Name = #CDCRS
Course Description Field name = #DSCRS
Student Name Field Name = #NMSTD
Examination Grade #1 Field Name = #RTEG1
Examination Grade #2 Field Name = #RTEG2
Examination Grade #3 Field Name = #RTEG3
Examination Grade #4 Field Name = #RTEG4
Examination Grade #5 Field Name = #RTEG5
Final Average Field Name = #RTFAV
Highest Grade Field Name = #RTHGR
Lowest Grade Field Name = #RTLGR

BASIC RPG400 PRO


GRAMMING

Hands-on

For Summary, use the following:


Record Format Name = SUMMARY
Examination Grade Average #1 Field Name = #RTEA1
Examination Grade Average #2 Field Name = #RTEA2
Examination Grade Average #3 Field Name = #RTEA3
Examination Grade Average #4 Field Name = #RTEA4
Examination Grade Average #5 Field Name = #RTEA5
Final Average Field Name = #RTEAF

BASIC RPG400 PRO


GRAMMING


*
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)

BASIC RPG400 PRO


GRAMMING

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'

BASIC RPG400 PRO


GRAMMING

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'

BASIC RPG400 PRO


GRAMMING

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
'==='

BASIC RPG400 PRO


GRAMMING

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)

BASIC RPG400 PRO


GRAMMING

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
'====='

BASIC RPG400 PRO


GRAMMING

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 ************************************

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING

EXTENSION
SPECIFICATIONS

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


E - SPECS SUMMARY

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


E - SPECS SUMMARY

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


E - SPECS SUMMARY

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


E - SPECS SUMMARY

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


E - SPECS SUMMARY

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING

INPUT
SPECIFICATIONS

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Program Described Files, Record Identification Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Program Described Files, Record Identification Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Program Described Files, Record Identification Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Program Described Files, Record Identification Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Program Described Files, Field Description Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Program Described Files, Field Description Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Program Described Files, Field Description Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Externally Described Files, Record Identification Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Externally Described Files, Record Identification Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Externally Described Files, Field Description Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Externally Described Files, Field Description Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Data Structure Statement Specifications

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Data Structure Statement Specifications

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Data Structure Statement Specifications

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Data Structure Subfield Specifications

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Data Structure Subfield Specifications

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Named Constant

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


I - SPECS SUMMARY
Named Constant Continuation

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING

CALCULATION
SPECIFICATIONS

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


C - SPECS FORM AREAS
1
2
3
4
5
6
7
8
678901234567890123456789012345678901234567890123456789012345678901234567890
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComment++++++++++++++
C Conditions

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

BASIC RPG/400 PROGRAMMING


C - SPECS FORM AREAS

Operations (Positions 18-53)


This area includes fields whose function is to inform
the computer about the operation to be executed.
Tests (Positions 54-59)
Indicators (usually 01-99) are specified in one or
more of the fields in this area to test a result field
value for positive, negative, and/or zero; High,Low
and Equal.
Comments (Positions 60-80)
Comments may be included in this area to support
instructions.
BASIC RPG400 PRO
GRAMMING

BASIC RPG/400 PROGRAMMING


C - SPECS SUMMARY
Position Name
6
Form Type

Entry
C

Explanation
Identification for a calculation specification

7-8

Blank

The operation is performed at detail calculation


time.

L0

The operation is performed at total calculation


time.

L1-L9

The operation is performed at total calculation


time when the control indicator is set on.

LR

The operation is performed after the last record


has been processed or after the LR indicator is
set on.

AN/OR

Indicators on more than one line condition the


operation.

Control Level

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


C - SPECS SUMMARY
(Continued)

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

Function Key indicators

L1-L9

Control level indicators

U1-U8

External indicators (Job switches)

OA-OG,OV Overflow indicators


18-27

Factor 1

Symbolic
name or
literal

Entry specifies a symbolic name or actual data


upon which the operation is performed.

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


C - SPECS SUMMARY
(Continued)

Position Name
28-32
Operation

Entry
Operation
code

Explanation
The operation to be performed.

33-42

Factor 2

Symbolic
name or
literal

Entry specifies a symbolic name or actual data


upon which the operation is performed.

43-48

Result Field

Field name

Names the field that contains the result of the


calculation performed.

49-51

Field length

Blank

The result field is defined elsewhere.

1-30

Numeric field length

1-256

Character field length

52

Decimal positions Blank

The result field is a character field or is defined


elsewhere.
Number of decimal positions in the result field.

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


C - SPECS SUMMARY
(Continued)

Position Name
53
Half adjust

54-59

Result indicators

Entry
Blank

Explanation
Half adjust is not performed.

Half adjust is performed.

Blank

No resulting indicators

01-99

General indicators

KA-KN &
KP-KY

Function Key indicators

L1-L9

Control level indicators

LR

Last record indicator

60-80

Comments

Comments

Document the calculation.

75-80

Amendment
number

Amendment
number

Amendment number
(MKI Standard Only!)

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


DEFINING INDICATORS
RECORD IDENTIFYING INDICATORS
pos. 19 & 20 of input specifications
FIELD INDICATORS
pos. 65 - 70 of input specifications
RESULTING INDICATORS
pos. 54 - 59 of calculation specifications
The purpose of the resulting indicator depends upon
the operation code specified.
VALID INDICATORS
RPG400 PRO
01 - 99 BASICLR
GRAMMING

BASIC RPG/400 PROGRAMMING


DEFINING INDICATORS
Control Level Indicators (L1 - L9 and LR)

can be specified in positions 7 and 8 of the Cspecs


Position 9 and 17

can specify one, two or three indicators (positions


10 & 11, 13 & 14 and 16 & 17) on each line.

Place an N before the appropriate indicator


(positions 9, 12 and 15), if it has to be off to
condition the operation

indicators are in an And relationship, when more


than one specified in the same line

specify AN/OR in positions 7 and 8 to place lines


of conditioning indicators in And/Or relationships

maximum BASIC
of seven
RPG400
(7) PRO
And/Or lines per group
GRAMMING

BASIC RPG/400 PROGRAMMING


SET ON OPERATION
SETON (Set On)
Indicators
7-8

9-17

Opt.

Opt.

Result
Factor 1
Blank

Factor 2
Blank

Field
Blank

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

58-59

At Least One Required

BASIC RPG/400 PROGRAMMING


SET OFF OPERATION
SETOF (Set Off)
Indicators

Result

7-8

9-17

Factor 1

Factor 2

Opt.

Opt.

Blank

Blank

Field
Blank

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

58-59

At Least One Required

BASIC RPG/400 PROGRAMMING


ARITHMETIC OPERATION CODES
ADD

Add

SUB

Subtract

MULT

Multiply

DIV

Divide

MVR

Move Remainder

Z-ADD

Zero & Add

Z-SUB

Zero & Subtract

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


ADD OPERATION
ADD (Add)
Indicators

Result
Field

7-8

9-17

Factor 1

Opt.

Opt.

Optional Required Require


d

Factor 2

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

+ve

-ve

58-59
Zero

BASIC RPG/400 PROGRAMMING


SUB OPERATION
SUB (Subtract)
Indicators

Result
Field

7-8

9-17

Factor 1

Opt.

Opt.

Optional Required Require


d

Factor 2

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

+ve

-ve

58-59
Zero

BASIC RPG/400 PROGRAMMING


Z-ADD OPERATION
Z-ADD (Zero and Add)
Indicators

Result

7-8

9-17

Factor 1

Opt.

Opt.

Blank

Factor 2

Field

Required Require
d

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

+ve

-ve

58-59
Zero

BASIC RPG/400 PROGRAMMING


Z-SUB OPERATION
Z-SUB (Zero and Subtract)
Indicators

Result

7-8

9-17

Factor 1

Opt.

Opt.

Blank

Factor 2

Field

Required Require
d

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

+ve

-ve

58-59
Zero

BASIC RPG/400 PROGRAMMING


MULT OPERATION
MULT (Multiply)
Indicators

Result
Field

7-8

9-17

Factor 1

Opt.

Opt.

Optional Required Require


d

Factor 2

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

+ve

-ve

58-59
Zero

BASIC RPG/400 PROGRAMMING


DIV OPERATION
DIV (Divide)
Indicators

Result
Field

7-8

9-17

Factor 1

Opt.

Opt.

Optional Required Require


d

Factor 2

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

+ve

-ve

58-59
Zero

BASIC RPG/400 PROGRAMMING


MVR OPERATION
MVR (Move remainder)
Indicators

Result

7-8

9-17

Factor 1

Factor 2

Opt.

Opt.

Blank

Blank

Field
Require
d

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

+ve

-ve

58-59
Zero

BASIC RPG/400 PROGRAMMING


ARITHMETIC OPERATIONS
can only be performed on numeric fields & literals
performed on data in packed decimal format. Data
stored in other formats is converted to or from packed
format. is performed for all arithmetic
decimal alignment
operations. Even if truncation occurs, the position of
the decimal point in the result field is not affected.
Factor 1 and factor 2 are not changed by arithmetic
operations
any data placed in the result field replaces previous
data
BASIC RPG400 PRO
GRAMMING

BASIC RPG/400 PROGRAMMING


ARITHMETIC OPERATIONS
truncation occurs when the result field is not large
enough to contain the operation's result:
- digits are dropped from the left when the
integer is too small
- digits are dropped from the right when the
number of decimal places is too small
half adjust can be used on all arithmetic
operations except MVR or DIV followed by MVR
the length of any field used in an arithmetic operation
cannot exceed 30 digits
all arithmetic operations are performed algebraically
BASIC RPG400 PRO
GRAMMING

BASIC RPG/400 PROGRAMMING


ARITHMETIC OPERATIONS
For arithmetic operations in which all three fields are
used:

Factor 1, Factor 2 and the Result field can be three


different fields

Factor 1, Factor 2 and the Result field can all be the


same field.

Factor 1 and Factor 2 can be the same but different


from the Result field.

Either Factor 1 or Factor 2 can be the same as the


Result field.
BASIC RPG400 PRO
GRAMMING

BASIC RPG/400 PROGRAMMING


ARITHMETIC OPERATIONS
results of all operations are signed (hexadecimal F
for positive and hexadecimal D for negative)
For the ADD, SUB, MULT and DIV operations,
Factor 1 is not required. If Factor 1 is not specified,
the operation is performed as though Factor 1 and
the Result field are the same field.

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


EXAMPLE
ON
ARITHMETIC
OPERATIONS
1
2
3
4
5
6
7

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

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


MOVE OPERATIONS
MOVE
Move Right
MOVEL
Move Left

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


MOVE OPERATION
MOVE (Move Right)
Indicators

Result

7-8

9-17

Factor 1

Factor 2

Opt.

Opt.

Blank

Required

Field
Required

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

58-59

+ve

-ve

Zero/Blank

BASIC RPG/400 PROGRAMMING


MOVE EXAMPLE 1
Factor 2 shorter than the result field
Factor 2
Character

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

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


MOVE EXAMPLE 2
Factor 2 longer than the result field
Factor 2
Character
Character

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

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


MOVE EXAMPLE 3
Factor 2 and the result field the same length
Factor 2

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

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


MOVEL OPERATION
MOVEL (Move Left)
Indicators

Result

7-8

9-17

Factor 1

Factor 2

Opt.

Opt.

Blank

Required

Field
Required

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

58-59

+ve

ve

Zero/Blank

BASIC RPG/400 PROGRAMMING


MOVEL EXAMPLE 1
Factor 2 shorter than the result field
Factor 2
Character C P T 5 N

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

Note : 4 = letter D, and 5 = letter N; arrow

Character

7 8 4 2 NH 4S A

is decimal point

BASIC RPG400 PRO


GRAMMING

Numeric

Numeric

Character

BASIC RPG/400 PROGRAMMING


MOVEL EXAMPLE 2
Factor 2 longer than the result field
Factor 2
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

BASIC RPG400 PRO


GRAMMING

Numeric

Character

BASIC RPG/400 PROGRAMMING


MOVEL EXAMPLE 3
Factor 2 and the result field the same length
Factor 2
Character P H 4 S N

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

BASIC RPG400 PRO


GRAMMING

Character

Numeric

Numeric

Character

BASIC RPG/400 PROGRAMMING


COMPARE OPERATIONS
COMP

Compare

CABxx

Compare & Branch

TAG

Tags

GOTO

Go to a Tag

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


REMINDERS FOR COMPARE
OPERATIONS
If numeric fields are compared, fields of unequal
length are aligned at the implied decimal point. The
shorter field is filled with zeros to the left or right of
the decimal point so that the field lengths are equal
for comparison.
If character fields are compared, fields of unequal
length are aligned to their leftmost character. The
shorter field is filled with blanks to equal the length of
the longer field so that the field lengths are equal for
BASIC RPG400 PRO
comparison
GRAMMING

BASIC RPG/400 PROGRAMMING


REMINDERS FOR COMPARE
OPERATIONS
(Continued)

All numeric comparisons are algebraic. A plus (+)


value is always greater than a minus () value.
Blanks within numeric fields are assumed to be
zeros.
Numeric fields are converted to packed decimal
format,if necessary, before they are compared.
A character field cannot be compared with a
numeric field.
BASIC RPG400 PRO
GRAMMING

BASIC RPG/400 PROGRAMMING


COMP OPERATION
COMP (Compare)
Indicators
7-8

9-17

Opt.

Opt.

Result
Factor 1

Factor 2

Required Required

Field
Blank

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

GT

LT

58-59
EQ

BASIC RPG/400 PROGRAMMING


COMP OPERATION EXAMPLE

1
2
3
4

Initial values for the fields are:


FLDA = 100.00 FLDB = 105.00
FLDD = ABC

FLDE = ABCDE
BASIC RPG400 PRO
GRAMMING

FLDC = 100.00

BASIC RPG/400 PROGRAMMING


CABxx OPERATION
CABxx (Compare and Branch)
Indicators
7-8

9-17

Opt.

Opt.

Result
Factor 1

Factor 2

Field

Required Required Optional

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

GT

LT

58-59
EQ

BASIC RPG/400 PROGRAMMING


CABxx OPERATION EXAMPLE
1234567
678901234567890123456789012345678901234567890123456789012345678901234
CL0N01N02N03Factor1+++OpcdeFactor2+++ResultLenDHHiLoEqComment++++++++
1 CFLDACABLTFLDBTAGX

C*
CFLDACABLEFLDBTAGX16
2
C*
3 CFLDACABFLDBTAGX1718
C*
CFLDACABEQFLDBTAGX20
4
C*

The field values are:


FLDA = 100.00

FLDB = 105.00
BASIC RPG400 PRO
GRAMMING

BASIC RPG/400 PROGRAMMING


GOTO OPERATION
GOTO (Go to a TAG)
Indicators
7-8

9-17

Opt.

Opt.

Result
Factor 1

Factor 2

Blank

Required

Field
Blank

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

Blank

Blank

58-59
Blank

BASIC RPG/400 PROGRAMMING


TAG OPERATION
TAG (Label)
Indicators
7-8
Opt.

9-17

Result
Factor 1

Blank Required

Factor 2
Blank

Field
Blank

BASIC RPG400 PRO


GRAMMING

Result Indicators
54-55

56-57

Blank

Blank

58-59
Blank

BASIC RPG/400 PROGRAMMING

HANDS-ON
EXERCISES

BASIC RPG400 PRO


GRAMMING

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.

BASIC RPG400 PRO


GRAMMING

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

BASIC RPG400 PRO


GRAMMING

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
********************************************************************

BASIC RPG400 PRO


GRAMMING

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

BASIC RPG400 PRO


GRAMMING

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

BASIC RPG400 PRO


GRAMMING

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

BASIC RPG400 PRO


GRAMMING

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
*******************************************************************

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING

OUTPUT
SPECIFICATIONS

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Program Described Files, Record Identification and
Control Entries (Record Line)

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Program Described Files, Record Identification and
Control Entries (Record Line)

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Program Described Files, Record Identification and
Control Entries (Record Line)

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Program Described Files, Record Identification and
Control Entries (Record Line)

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Program Described Files, Record Identification and
Control Entries (Record Line)

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Program Described Files, Record Identification and
Control Entries (Record Line)

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Program Described Files, Record Identification and
Control Entries (Record Line)

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Program Described Files, Field Description and
Control Entries (Field Line)

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Program Described Files, Field Description and
Control Entries (Field Line)

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Program Described Files, Field Description and
Control Entries (Field Line)

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Program Described Files, Field Description and
Control Entries (Field Line)

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Externally Described Files, Record Identification
and Control Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Externally Described Files, Record Identification
and Control Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Externally Described Files, Record Identification
and Control Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Externally Described Files, Record Identification
and Control Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Externally Described Files, Field Description and
Control Entries

BASIC RPG400 PRO


GRAMMING

BASIC RPG/400 PROGRAMMING


O - SPECS SUMMARY
Externally Described Files, Record Identification
and Control Entries

BASIC RPG400 PRO


GRAMMING

You might also like