In this section, we will guide you through writing your first Control Language (CL) program. By the end of this lesson, you will have a basic understanding of how to create, compile, and run a simple CL program.

Objectives

  • Understand the basic structure of a CL program.
  • Learn how to create a CL source file.
  • Compile and run your first CL program.

Basic Structure of a CL Program

A CL program typically consists of the following components:

  1. Program Declaration: Defines the program and its attributes.
  2. Variable Declarations: Declares any variables that will be used in the program.
  3. Command Statements: Contains the logic and commands to be executed.
  4. End Program Statement: Marks the end of the program.

Example Structure

PGM        /* Program Declaration */
DCL        /* Variable Declarations */
           /* Command Statements */
ENDPGM     /* End Program Statement */

Step-by-Step Guide to Writing Your First CL Program

Step 1: Setting Up Your Environment

Before you start writing your CL program, ensure that your environment is set up correctly. Refer to the previous section on Setting Up Your Environment for detailed instructions.

Step 2: Creating a CL Source File

  1. Open your development environment (e.g., IBM i Access Client Solutions).
  2. Create a new source file:
    • Navigate to the source physical file where you want to store your CL program. For example, QCLSRC.
    • Create a new member in the source file. For example, MYFIRSTCL.

Step 3: Writing the CL Program

Open the newly created source member and enter the following code:

PGM        /* Start of the program */

DCL        VAR(&MSG) TYPE(*CHAR) LEN(50) /* Declare a variable */

CHGVAR     VAR(&MSG) VALUE('Hello, World!') /* Change variable value */

SNDPGMMSG  MSG(&MSG) /* Send program message */

ENDPGM     /* End of the program */

Explanation of the Code

  • PGM: Marks the beginning of the CL program.
  • DCL VAR(&MSG) TYPE(*CHAR) LEN(50): Declares a variable named &MSG of type character with a length of 50.
  • CHGVAR VAR(&MSG) VALUE('Hello, World!'): Changes the value of the variable &MSG to 'Hello, World!'.
  • SNDPGMMSG MSG(&MSG): Sends the value of &MSG as a program message.
  • ENDPGM: Marks the end of the CL program.

Step 4: Compiling the CL Program

To compile your CL program, use the following command:

CRTCLPGM PGM(MYLIB/MYFIRSTCL) SRCFILE(MYLIB/QCLSRC)

Replace MYLIB with the name of your library.

Step 5: Running the CL Program

To run your compiled CL program, use the following command:

CALL PGM(MYLIB/MYFIRSTCL)

Replace MYLIB with the name of your library.

Expected Output

When you run the program, you should see the message 'Hello, World!' displayed.

Practical Exercise

Exercise 1: Modify the Program

Modify the program to display a different message, such as 'Welcome to CL Programming!'.

Solution

PGM        /* Start of the program */

DCL        VAR(&MSG) TYPE(*CHAR) LEN(50) /* Declare a variable */

CHGVAR     VAR(&MSG) VALUE('Welcome to CL Programming!') /* Change variable value */

SNDPGMMSG  MSG(&MSG) /* Send program message */

ENDPGM     /* End of the program */

Exercise 2: Add Another Variable

Add another variable to the program and display both messages.

Solution

PGM        /* Start of the program */

DCL        VAR(&MSG1) TYPE(*CHAR) LEN(50) /* Declare first variable */
DCL        VAR(&MSG2) TYPE(*CHAR) LEN(50) /* Declare second variable */

CHGVAR     VAR(&MSG1) VALUE('Hello, World!') /* Change first variable value */
CHGVAR     VAR(&MSG2) VALUE('Welcome to CL Programming!') /* Change second variable value */

SNDPGMMSG  MSG(&MSG1) /* Send first program message */
SNDPGMMSG  MSG(&MSG2) /* Send second program message */

ENDPGM     /* End of the program */

Common Mistakes and Tips

  • Syntax Errors: Ensure that all commands and variable declarations are correctly typed.
  • Variable Length: Make sure the length of the variable is sufficient to hold the message.
  • Compilation Errors: Check for any errors during the compilation process and correct them before running the program.

Conclusion

Congratulations! You have successfully written, compiled, and run your first CL program. This foundational knowledge will be crucial as you progress through more complex topics in Control Language. In the next section, we will delve into basic CL commands, which will further enhance your understanding and capabilities in CL programming.

© Copyright 2024. All rights reserved