Sie sind auf Seite 1von 48

RPG IV

Top-Down, Structured Program Design Chapter 5

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

Structured Design
A methodology that limits its control to 3 basic structures:
Sequence Selection Iteration

Each of these control structures has a single entry point and a single exit point

Sequence Control Structure

Selection Control Structure

Iteration Control Structure

Relational Codes
Symbol > < = <> <= >= Code GT LT EQ NE LE GE Meaning Greater than Less than Equal to Not equal to Less than or equal to Greater than or equal to

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

Selection Operations
IF Conditional expression - ENDIF IF Conditional expression - ELSE ENDIF AND - OR Nested Ifs (Case logic) SELECT - ENDSL CASxx - ENDCS

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

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

AND - OR
AND, both relationships must be true OR, one or the other or both relationships must be true

Nested IFs
Build IFs within Ifs

IF and Page Overflow


Built in indicators called overflow indicators OA thru OG and OV Use the Keyword OFLIND in the File Specification to make the association between the indicator and report file The indicator will automatically come on when the printer reaches the overflow line at the bottom of the page

SELECT
Appears on a line alone to identify the start of a case construct The SELECT is followed by one or more WHEN, each of which specifies a conditions to be tested Each WHEN is followed by one or more statements to be performed WHEN conditions can be coupled with AND and/or OR operations

SELECT cont
OTHER means in all other cases OTHER should be the final catch all As soon as it encounters a true condition, the computer executes the operation(s) following a WHEN Control is then sent to the end of SELECT, signaled by an ENDSL

CASxx
Nearly identical to SELEC CASxx operation sends control to subroutines Use ENDCS to signal end of CASxx

Iteration Operations
DOW Conditional expression - ENDDO DOU Conditional expression - ENDDO DO - ENDDO FOR - ENDFOR

Do While Loop

DOW
This operation establishes a loop, based on a comparison Test is made at the beginning of loop All operations coded between this operator and ENDDO are repeated as long as the condition specified in the relational comparison remains true The DOW allows you to use AND and OR to form compound conditions

Do Until Loop

DOU
The DOU is a structure iteration operation very similar to DOW The DOW repeats while the specified conditions remains true The DOU repeats until the condition becomes true DOU the comparison is made after the instructions in the loop have been executed

DO
Designed specifically for countcontrolled loops Specify four things:
starting value of counter limit value of counter counter increment value to be added to counter

You can omit any of the four, default value is 1

DO cont
Factor 1 on the DO is the starting value of counter Factor 2 on the DO is the limit value of counter Result on the DO is the counter Factor 2 on the ENDDO is the increment value to be added to counter

FOR
FOR is a free-form replacement for the DO operation Like DO, FOR is a count-controlled loop instruction

Early Exits from Loops


ITER
When ITER is encountered within a loop, control skips past the remaining instructions in the loop and causes the next repetition to begin

LEAVE
Terminates the looping process and sends the control to the statements following the ENDDO

Subroutines
BEGSR/ENDSR EXSR

BEGSR/ENDSR
Block of code with an identifiable beginning and end Subroutines are identified by the (optional) letters SR in positions 7-8 of a line, The name of the subroutine in factor 1 and the operation BEGSR The last line of a subroutine is a line containing ENDSR in the operation

BEGSR/ENDSR cont
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

EXSR
Execute subroutine Enter the name of the subroutine to be performed in factor 2

Control Break IPO


INPUT File SALESFIL Salesman Department Sales Sales date PROCESS Print Heading Initialize variables For each record Read Record Determine Salesman break Subtotal Sales Print Salesman and Sales Accumulate Grand total Print Grand total OUTPUT Heading Report Body Salesman Sales Subtotal Sales Grand total

Control Break Hierarchy


0000-Mainline

1000-Initial

3000-SlspBreak

5000-Detail

7000-Terminate

3000-SlspBreak

Control Break Logic: 0000-MainLine


Do Initialization Routine WHILE not end-of file IF change in salesperson Do SlspBreak Routine ENDIF Do Detail Process Routine Read next record ENDWHILE Do Termination Routine END Program

Control Break Logic: 1000-Initial


Read first record Set up hold area Print Headings END routine

Control Break Logic: 3000-Slspbreak


Print salesperson line Add salespersons total to grand total Zero out salespersons total Move new value to hold area END routine

Control Break Logic: 5000-Detail


IF page overflow Print headings ENDIF Print detail line Accumulate sales in salespersons total END routine

Control Break Logic: 7000-Terminate


