Sunteți pe pagina 1din 22

CL Control Structures

Part 9

Presentation Copyright 2008, Bryan Meyers, www.bmeyers.net


CL and Structured
Programming
GOTO
IF/ELSE
SELECT/WHEN/OTHERWISE
DOWHILE (Do While)
DOUNTIL (Do Until)
DOFOR (Do For)
CALLSUBR (Call Subroutine)
GOTO Command

GOTO jumps to a label


Unconditional branch
Can branch forward or backward
Sequential processing resumes at label

GOTO RESUME
LOOP1: CALL PGMPROC
DLTF WRKFILE
CALL RESET
GOTO LOOP1
RESUME: RETURN
ENDPGM
IF Command

IF provides conditional processing


Defines, tests condition and single command to
execute if condition is true
Block of multiple commands not supported
IF COND(&user = 'STUDENT1') THEN(DSPTAP TAP01 OUTPUT(*PRINT))

IF (&user = 'JOAN') GOTO SKIP1

IF (&number = 15) CHGVAR &number (&number - 1)


Nested IF Structures

IF command allows up to ten nesting levels


Positional notation, indentation aid readability
IF COND(&user = 'STUDENT1') +
THEN(IF COND(&number = 5) THEN(DSPTAP TAP01 OUTPUT(*PRINT)))

IF (&user = 'STUDENT1') +
IF (&number = 5) DSPTAP TAP01 OUTPUT(*PRINT)

IF (&user = 'STUDENT1') +
IF (&number = 5) +
IF (&day = 'Tuesday') DSPTAP TAP01 OUTPUT(*PRINT)
ELSE Command

ELSE specifies single command to execute if


preceding IF condition is not true
Must follow required IF command
LOOP: SNDRCVF
IF (&option = 1) GOTO OPT1
IF (&option = 2) GOTO OPT2
CHGVAR &option 0
CALL PGM0
IF (&continue = 'Yes') GOTO LOOP
ELSE GOTO END
OPT1: CALL PGM1
GOTO LOOP
OPT2: CALL PGM2
GOTO LOOP
END: RETURN
Nested IF/ELSE Commands

ELSE command is paired with last unpaired IF


command
ELSE can specify no processing if necessary
IF (&user = 'STUDENT1') + /* Level 1 */
IF (&number = 5) + /* Level 2 */
DSPTAP TAP01 OUTPUT(*PRINT)
ELSE IF (&day = 'Tuesday') + /* Level 3 */
GOTO NEXT1
ELSE /* No processing */
ELSE GOTO ENDIT
DO Groups

DO command defines a block of commands to


be processed
Can be used with conditional commands (IF, ELSE,
etc.)
ENDDO command must complete DO group
Style: always use DO with conditional
commands
IF (&option *EQ 1) DO
CALL PGMYES
DLTF TRANSFILE
ENDDO
SELECT/WHEN/OTHERWISE
May simplify nested IF/ELSE structures
Case logic
SELECT/ENDSELECT form a SELECT group
WHEN command evaluate condition, executes
command when true
Only one WHEN command is executed
Control falls to ENDSELECT after WHEN is executed
WHEN command may specify no processing, if
necessary
OTHERWISE command executes if no previous
WHEN conditions are true
SELECT/WHEN/OTHERWISE

SELECT
WHEN (&sales *LE 5000) /* Do nothing */
WHEN (&sales *LE 10000) CALL PGM1
WHEN (&sales *LE 20000) CALL PGM2
OTHERWISE CALL PGM3
ENDSELECT

SELECT
WHEN (&int *LT 0) /* Do nothing */
WHEN (&int *EQ 0) DO
CHGVAR &status Y
CALL PGMYES
DLTF TRANSFILE
ENDDO
OTHERWISE CALL PGM2
ENDSELECT
Program Looping and Iteration

Conditional loop repeats a code group based


upon a condition
DOWHILE command
DOUNTIL command
Count-controlled loop executes a code group a
specific number of times
DOFOR command
DOWHILE Command

DOWHILE repeats group of commands while a


condition is true
ENDDO must close group
Leading decision loop
Condition is tested before group is executed
Group will never execute if condition is initially
false
DCL &flag *LGL VALUE('1') /* On */
DCL &int *INT

DOWHILE &flag
CHGVAR &int (&int + 1)
IF (&int *GT 100) CHGVAR &flag '0' /* Off */
ENDDO
DOUNTIL Command

