BudiBadu Logo
Samplebadu

COBOL by Example: Paragraphs

COBOL 2002

Understanding paragraphs as the smallest named units of code in the PROCEDURE DIVISION consisting of user-defined names and executable sentences, implementing structured programming with PERFORM statements to execute paragraphs and return control, avoiding unstructured GO TO statements that create spaghetti code, and managing sequential paragraph execution with proper termination to prevent fall-through behavior.

Code

       IDENTIFICATION DIVISION.
       PROGRAM-ID. PARA-DEMO.
       
       PROCEDURE DIVISION.
       MAIN-PARA.
           DISPLAY "Start of Program".
           PERFORM SECOND-PARA.
           DISPLAY "End of Program".
           STOP RUN.
           
       SECOND-PARA.
           DISPLAY "Inside Second Paragraph".

Explanation

A PARAGRAPH is the smallest named unit of code within the Procedure Division. It consists of a user-defined name followed by a period, and then one or more sentences (instructions). Paragraphs are used to break down the program logic into small, manageable, and reusable chunks that can be called from other parts of the program.

Paragraphs can be executed using the PERFORM statement, which works like a function call. You can also use GO TO to jump to a paragraph, but this is generally discouraged in modern programming as it leads to spaghetti code that is hard to follow. PERFORM ensures structured programming by returning control to the caller after the paragraph finishes.

Just like sections, paragraphs execute sequentially. In this example, if you don't explicitly stop execution (e.g., with STOP RUN or EXIT PROGRAM), the code will continue to the next paragraph automatically. This fall-through behavior is a common source of bugs for beginners.

Code Breakdown

5
MAIN-PARA. This is a paragraph label. It must start in "Area A" (columns 8-11). The code inside it starts in "Area B" (column 12+).
7
PERFORM SECOND-PARA. This instruction jumps to SECOND-PARA, executes all its statements, and then returns to line 8.
8
DISPLAY "End of Program". This line runs ONLY after SECOND-PARA has completed and returned.
9
STOP RUN. This terminates the program. If we removed this line, the program would continue to line 11 and execute SECOND-PARA again!
11
SECOND-PARA. The definition of our reusable code block. It ends when a new paragraph/section starts or the file ends.