IDENTIFICATION DIVISION. PROGRAM-ID. VARS. DATA DIVISION. *> working storage defines variables WORKING-STORAGE SECTION. *> define a number with a sign, 3 numbers, a decimal, and then *> two numbers aafter the decimal. by default it should be 0 filled 01 FIRST-VAR PIC S9(3)V9(2). *> do the same thing as above but actually initialize *> to a number -123.45 01 SECOND-VAR PIC S9(3)V9(2) VALUE -123.45. *> defines an alphabetic string and initialize it to abcdef 01 THIRD-VAR PIC A(6) VALUE 'ABCDEF'. *> define an alphanumeric string and initialize it to a121$ 01 FOURTH-VAR PIC X(5) VALUE 'A121$'. *> create a grouped variable 01 GROUP-VAR. 05 SUBVAR-1 PIC 9(3) VALUE 337. *> create 3 alphanumerics, but use less than *> the allocated space for each of them 05 SUBVAR-2 PIC X(15) VALUE 'LALALALA'. 05 SUBVAR-3 PIC X(15) VALUE 'LALALA'. 05 SUBVAR-4 PIC X(15) VALUE 'LALALA'. *> print our variables PROCEDURE DIVISION. DISPLAY "1ST VAR :"FIRST-VAR. DISPLAY "2ND VAR :"SECOND-VAR. DISPLAY "3RD VAR :"THIRD-VAR. DISPLAY "4TH VAR :"FOURTH-VAR. DISPLAY "GROUP VAR :"GROUP-VAR. STOP RUN.
Write, Run & Share COBOL code online using OneCompiler's COBOL online compiler for free. It’s a reliable and accessible playground to practice and run COBOL code with ease. The compiler supports classic COBOL syntax and is great for learning, teaching, and experimenting with business logic programs.
COBOL (Common Business-Oriented Language) is a high-level programming language developed in the 1950s. It is primarily used in business, finance, and administrative systems for companies and governments. COBOL is known for its English-like syntax and is still widely used in legacy enterprise systems.
The following is a simple COBOL program that prints a greeting:
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO.
PROCEDURE DIVISION.
DISPLAY "Hello, OneCompiler!".
STOP RUN.
In COBOL, input is typically handled using the ACCEPT
keyword. Here’s an example that takes user input and prints it back.
IDENTIFICATION DIVISION.
PROGRAM-ID. GREET.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 USER-NAME PIC A(30).
PROCEDURE DIVISION.
DISPLAY "Enter your name: ".
ACCEPT USER-NAME.
DISPLAY "Hello, " USER-NAME "!".
STOP RUN.
COBOL programs are divided into four divisions:
Variables are declared in the DATA DIVISION
using PIC
clauses.
01 AGE PIC 99.
01 NAME PIC A(20).
01 SALARY PIC 9(5)V99.
DISPLAY "Welcome to COBOL!".
ACCEPT USER-INPUT.
IF AGE >= 18
DISPLAY "Eligible to vote."
ELSE
DISPLAY "Not eligible."
END-IF.
PERFORM VARYING I FROM 1 BY 1 UNTIL I > 5
DISPLAY "Count: " I
END-PERFORM.
This guide provides a quick reference to COBOL programming syntax and features. Start coding in COBOL using OneCompiler’s COBOL online compiler today!