DOUNTIL repeats group of commands until a


condition is true
ENDDO must close group
Trailing decision loop
Condition is tested after group executes at least
once
DCL &flag *LGL VALUE('0') /* Off */
DCL &int *INT

DOUNTIL &flag
CHGVAR &int (&int + 1)
IF (&int *GT 100) CHGVAR &flag '1' /* On */
ENDDO
DOFOR Command

DOFOR repeats group of commands desired


number of times
ENDDO must close group
Required parameters
VAR names counter variable to control loop
VAR is automatically incremented after each repetition
FROM indicates starting value for VAR
Group is repeated while TO value is not exceeded
DCL &int *UINT

DOFOR &int FROM(1) TO(100)

ENDDO
Loops and Early Exits

Skip remaining instructions in DOWHILE,


DOUNTIL or DOFOR group
ITERATE command causes next iteration
LEAVE command terminates loop
Passes control to statement following ENDDO
DCL &mode *CHAR 6

DOUNTIL (&mode *EQ 'EXIT')
CALL PGMA
IF &mode = 'EXIT' LEAVE
IF &mode = 'ADD' DO
CALL PGMB
ITERATE
ENDDO
CALL PGMC
ENDDO
Loops and Early Exits

LEAVE and ITERATE may specify a label


DCL &int *INT
DCL &mode *CHAR 6
DCL &lgl *LGL VALUE('1')

DO1: DOFOR &int FROM(1) TO(10)

DO2: DOUNTIL (&lgl)

IF (&mode *EQ 'EXIT') LEAVE DO1

ENDDO /* DOUNTIL */

ENDDO /* DOFOR */
Using Subroutines

Subroutine is a named block of code used to


organize processing
Coded at end of procedure section
Top-down design
CALLSUBR executes subroutine
SUBR identifies beginning of subroutine
ENDSUBR identifies end of subroutine
Returns to line following CALLSUBR
RTNSUBR provides early out
Using Subroutines
DOUNTIL (&option *EQ 1)
IF (&answer *EQ 'Y') CALLSUBR YES
ELSE CALLSUBR NO
ENDDO
CALLSUBR CLEANUP
RETURN

SUBR CLEANUP
DLTF WORKFILE
CALL PGMLOG
ENDSUBR

SUBR NO
CALL PGMNO
CALL RESET
IF (&continue = 'N') CHGVAR &option 0
ELSE CHGVAR &option 1
ENDSUBR

SUBR YES
CALL PGMYES
DLTF TRANSFILE
IF (&continue = 'N') CHGVAR &option 0
ELSE CHGVAR &option 1
ENDSUBR
Subroutines and GOTO

GOTO must specify label within subroutine


Cannot branch out of subroutine
Nested subroutines not allowed
One subroutine can CALLSUBR another subroutine
START: SUBR MYSUBR
CALL PGMYES
IF (&restart = 'Y') GOTO START
DLTF TRANSFILE
IF (&continue = 'N') CHGVAR &option 0
ELSE CHGVAR &option 1
ENDSUBR
Leaving a Subroutine

ENDSUBR
Control returns to line immediately following
CALLSUBR
Only one ENDSUBR allowed per subroutine
RTNSUBR
Leaves a subroutine early
Subroutine can have several RTNSUBR commands
SUBR MYSUBR
CALL PGMYES
IF (&cancel = 'Y') RTNSUBR
DLTF TRANSFILE
IF (&continue = 'N') CHGVAR &option 0
ELSE CHGVAR &option 1
ENDSUBR
Using a Return Value

ENDSUBR and RTNSUBR can pass a signed


integer value (RTNVAL) back to caller
E.g., error code
CALLSUBR can receive RTNVAL
DCL &error *INT

CALLSUBR MYSUBR RTNVAL(&error)
IF (&error *NE 0) DO
(Processing for nonzero return value)
ENDDO

RETURN

SUBR MYSUBR
CALL PGMA
IF (&error = Y) RTNSUBR RTNVAL(-1)
ENDSUBR RTNVAL(0)
DCLPRCOPT Command

DCLPRCOPT sets processing and compile


options for compile unit
Sets size of subroutine stack
Specifies compiler processing options
DCLPRCOPT overrides options set by CRTCLMOD or
CRTBNDCL compiler commands
Coded in declarations section
Only one allowed
DCLPRCOPT SUBRSTACK(256) +
LOG(*YES) +
ALWRTVSRC(*NO)

S-ar putea să vă placă și