Controlling processing

162 Views


Controlling processing within a CL program or CL procedure

You can use commands to change the flow of logic within your CL procedure.

Commands in a CL procedure are processed in consecutive sequence.

Each command is processed, one after another, in the sequence in which it is encountered.

You can alter this consecutive processing using commands that change the flow of logic in the procedure.

These commands can be conditional or unconditional.

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
  • CALLSUBR

Conditional branching means that under certain specified conditions, processing can branch to sections or commands that are not consecutive within the procedure.

This is called conditional processing because the branching only occurs when the specified condition is true.

Conditional processing commands include:

  • IF and THEN
  • SELECT, WHEN
  • OTHERWISE
  • DOFOR
  • DOWHILE
  • DOUNTIL

GOTO command and command labels in a CL program or procedure

The Go To (GOTO) command processes an unconditional branch.

With the GOTO command, processing is directed to another part (identified by a label) of the program or procedure whenever the GOTO command is encountered.

This branching does not depend on the evaluation of an expression.

The GOTO command has one parameter, which contains the label of the statement branched to.

GOTO CMDLBL(label)
				PGM
				.
				.
				.
START:          SNDRCVF RCDFMT(MENU)
				IF (&RESP=1) THEN(CALL CUS210)
				.
				.
				.
				GOTO START
				.
				.
				.
				ENDPGM

IF command in a CL program or procedure

The If (IF) command is used to state a condition that, if true, specifies a statement or group of statements in the program or procedure to be run.

The Else (ELSE) command can be used with the IF command to specify a statement or group of statements to be run if the condition expressed by the IF command is false.

			IF (&A=2) THEN(GOTO FINAL)
			IF (&A=3) THEN(CHGVAR &C 5)
			.
			.
			.
FINAL:      IF (&C=5) CALL PROGA
			ENDPGM
IF (&A=&B) THEN(CALLPRC PROCA)
ELSE CMD(CALLPRC PROCB)
CHGVAR &C 8

Embedded IF commands in a CL program or procedure
An If (IF) command can be embedded in another IF command.

IF (&A=&B) THEN(IF (&C=&D) THEN(GOTO END))
GOTO START

As the levels of embedding increase and logic grows more complex, you might want to enter the code in free-form design to clarify relationships:

Branch must be different depending on which condition fails.

DO command and DO groups in a CL program or procedure

The Do (DO) command allows you to process a group of commands together.

The group is defined as all those commands between the DO command and the corresponding End Do Group (ENDDO) command.

Do groups are most frequently associated with the IF, ELSE, or MONMSG commands. Here is an example of a Do group.

If the logical expression (&A=&B) is true, then the Do group is processed. If the expression is not true, then processing starts after the ENDDO command; the Do group is skipped.

Do groups can be nested within other Do groups, up to a maximum of 25 levels of nesting.

DOUNTIL command in a CL program or procedure

The Do Until (DOUNTIL) command processes a group of CL commands one or more times.

The group of commands is defined as those commands between the DOUNTIL and the matching End Do (ENDDO) command.

After the group of commands is processed, the stated condition is evaluated. The body of the DOUNTIL group will be run at least one time. 

If the condition is true, the DOUNTIL group will be exited and processing will resume with the next command following the associated ENDDO.

If the condition is false, the group will continue processing with the first command in the group.

Leave (LEAVE) command can be used to exit the DOUNTIL group and resume processing following the ENDDO.

ITERATE command can be used to skip the remaining commands in the group and evaluate the stated condition immediately.

DOWHILE command in a CL program or procedure

The Do While (DOWHILE) command lets you process a group of commands zero or more times while the value of a logical expression is true.

The DOWHILE command is used to state a condition that, if true, specifies a command or group of commands in the program or procedure to run.

The stated condition is evaluated before the group of commands is processed.

The group of commands will never be processed if the stated condition is initially false.

DOFOR command in a CL program or procedure

The Do For (DOFOR) command lets you process a group of commands a specified number of times.

The DOFOR command specifies a variable, its initial value, an increment or decrement amount, and a terminal value condition.

The format of the DOFOR command is:

DOFOR VAR(integer-variable) FROM(initial-value) TO(end-value) BY(integer-constant)

When the DOFOR group is processed, &INT is initialized to 2 and the value of &INT is checked to see if it is greater than 4. It is not, so the body of the group is processed. On the second iteration of the group, one is added to &INT and the check is repeated. It is less than 4, so the DOFOR group is processed again. On reaching the ENDDO the second time, the value of &INT is again incremented by 1. &INT now has a value of 4. Because &INT is still less than or equal to 4, the DOFOR group is processed again. On reaching the ENDDO the third time, the value of &INT is again incremented by 1. This time, the value is 5, and processing continues with the command following the ENDDO.

ITERATE command in a CL program or procedure

