In this section, we will cover the fundamental syntax and structure of Control Language (CL). Understanding these basics is crucial for writing effective CL programs. We will break down the key components and provide examples to illustrate each concept.

Key Concepts

  1. CL Program Structure
  2. Commands
  3. Parameters
  4. Comments
  5. Labels

  1. CL Program Structure

A CL program typically consists of a series of commands that are executed sequentially. The basic structure of a CL program is as follows:

PGM
    // Your commands go here
ENDPGM
  • PGM marks the beginning of the program.
  • ENDPGM marks the end of the program.

Example

PGM
    DCL VAR(&MSG) TYPE(*CHAR) LEN(50)
    CHGVAR VAR(&MSG) VALUE('Hello, World!')
    SNDPGMMSG MSG(&MSG)
ENDPGM

In this example:

  • DCL declares a variable.
  • CHGVAR changes the value of the variable.
  • SNDPGMMSG sends a program message.

  1. Commands

Commands are the building blocks of CL programs. Each command performs a specific operation. Commands are written in uppercase and follow a specific syntax.

Example

DCL VAR(&COUNTER) TYPE(*INT) LEN(4)

In this example:

  • DCL is the command to declare a variable.
  • VAR(&COUNTER) specifies the variable name.
  • TYPE(*INT) specifies the variable type.
  • LEN(4) specifies the length of the variable.

  1. Parameters

Parameters are used to provide additional information to commands. They are enclosed in parentheses and separated by spaces.

Example

SNDPGMMSG MSG('Processing Complete') TOPGMQ(*EXT) MSGTYPE(*INFO)

In this example:

  • MSG('Processing Complete') specifies the message to be sent.
  • TOPGMQ(*EXT) specifies the target program queue.
  • MSGTYPE(*INFO) specifies the message type.

  1. Comments

Comments are used to add explanations or notes within the code. They are ignored by the CL interpreter. Comments start with //.

Example

PGM
    // Declare a variable to hold the message
    DCL VAR(&MSG) TYPE(*CHAR) LEN(50)
    
    // Set the message value
    CHGVAR VAR(&MSG) VALUE('Hello, World!')
    
    // Send the message
    SNDPGMMSG MSG(&MSG)
ENDPGM

  1. Labels

Labels are used to mark specific points in the program. They are useful for controlling the flow of the program, such as in loops or conditional statements. Labels are defined by a name followed by a colon (:).

Example

PGM
    DCL VAR(&COUNTER) TYPE(*INT) LEN(4)
    CHGVAR VAR(&COUNTER) VALUE(1)
    
    LOOP: // Label for the loop
    IF COND(&COUNTER *LE 10) THEN(DO)
        SNDPGMMSG MSG('Counter is ' *CAT &COUNTER)
        CHGVAR VAR(&COUNTER) VALUE(&COUNTER + 1)
        GOTO CMDLBL(LOOP)
    ENDDO
ENDPGM

In this example:

  • LOOP: is a label.
  • GOTO CMDLBL(LOOP) directs the program flow to the LOOP label.

Practical Exercise

Exercise 1: Basic CL Program

Write a CL program that:

  1. Declares a variable to hold a message.
  2. Sets the message to "Welcome to CL Programming!".
  3. Sends the message.

Solution

PGM
    // Declare a variable to hold the message
    DCL VAR(&MSG) TYPE(*CHAR) LEN(50)
    
    // Set the message value
    CHGVAR VAR(&MSG) VALUE('Welcome to CL Programming!')
    
    // Send the message
    SNDPGMMSG MSG(&MSG)
ENDPGM

Exercise 2: Loop with Counter

Write a CL program that:

  1. Declares a counter variable.
  2. Initializes the counter to 1.
  3. Uses a loop to send a message "Counter is X" where X is the counter value.
  4. Increments the counter by 1.
  5. Repeats the loop until the counter reaches 5.

Solution

PGM
    DCL VAR(&COUNTER) TYPE(*INT) LEN(4)
    CHGVAR VAR(&COUNTER) VALUE(1)
    
    LOOP: // Label for the loop
    IF COND(&COUNTER *LE 5) THEN(DO)
        SNDPGMMSG MSG('Counter is ' *CAT &COUNTER)
        CHGVAR VAR(&COUNTER) VALUE(&COUNTER + 1)
        GOTO CMDLBL(LOOP)
    ENDDO
ENDPGM

Conclusion

In this section, we covered the basic syntax and structure of CL programs, including the use of commands, parameters, comments, and labels. Understanding these fundamentals is essential for writing and reading CL programs. In the next module, we will dive deeper into basic CL commands and their usage.

© Copyright 2024. All rights reserved