In this section, we will explore looping constructs in Control Language (CL). Looping constructs allow you to execute a block of code multiple times, which is essential for tasks that require repetition. We will cover the following topics:

  1. Introduction to Looping Constructs
  2. Types of Loops in CL
  3. Using the DO Loop
  4. Using the DOWHILE Loop
  5. Using the DOUNTIL Loop
  6. Practical Examples
  7. Exercises

  1. Introduction to Looping Constructs

Looping constructs are used to repeat a sequence of instructions until a certain condition is met. This is useful for tasks such as processing records in a file, performing calculations, or automating repetitive tasks.

  1. Types of Loops in CL

CL supports several types of loops:

  • DO Loop: Executes a block of code a specified number of times.
  • DOWHILE Loop: Executes a block of code as long as a specified condition is true.
  • DOUNTIL Loop: Executes a block of code until a specified condition becomes true.

  1. Using the DO Loop

The DO loop is used to execute a block of code a fixed number of times. The syntax is as follows:

DO var = start TO end BY step
    /* Code to be repeated */
ENDDO
  • var: Loop control variable.
  • start: Initial value of the loop control variable.
  • end: Final value of the loop control variable.
  • step: Increment value for the loop control variable.

Example

PGM
    DCL VAR(&I) TYPE(*INT) VALUE(1)
    
    DO VAR(&I) FROM(1) TO(5) BY(1)
        SNDPGMMSG MSG('Iteration ' *CAT &I)
    ENDDO
ENDPGM

In this example, the message "Iteration 1", "Iteration 2", ..., "Iteration 5" will be sent.

  1. Using the DOWHILE Loop

The DOWHILE loop executes a block of code as long as a specified condition is true. The syntax is:

DOWHILE condition
    /* Code to be repeated */
ENDDO

Example

PGM
    DCL VAR(&I) TYPE(*INT) VALUE(1)
    
    DOWHILE COND(&I <= 5)
        SNDPGMMSG MSG('Iteration ' *CAT &I)
        CHGVAR VAR(&I) VALUE(&I + 1)
    ENDDO
ENDPGM

In this example, the message "Iteration 1", "Iteration 2", ..., "Iteration 5" will be sent.

  1. Using the DOUNTIL Loop

The DOUNTIL loop executes a block of code until a specified condition becomes true. The syntax is:

DOUNTIL condition
    /* Code to be repeated */
ENDDO

Example

PGM
    DCL VAR(&I) TYPE(*INT) VALUE(1)
    
    DOUNTIL COND(&I > 5)
        SNDPGMMSG MSG('Iteration ' *CAT &I)
        CHGVAR VAR(&I) VALUE(&I + 1)
    ENDDO
ENDPGM

In this example, the message "Iteration 1", "Iteration 2", ..., "Iteration 5" will be sent.

  1. Practical Examples

Example 1: Summing Numbers

PGM
    DCL VAR(&SUM) TYPE(*INT) VALUE(0)
    DCL VAR(&I) TYPE(*INT) VALUE(1)
    
    DO VAR(&I) FROM(1) TO(10) BY(1)
        CHGVAR VAR(&SUM) VALUE(&SUM + &I)
    ENDDO
    
    SNDPGMMSG MSG('Sum of numbers from 1 to 10 is ' *CAT &SUM)
ENDPGM

Example 2: Processing Records in a File

PGM
    DCLF FILE(MYFILE)
    DCL VAR(&EOF) TYPE(*LGL) VALUE('0')
    
    RCVF
    MONMSG MSGID(CPF0864) EXEC(CHGVAR VAR(&EOF) VALUE('1'))
    
    DOWHILE COND(&EOF = '0')
        /* Process the record */
        RCVF
        MONMSG MSGID(CPF0864) EXEC(CHGVAR VAR(&EOF) VALUE('1'))
    ENDDO
ENDPGM

  1. Exercises

Exercise 1: Print Even Numbers

Write a CL program that prints even numbers from 2 to 20.

Solution

PGM
    DCL VAR(&I) TYPE(*INT) VALUE(2)
    
    DO VAR(&I) FROM(2) TO(20) BY(2)
        SNDPGMMSG MSG('Even number: ' *CAT &I)
    ENDDO
ENDPGM

Exercise 2: Factorial Calculation

Write a CL program that calculates the factorial of a given number.

Solution

PGM
    DCL VAR(&N) TYPE(*INT) VALUE(5)
    DCL VAR(&FACT) TYPE(*INT) VALUE(1)
    DCL VAR(&I) TYPE(*INT) VALUE(1)
    
    DO VAR(&I) FROM(1) TO(&N) BY(1)
        CHGVAR VAR(&FACT) VALUE(&FACT * &I)
    ENDDO
    
    SNDPGMMSG MSG('Factorial of ' *CAT &N *CAT ' is ' *CAT &FACT)
ENDPGM

Conclusion

In this section, we covered the basics of looping constructs in CL, including the DO, DOWHILE, and DOUNTIL loops. We also provided practical examples and exercises to reinforce the concepts. Understanding and using loops effectively is crucial for writing efficient and powerful CL programs. In the next section, we will delve into error handling in CL.

© Copyright 2024. All rights reserved