Vous êtes sur la page 1sur 4

http://publib.boulder.ibm.com/infocenter/iseries/v5r3/topic/books/sc415721.

pdf
Parts of a CL Procedure
While each source statement entered as part of a CL procedure is actually a CL
command, the source can be divided into the following basic parts used in many
typical CL procedures.
PGM command
PGM PARM(&A)
Optional PGM command beginning the procedure and identifying any
parameters received.
Declare commands
(DCL, DCLF, COPYRIGHT)
Mandatory declaration of procedure variables when variables are used. The
declare commands must precede all other commands except the PGM
command.
CL processing commands
CHGVAR, SNDPGMMSG, OVRDBF, DLTF,
CL commands used as source statements to manipulate constants or
variables (this is a partial list).
Logic control commands
IF, THEN, ELSE, DO, ENDDO, DOWHILE, DOUNTIL, DOFOR, LEAVE, ITERATE,
GOTO, SELECT, ENDSELECT, WHEN, OTHERWISE
Commands used to control processing within the CL procedure.
Built-in functions
%SUBSTRING (%SST), %SWITCH, and %BINARY (%BIN)
Built-in functions and operators used in arithmetic, relational or logical
expressions.
Program control commands
CALL, RETURN
CL commands used to pass control to other programs.
Procedure control commands
CALLPRC, RETURN
CL commands used to pass control to other procedures.
ENDPGM command
ENDPGM
Optional End Program command.

Declaring a Variable
In its simplest form, the Declare CL Variable (DCL) command has the following
parameters:
DCL &OBJ (TYPE) LEN(10)

Specifying the Data Type


The
TYPE parameter
on the DCL command also is required. This parameter specifies the type of
data the variable can contain. CL supports six data types when declaring variables:

*
CHAR for variables containing character data

*LGL for variables containing logical data

*DEC for variables containing numeric data in packed decimal format

*INT for variables containing numeric data in signed integer format

*UINT for variables containing numeric data in unsigned integer format

*PTR for pointer variables containing a memory address


Unconditional branching means that you can instruct processing to branch to
commands or sets of commands located anywhere in the procedure without regard
to what conditions exist at the time the branch instruction is processed.
Unconditional processing commands include:
GOTO
ITERATE
LEAVE
Conditional branching means that under certain specified conditions, processing
may branch to sections or commands that are not consecutive within the
procedure. The branching may be to any statement in the procedure. This is called
conditional processing because the branching only occurs when the specified
condition is true. Conditional processing is usually associated with the IF
command. With the ELSE command, you can specify alternative processing if the
condition is not true.The simple DO command allows you to create groups of
commands that are always processed together, as a group, under specified
conditions. Conditional processing commands include:
v
IF and THEN
v
SELECT, WHEN, and OTHERWISE
v
DOFOR
v
DOWHILE
v
DOUNTIL
IF
COND(&OPTION=1) THEN(CALLPRC PRC(ADDREC))
ELSE
CMD(IF COND(&OPTION=2) THEN(CALLPRC PRC(DSPFILE)))
ELSE
CMD(IF COND(&OPTION=3) THEN(CALLPRC PRC(PRINTFILE)))
ELSE CMD(IF COND(&OPTION=4) THEN(CALLPRC PRC(DUMP)))
RCLRSC
RETURN

Up to 25 levels of such embedding are permitted in CL programming.


As the levels of embedding increase and logic grows more complex, you may wish
to enter the code in free-form design to clarify relationships:
PGM
DCL &A *DEC 1
DCL &B *CHAR 2

DCL &RESP *DEC 1


IF (&RESP=1) +
IF (&A=5) +
IF (&B=NO) THEN(DO)
.
.
.
ENDDO
CHGVAR &A VALUE(8)
CALL PGM(DAILY)
ENDPGM

The following is an example of conditional processing with a DOUNTIL command.


DOUNTIL (&LGL)
.
.
.
CHGVAR &INT (&INT + 1)
IF (&INT *GT 5) (CHGVAR &LGL 1)
ENDDO

The following is an example of conditional processing with a DOWHILE


command.
DOWHILE (&LGL)
.
.
.
IF (&INT *EQ 2) (CHGVAR &LGL 0)
ENDDO

Using the ITERATE Command


The ITERATE command can be used to skip the remaining commands in an active
DOWHILE, DOUNTIL, or DOFOR group. ITERATE is not valid with simple DO
command groups.
An ITERATE command without a label will skip to the ENDDO of the innermost
active DO group. Specifying a label skips to the ENDDO of the DO associated with
the label.
The following illustrates use of the ITERATE command:
DO_1:
DO_2:DOWHILE &LGL
DO_3:
DOFOR &INT FROM(0) TO(99)
.
.
.
IF (&A *EQ 12) THEN (ITERATE DO_1)
.
. /* Not processed if &A equals 12
*/
.
IF (&A *GT 12) ITERATE
.
.
/* Not processed if &A greater than 12 */
.
ENDDO
.
.
.
IF (&A *LT 0) (ITERATE DO_1)
.

. /* Not processed if &A less than zero


*/
.
ENDDO

A LEAVE command without a label will leave the innermost active DO group.
Specifying a label allows the processing to break out of one or more enclosing
groups.
The following illustrates use of the LEAVE command:
DO_1:
DO_2:DOWHILE &LGL
DO_3:
DOFOR &INT FROM(0) TO(99)
.
.
.
IF (&A *EQ 12) THEN(LEAVE DO_1)
.
. /* Not processed if &A equals 12
*/
.
IF (&A *GT 12) LEAVE
.
.
/* Not processed if &A greater than 12 */
.
ENDDO
.
.
.
IF (&A *LT 0) (LEAVE DO_1)
.
. /* Not processed if &A less than zero
*/
.
ENDDO

The general structure of the SELECT command is as follows:


SELECT
WHEN (condition-1) THEN(command-1)
.
.
WHEN (condition-n) THEN(command-n)
OTHERWISE command-x
ENDSELECT
SELECT
WHEN (&LGL)
WHEN (&INT *LT 0)
WHEN (&INT *GT 0)
CHGVAR &INT (&INT
ENDDO
OTHERWISE (CHGVAR
ENDSELECT

THEN(CHGVAR &INT 0)
(DOUNTIL (&INT *EQ 0))
- 1)
&LGL 1)

Vous aimerez peut-être aussi