Thursday, April 17, 2014

A Simple Loop Program

Here it is a simple program that print counting upto 10. it is a one of the basic programs but still it has so many advance topics and techniques. let us see the full coding first.

0001.00 DCOUNTER          S              2P 0                                                               
0002.00 D                                                                                                   
0003.00 C                   FOR       COUNTER = 1 TO 10                                                     
0004.00 C     COUNTER       DSPLY                                                                           
0005.00 C                   ENDFOR                                                                          
0006.00 C                   SETON                                            LR                              

Now, Let us parse the program line by line

0001.00 DCOUNTER          S              2P 0                                             
It is the very first line of the program.
0001.00 = line number
D = shows that it is Data Specification (RPGLE program is consists of specification such as H, F, D, I, C, . . . ). D-spec is used to declare variables
counter = variable name
S = it shows that variable is data structure (DS), single(S), constant(C)...
2 = variable length
P = data type ( few data type are character(A), Packed Decimal(P), zoned(S). 0 is showing that there is NO decimal Place

0002.00 D      (blank line)


0003.00 C                   FOR       COUNTER = 1 TO 10


C = indicate that following line is Calculation (C) Specification related. every calculation, looping, branching, program-calling is done here in C-Spec.
this line tells the compiler that a 'for-loop' has to be execute from 1 to 10.

0004.00 C     COUNTER       DSPLY

it display the value of 'counter' variable

0005.00 C                   ENDFOR      

this line complete the for loop and every thing between 'FOR' and 'ENDFOR' is part/ body of for loop. 

FOR-LOOP Syntax


FOR

.
.
.
.
ENDFOR 

0006.00 C                   SETON                                            LR        

it set on the last record(LR) indicator. which tells the compiler that end now.
A very importantand must have line in the RPG PGM. Without having this line compiler will generate following error while compiling the PGM and PGM will not compile.

 Msg id  Sv Number Seq     Message text                                               *RNF7023 40      0         The Compiler cannot determine how the program can end.        

Images




         

No comments:

Post a Comment