Control Structures in COBOL

When it comes to programming in COBOL, mastering control structures is vital for effective flow control. Control structures allow you to direct the execution path of your program based on certain conditions and iterative processes. This article will delve into the key control structures in COBOL, focusing on conditional statements and loops.

Conditional Statements

Conditional statements in COBOL enable you to make decisions in your programs based on specified conditions. The primary conditional structures you'll encounter are the IF statement, EVALUATE statement, and the use of PERFORM for condition checking. Let’s explore each of these in detail.

The IF Statement

The IF statement is the most straightforward way to implement conditional logic in COBOL. It checks a condition and executes code blocks based on whether that condition is true or false.

Syntax:

IF condition
    statement-1
    [ELSE
        statement-2]
END-IF.

Example:

IF salary > 50000
    DISPLAY 'High Salary'
ELSE
    DISPLAY 'Salary is within range'
END-IF.

In this example, if the salary variable is greater than 50,000, the program displays "High Salary." Otherwise, it will show "Salary is within range." The END-IF statement signifies the end of the conditional logic.

Nested IF Statements

You can also nest IF statements within one another to evaluate multiple conditions.

Example:

IF salary > 50000
    DISPLAY 'High Salary'
ELSE
    IF salary < 30000
        DISPLAY 'Low Salary'
    ELSE
        DISPLAY 'Average Salary'
    END-IF
END-IF.

In this example, if the salary is above 50,000, it displays "High Salary." If it’s below 30,000, it displays "Low Salary." If it falls in between, it shows "Average Salary."

The EVALUATE Statement

The EVALUATE statement is another powerful control structure. It functions similarly to a switch-case statement found in other programming languages, allowing for cleaner handling of multiple conditions.

Syntax:

EVALUATE expression
    WHEN condition-1
        statement-1
    WHEN condition-2
        statement-2
    ...
    WHEN OTHER
        statement-n
END-EVALUATE.

Example:

EVALUATE grade
    WHEN 'A'
        DISPLAY 'Excellent!'
    WHEN 'B'
        DISPLAY 'Well Done!'
    WHEN 'C'
        DISPLAY 'Satisfactory'
    WHEN OTHER
        DISPLAY 'Needs Improvement'
END-EVALUATE.

Here, the EVALUATE statement checks the value of grade and displays a corresponding message. This approach enhances readability, especially with many conditions to evaluate.

Looping Structures

Loops in COBOL allow you to execute a block of code repeatedly based on certain conditions. The primary looping structures available are the PERFORM statement, PERFORM UNTIL, and PERFORM VARYING.

The PERFORM Statement

The PERFORM statement is used to execute a paragraph or a section of code. This can effectively create subroutines within your program.

Syntax:

PERFORM paragraph-name.

Example:

PROCEDURE DIVISION.
MAIN-LOGIC.
    PERFORM DISPLAY-MESSAGE.
    EXIT PROGRAM.

DISPLAY-MESSAGE.
    DISPLAY 'Hello, COBOL World!'.

In this example, the PERFORM statement calls the DISPLAY-MESSAGE paragraph, which displays a message.

PERFORM UNTIL Loop

The PERFORM UNTIL loop continues executing the statements within it until a specified condition becomes true.

Syntax:

PERFORM UNTIL condition
    statement-1
    statement-2
    ...
END-PERFORM.

Example:

MOVE 0 TO counter.
PERFORM UNTIL counter >= 10
    DISPLAY 'Counter: ' counter
    ADD 1 TO counter
END-PERFORM.

In this example, counter starts at 0. The loop continues to display the counter's value and increments it until it reaches or exceeds 10.

PERFORM VARYING Loop

The PERFORM VARYING loop is another common looping structure. It allows you to specify both the initial value and the incrementing condition in a single statement.

Syntax:

PERFORM VARYING index FROM start-value BY increment
    UNTIL condition
    statement-1
END-PERFORM.

Example:

PERFORM VARYING index FROM 1 BY 1
    UNTIL index > 5
    DISPLAY 'Value of index: ' index
END-PERFORM.

In this case, the loop iterates starting from 1, increasing the index by 1 with each iteration, and continues until index exceeds 5. This structure is concise and often preferred for counting operations.

Combining Conditional Statements and Loops

You can also combine conditional statements with loops to create more complex flows. For example, using an IF statement inside a loop can help manage operations selectively.

Example:

MOVE 0 TO counter.
PERFORM UNTIL counter >= 10
    ADD 1 TO counter
    IF counter MOD 2 = 0
        DISPLAY counter ' is even.'
    ELSE
        DISPLAY counter ' is odd.'
    END-IF
END-PERFORM.

Here, the loop counts from 1 to 10, using a conditional check to determine whether the counter holds an even or odd value.

Conclusion

Understanding control structures like conditional statements and loops in COBOL is essential for any programmer aiming to write efficient and effective code. The IF, EVALUATE, and looping structures such as PERFORM UNTIL and PERFORM VARYING provide powerful tools for controlling program flow and enabling dynamic decision-making based on specific conditions.

With the use of these structures, you can create programs that are not only functional but also maintainable and clear to other developers. By leveraging COBOL's robust control flow capabilities, you're empowered to tackle complex logic and produce cleaner code.

As you continue your journey in COBOL programming, keep experimenting with these control structures to master the art of flow control! Happy coding!