Do Slspbreak Routine Print grand total END Routine

Sample Program - F Specs


*..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 F************************************************************************** F* Program :R0400HOK * F* Name :Yeager, Meyers & Kronholm * F* Description :This program produces a Sales Report Listing subtotal * F* for each salesman * F* Indicators :OF - Overflow * F* LR - Last Record * F* * F************************************************************************** *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 F*ilename++IP FRlen+ Device * FSalesFile IF F 19 DISK FQPRINT O F 132 PRINTER OFLIND(*INOF)

Sample Program - D & I Specs


D************************************************************************** D* Definition of all work fields used in program. * D************************************************************************** *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 D*Name++++++++++ Ds Len+ Dc D HoldSlsp S 4 D SlspTotal S 6 2 INZ(0) D GrandTotal S 8 2 INZ(0) I************************************************************************** I* Input file of received items defined within the program. * I************************************************************************** *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 I*ilename++Sq ISalesFile NS *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 I* From+To+++DcField+++++++++ I 1 4 SalesPrsn I 5 7 Dept I 8 13 2SalesAmt

Sample Program - C Specs


C************************************************************************** C* OOOO-Mainline * C* Calculations required to produce the sales report * C************************************************************************** C EXSR Initial C* C DOW Not %EOF(SalesFile) C IF HoldSlsp <> SalesPrsn C EXSR SlspBreak C ENDIF C EXSR Detail C READ SalesFile 90 C ENDDO C* C ESXR Terminate C EVAL *INLR = *ON C RETURN

Sample Program - C Specs


C************************************************************************** C* 1000-Initial * C* Subroutine to read first record, set up hold salesman, and print * C* first page heading. * C************************************************************************** CSR Initial BEGSR C READ SalesFile 90 C EVAL HoldSlsp = SalesPrsn C EXCEPT Headings C ENDSR

Sample Program - C Specs


C************************************************************************** C* 3000-SlspBreak * C* Subroutine done when salesman changes; print subtotal, rollover * C* accumulator, zero out accumulator, and reset hold * C************************************************************************** CSR SlspBreak BEGSR C EXCEPT BreakLine C EVAL GrandTotal = GrandTotal + SlspTotal C EVAL SlspTotal = 0 C EVAL HoldSlsp = SalesPrsn C ENDSR

Sample Program - C Specs


C************************************************************************** C* 5000-Detail * C* Subroutine executed for each input record * C************************************************************************** CSR Detail BEGSR C IF *INOF = *ON C EXCEPT Headings C EVAL *INOF = *OFF C ENDIF C* C EXCEPT DetailLine C EVAL SlspTotal = SlspTotal + SalesAmt C ENDSR

Sample Program - C Specs


C************************************************************************** C* 7000-Terminate * C* Subroutine done at end of file; execute SlspBreak one last time* C* and print grand total line. * C************************************************************************** CSR Terminate BEGSR C EXSR SlspBreak C EXCEPT TotalLine C ENDSR

Sample Program - O Specs


*..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 O*ilename++D ExceptnameB++A++Sb+Sa+ OQPRINT E Headings 2 2 *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 O* Field+++++++++Y End++ Constant/Editword+++++++++++ O 8 R0400HOK O* *DATE is a four digit year. O *DATE Y 20 O 33 SALES REPORT' O 40 'PAGE' O PAGE Z 44 O E Headings 2 O 20 SLSPSN.' O 37 AMT.' O** Detail Line ** O E DetailLine 1 O SalesPrsn 18 O SalesAmt 1 39

Sample Program - O Specs


*..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 O*ilename++D ExceptnameB++A++Sb+Sa+ O E BreakLine 1 2 *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 O* Field+++++++++Y End++ Constant/Editword+++++++++++ O 24 TOTAL O SlspTotal 1 39 O 40 * *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 O*ilename++D ExceptnameB++A++Sb+Sa+ O E TotalLine *..1....+....2....+....3....+....4....+....5....+....6....+....7....+....8 O* Field+++++++++Y End++ Constant/Editword+++++++++++ O 26 'GRAND TOTAL' O GrandTotal 1 39 O 41 **

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 that you approach designing your program hierarchically, working out its broad logic first and later attending to the detailed processing requirements

Points to Remember (cont)


RPG provides structured operations IF, CASxx, and SELECT to implements decision logic Structured operations DOW, DOU, DO and FOR to implement looping logic Most of the structured operation of RPG require the use of one of six relational operators The operators are: GT, LT, EQ, NE, LE, GE

Das könnte Ihnen auch gefallen