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:
- Program Declaration: Defines the program and its attributes.
- Variable Declarations: Declares any variables that will be used in the program.
- Command Statements: Contains the logic and commands to be executed.
- 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
- Open your development environment (e.g., IBM i Access Client Solutions).
- 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
.
- Navigate to the source physical file where you want to store your CL program. For example,
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:
Replace MYLIB
with the name of your library.
Step 5: Running the CL Program
To run your compiled CL program, use the following command:
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.
CL (Control Language) Course
Module 1: Introduction to CL
- What is Control Language?
- Setting Up Your Environment
- Basic Syntax and Structure
- Writing Your First CL Program
Module 2: Basic CL Commands
- Introduction to CL Commands
- File Management Commands
- Job Management Commands
- System Management Commands
Module 3: Variables and Expressions
Module 4: Control Structures
Module 5: Advanced CL Commands
- Advanced File Operations
- Advanced Job Scheduling
- System Configuration Commands
- Security and Permissions