Author: Saim Khalid

  • WORKING STORAGE SECTION and LINKAGE SECTION?

    WORKING-STORAGE: Declares variables for the current program.

    LINKAGE SECTION: Declares variables that come from another program or JCL (passed parameters).

  • PERFORM and CALL?

    • PERFORM → used for executing a paragraph or section within the same program (like a loop or subroutine).
    • CALL → used for calling an external program or subprogram.

    Example:

    PERFORM CALC-PARA.      *> internal call
    
    CALL 'SUBPROG' USING VAR1.  *> external program call
    
  • What is PIC clause in COBOL?

    • PIC stands for Picture Clause → defines the type & size of a variable.

    Example:

    01 NAME  PIC A(10).    *> 10 characters (Alphabetic)
    01 AGE   PIC 9(3).     *> 3-digit number
    01 SALARY PIC 9(5)V99. *> 5 digits + 2 decimal places
    
  • COMP and COMP-3?

    • COMP (Binary Storage): Data stored in pure binary format. Faster for arithmetic operations.
    • COMP-3 (Packed Decimal): Data stored in packed format (2 digits per byte). Saves space, often used in financial apps.

    Example:

    01 A  PIC 9(4) COMP.     *> Binary storage
    01 B  PIC 9(4) COMP-3.  *> Packed decimal storage
  • divisions in COBOL program?

    COBOL program is divided into 4 divisions:

    1. IDENTIFICATION DIVISION – program info (name, author).
    2. ENVIRONMENT DIVISION – environment details (input-output devices).
    3. DATA DIVISION – data & variables declaration.
    4. PROCEDURE DIVISION – actual logic/code.

    Example:

    IDENTIFICATION DIVISION.
    PROGRAM-ID. SAMPLE.
    DATA DIVISION.
    WORKING-STORAGE SECTION.
    01 NUM PIC 9(3).
    PROCEDURE DIVISION.
    
    DISPLAY "HELLO".
    STOP RUN.

  • Loop (Perform Until)

      IDENTIFICATION DIVISION.
       PROGRAM-ID. LOOP-DEMO.
    
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 COUNTER    PIC 9(2) VALUE 1.
    
       PROCEDURE DIVISION.
    
       PERFORM UNTIL COUNTER > 5
           DISPLAY "COUNTER = " COUNTER
           ADD 1 TO COUNTER
       END-PERFORM.
       STOP RUN.</code></pre>

    Explanation:

    PERFORM UNTIL → loop continues until condition is met.

    COUNTER → starts at 1, increments until 5.

    Output:

    ini


    COUNTER = 1
    COUNTER = 2
    COUNTER = 3
    COUNTER = 4
    COUNTER = 5

  • Simple IF ELSE

    IDENTIFICATION DIVISION.
    PROGRAM-ID. CHECK-NUMBER.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 NUM     PIC 9(3).
    
       PROCEDURE DIVISION.
    
       DISPLAY "ENTER A NUMBER: ".
       ACCEPT NUM.
       IF NUM &gt; 100
           DISPLAY "NUMBER IS GREATER THAN 100"
       ELSE
           DISPLAY "NUMBER IS 100 OR LESS".
       STOP RUN.</code></pre>

    Explanation:

    IF NUM > 100 → checks condition.

    DISPLAY → shows different messages based on condition.

  • User Input and Display

       IDENTIFICATION DIVISION.
       PROGRAM-ID. USER-INPUT.
    
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 USER-NAME   PIC A(20).
    
       PROCEDURE DIVISION.
    
       DISPLAY "ENTER YOUR NAME: ".
       ACCEPT USER-NAME.
       DISPLAY "HELLO, " USER-NAME.
       STOP RUN.</code></pre>

    Explanation:

    PIC A(20) → allows up to 20 alphabetic characters.

    ACCEPT USER-NAME → takes input from user.

    DISPLAY → prints greeting with input.

  • Add Two Numbers

       IDENTIFICATION DIVISION.
       PROGRAM-ID. ADD-NUMBERS.
    
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 NUM1     PIC 9(3) VALUE 25.
       01 NUM2     PIC 9(3) VALUE 30.
       01 SUM      PIC 9(4).
    
       PROCEDURE DIVISION.
    
       ADD NUM1 TO NUM2 GIVING SUM.
       DISPLAY "THE SUM IS: " SUM.
       STOP RUN.</code></pre>

    Explanation:

    WORKING-STORAGE SECTION → used to declare variables.

    01 NUM1 PIC 9(3) → variable with 3 digits (e.g., 025).

    ADD NUM1 TO NUM2 GIVING SUM → performs addition.

    DISPLAY → shows result.

    Output:

    THE SUM IS: 55

  • Hello World Program

       IDENTIFICATION DIVISION.
       PROGRAM-ID. HELLO-WORLD.
       PROCEDURE DIVISION.
    
       DISPLAY "HELLO, WORLD!".
       STOP RUN.</code></pre>

    Explanation:

    IDENTIFICATION DIVISION → declares the program name.

    PROGRAM-ID → the name of the program (HELLO-WORLD).

    PROCEDURE DIVISION → instructions.

    DISPLAY → prints output on screen.

    STOP RUN → ends the program.