Show List

Control structures in CL400

CL400 provides several control structures that allow you to direct the flow of your program. Here are explanations and code examples for three commonly used control structures: IF, DO, and SELECT.

  • IF statements:

An IF statement is used to evaluate a condition and execute a set of statements if the condition is true. The basic syntax of an IF statement is as follows:

sql
Copy code
IF condition THEN statements ELSEIF condition THEN statements ELSE statements ENDIF

Here is an example of an IF statement that checks the value of a variable and outputs a message based on the result:

scss
Copy code
/* Sample IF statement */ DCL VAR(&COUNT) TYPE(*DEC) LEN(5 0) CHGVAR VAR(&COUNT) VALUE(10) IF (&COUNT > 5) THEN(DO) SNDPGMMSG MSG('Count is greater than 5') TOUSR(*CURRENT) ENDDO ELSEIF (&COUNT < 5) THEN(DO) SNDPGMMSG MSG('Count is less than 5') TOUSR(*CURRENT) ENDDO ELSE DO SNDPGMMSG MSG('Count is equal to 5') TOUSR(*CURRENT) ENDDO

In this example, the IF statement checks whether the value of the &COUNT variable is greater than 5, less than 5, or equal to 5, and outputs a message based on the result.

  • DO loops:

A DO loop is used to execute a set of statements repeatedly until a certain condition is met. The basic syntax of a DO loop is as follows:

vbnet
Copy code
DO {WHILE | UNTIL} condition statements ENDDO

Here is an example of a DO loop that counts from 1 to 10 and outputs each number:

scss
Copy code
/* Sample DO loop */ DCL VAR(&COUNT) TYPE(*DEC) LEN(5 0) DO WHILE (&COUNT <= 10) SNDPGMMSG MSG('Count is ' *CAT &COUNT) TOUSR(*CURRENT) ADDNBR 1 TOVAR(&COUNT) ENDDO

In this example, the DO loop starts with a value of 1 for the &COUNT variable, and adds 1 to the variable each time the loop is executed, until the variable reaches a value of 10.

  • SELECT statements:

A SELECT statement is used to choose one of several paths of execution based on the value of a variable or expression. The basic syntax of a SELECT statement is as follows:

sql
Copy code
SELECT WHEN condition THEN statements WHEN condition THEN statements ... OTHERWISE statements ENDSELECT

Here is an example of a SELECT statement that checks the value of a variable and outputs a message based on the result:

scss
Copy code
/* Sample SELECT statement */ DCL VAR(&FRUIT) TYPE(*CHAR) LEN(10) CHGVAR VAR(&FRUIT) VALUE('apple') SELECT WHEN (&FRUIT = 'apple') THEN(DO) SNDPGMMSG MSG('You chose an apple') TOUSR(*CURRENT) ENDDO WHEN (&FRUIT = 'banana') THEN(DO) SNDPGMMSG MSG('You chose a banana') TOUSR(*CURRENT) ENDDO OTHERWISE DO SNDPGMMSG MSG('You chose something else') TOUSR(*CURRENT) ENDDO ENDSELECT

In this example, the SELECT statement checks the value of the &FRUIT variable and outputs a message based on whether the value is 'apple', 'banana', or something else.

Overall, control structures like IF, DO, and SELECT are essential for controlling the flow of a CL400 program. They allow you to execute specific blocks of code based on certain conditions, repeat code until a condition is met, and choose from multiple paths of execution based on variable values.

By using these control structures effectively, you can create more sophisticated and flexible programs. In addition to the examples provided, there are many other ways to use these statements in your programs, depending on your specific needs and goals.


    Leave a Comment


  • captcha text