How to Say “Hello, World!” in COBOL

Welcome to this comprehensive guide on how to say “Hello, World!” in COBOL. Whether you are a beginner or just looking to expand your programming language repertoire, we’ll walk you through various ways to greet the world in COBOL, along with tips, examples, and even a touch of regional variation. Let’s get started!

Formal Greetings in COBOL

When it comes to formal greetings in COBOL, there are a few different approaches you can take. Let’s explore them:

Using the DISPLAY Statement

One way to greet the world formally in COBOL is by utilizing the DISPLAY statement. Here’s an example:

IDENTIFICATION DIVISION. PROGRAM-ID. HELLO-WORLD. DATA DIVISION. WORKING-STORAGE SECTION. 01 MESSAGE PIC X(20) VALUE "Hello, World!". PROCEDURE DIVISION. DISPLAY MESSAGE. STOP RUN.

Tip: The DISPLAY statement is used to send output to the terminal, and the STOP RUN statement terminates the program’s execution.

In the above example, we define a variable named MESSAGE in the WORKING-STORAGE SECTION. The PIC X(20) specifies that it’s a field of 20 characters. We then use the DISPLAY statement to output the contents of MESSAGE, which contains “Hello, World!”.

Using the WRITE Statement

Another formal way to greet the world in COBOL is by utilizing the WRITE statement:

IDENTIFICATION DIVISION. PROGRAM-ID. HELLO-WORLD. DATA DIVISION. WORKING-STORAGE SECTION. 01 MESSAGE PIC X(20) VALUE "Hello, World!". PROCEDURE DIVISION. MOVE MESSAGE TO WS-MESSAGE. WRITE WS-MESSAGE. STOP RUN. 

Tip: The MOVE statement is used to copy the value of MESSAGE into the WS-MESSAGE variable, which is then displayed using the WRITE statement.

In this example, we define a variable named MESSAGE and a working-storage variable named WS-MESSAGE. We use the MOVE statement to assign the value of MESSAGE to WS-MESSAGE, and then the WRITE statement is used to display the contents of WS-MESSAGE.

Informal Greetings in COBOL

Now, let’s explore some more relaxed and informal ways to say “Hello, World!” in COBOL:

Using the PERFORM Statement

The PERFORM statement in COBOL allows you to loop through a set of instructions multiple times. Here’s an example of an informal greeting using PERFORM:

IDENTIFICATION DIVISION. PROGRAM-ID. HELLO-WORLD. DATA DIVISION. WORKING-STORAGE SECTION. 01 COUNTER PIC 9(3). PROCEDURE DIVISION. PERFORM VARYING COUNTER FROM 1 BY 1 UNTIL COUNTER > 3 DISPLAY "Hello, World!" END-PERFORM. STOP RUN. 

Tip: The PERFORM statement establishes a loop that repeats until the condition specified after UNTIL is true. In our case, the loop will iterate three times, displaying “Hello, World!” each time.

By utilizing the PERFORM statement, we can repeat the DISPLAY statement for a specified number of times. In this example, it will display “Hello, World!” three times before the program execution stops.

Adding a Touch of Regional Variation

While COBOL is not commonly associated with regional variations, programmers often add a personal touch or flavor to their code. One way to achieve this is through localized greetings:

IDENTIFICATION DIVISION. PROGRAM-ID. HELLO-WORLD. DATA DIVISION. WORKING-STORAGE SECTION. 01 MESSAGE PIC X(60). PROCEDURE DIVISION. MOVE "Hello, World!" TO MESSAGE. DISPLAY MESSAGE. STOP RUN. 

In this example, we’ve increased the message length to 60 characters, allowing for a longer greeting. By doing so, you have the flexibility to tailor your greeting, adding regional variations or playful phrases.

Conclusion

Congratulations! You have now learned several ways to say “Hello, World!” in COBOL, both formally and informally. You’ve seen how to utilize the DISPLAY and WRITE statements, as well as the PERFORM statement for a more dynamic greeting. Additionally, we explored the possibility of adding regional variation by modifying the greeting message. With these examples and tips, you can now confidently incorporate greetings into your COBOL programs. Happy coding!

⭐Share⭐ to appreciate human effort 🙏
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top