The Iterate (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.

In this example, the labels DO_1 and DO_2 are associated with the DOWHILE group. They can be specified on an ITERATE command appearing in either the DOWHILE or DOFOR group. When &A is equal to 12, the ITERATE DO_1 command is run. Processing continues at the ENDDO associated with the DOWHILE command. The value of &LGL is evaluated and, if true, continues with the DOFOR following the DOWHILE. If &LGL is false, processing continues with the CL command following the second ENDDO.

If &A is not equal to 12 but is greater than 12, processing continues with the ENDDO of the DOFOR group. The value of &INT is incremented and compared to the ending value of 99. If &INT is less than or equal to 99, processing continues with the first command following the Do For (DOFOR) command. If &INT is greater than 99, processing continues with the next command following the first ENDDO.

LEAVE command in a CL program or procedure

The Leave (LEAVE) command can be used to exit an active DOWHILE, DOUNTIL, or DOFOR group.

It provides a structured manner to leave an active group without resorting to the use of the Goto (GOTO) command.

LEAVE is not valid with simple Do (DO) command groups. 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.

In this example, the labels DO_1 and DO_2 are associated with the DOWHILE group. They can be specified on a LEAVE command appearing in either the DOWHILE or DOFOR group. When &A is equal to 12, the LEAVE DO_1 command is run and processing continues with the CL command following the second ENDDO.

If &A is not equal to 12 but is greater than 12, the DOFOR group is exited and processing continues with next command following the first ENDDO.

CALLSUBR command in a CL program or procedure

The Call Subroutine (CALLSUBR) command is used in a CL program or procedure for passing control to a subroutine that is defined within the same program or procedure.

The CALLSUBR command has two parameters: Subroutine (SUBR), which contains the name of the subroutine to which control is to be transferred to, and Return value (RTNVAL), which specifies the variable that will contain the return value from the called subroutine.

See the following example: CALLSUBR SUBR(mysubr) RTNVAL(&myrtnvar)

The subroutine mysubr must be defined in the program or procedure by the Subroutine (SUBR) parameter of an SUBR command. 

The CALLSUBR command can be placed anywhere within the program or procedure.

In the following example, the first CALLSUBR command passes control to the subroutine SUBR1, and the return value of 12 is placed into the variable &myrtnvar when control returns. If a message is monitored by the MONMSG command, the Goto (GOTO) command is run and control branches to the label DUMP

SELECT command and SELECT groups in a CL program or procedure

The Select (SELECT) command is used to identify one or more conditions and an associated group of commands to process when that condition is true.

A special group of commands can also be specified to be processed when none of the stated conditions are true.

Only one of the groups of commands identified by When (WHEN) or Otherwise (OTHERWISE) commands will be processed within the group.

The general structure of the SELECT command is as follows:

A SELECT group must specify at least one WHEN command. The WHEN command includes an expression, which is tested (true or false), and an optional THEN parameter that specifies the action to take if the condition is true.

The logical expression on the COND parameter can be a single logical variable or constant, or it must describe a relationship between two or more operands; the expression is then evaluate as true or false.

SUBR command and subroutines in a CL program or procedure

The Subroutine (SUBR) command is used in a CL program or procedure, along with the End Subroutine (ENDSUBR) command, to delimit the group of commands that define a subroutine.

The name of the subroutine, as used by the CALLSUBR command, is defined by the SUBR parameter on the SUBR command.

The first SUBR command that is encountered in a program or procedure also marks the end of the mainline of that program or procedure.

The SUBR and ENDSUBR commands must be matched pairs, and subroutines cannot be nested.

The following example is about the general structure of a CL procedure that contains a subroutine.

In this example, the Declare Processing Options (DCLPRCOPT) command was used to specify the size of the subroutine stack to be 25. The variable &RTNVAR is used to contain the return value from the subroutine. The CALLSUBR command will transfer control to the subroutine SUBR1, as defined by the SUBR command. If the RTNSUBR command is run, the value of &RTNVAR will be -1, if the ENDSUBR command is run, &RTNVAR will equal 0. If no RTNVAL parameter was defined on the CALLSUBR command, the return value from the subroutine would be ignored.

*AND, *OR, and *NOT operators

The logical operators *AND and *OR specify the relationship between operands in a logical expression.

The logical operator *NOT is used to negate logical variables or constants.

*AND and *OR are the reserved values used to specify the relationship between operands in a logical expression.

The ampersand symbol (&) can replace the reserved value *AND, and the vertical bar (|) can replace *OR. The reserved values must be preceded and followed by blanks. 

There are three kinds of operators other than logical operators: 

Arithmetic (+, -, *, /) 

Character (*CAT, ||, *BCAT, |>, *TCAT, |, *LT, =, *LE, , *NL, ¬<)

Relational (*EQ, =, *GT, >, *LT, =, *LE, , *NL, ¬<)

The following examples are about logical expressions:

((&C *LT 1) *AND (&TIME *GT 1430))

(&C *LT 1 *AND &TIME *GT 1430)

((&C < 1) & (&TIME>1430))

((&C< 1) & (&TIME>1430)


 

Post Comments