The snippets are under the CC-BY-SA license.

Creative Commons Attribution-ShareAlike 3.0

Logo

Programming-Idioms.org

  • The snippets are under the CC-BY-SA license.
  • Please consider keeping a bookmark
  • (instead of printing)
Cobol
1
Print a literal string on standard output
DISPLAY "Hello World!".
2
Loop to execute some code a constant number of times
IDENTIFICATION DIVISION.
PROGRAM-ID. hello 10 times.
PROCEDURE DIVISION.
PERFORM 10 TIMES
   DISPLAY "Hello"
END-PERFORM
STOP RUN.
6
Do something with each item x of the list (or array) items, regardless indexes.
IDENTIFICATION DIVISION.
PROGRAM-ID. list.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LIST.
   03 X OCCURS 5 TIMES
        INDEXED BY i PIC 9.
PROCEDURE  DIVISION.       
    PERFORM VARYING i FROM 1  BY 1 UNTIL i > 5
        DISPLAY X(i)
    END-PERFORM    
STOP RUN.
21
Swap the values of the variables a and b
IDENTIFICATION DIVISION.
PROGRAM-ID. swap.
PROCEDURE DIVISION.
    MOVE a    TO temp
    MOVE b    TO a
    MOVE temp TO b
STOP RUN.
41
Create string t containing the same characters as string s, in reverse order.
Original string s must remain unaltered. Each character must be handled correctly regardless its number of bytes in memory.
IDENTIFICATION DIVISION.
PROGRAM-ID. reverse string.
PROCEDURE DIVISION.
    MOVE FUNCTION REVERSE(s) TO t
STOP RUN.
46
Create the string t consisting of the 5 first characters of the string s.
Make sure that multibyte characters are properly handled.
IDENTIFICATION DIVISION.
PROGRAM-ID. prefix 5.
PROCEDURE DIVISION.
    MOVE s(1:5) TO t 	
STOP RUN.
47
Create string t consisting in the 5 last characters of string s.
Make sure that multibyte characters are properly handled.
IDENTIFICATION DIVISION.
PROGRAM-ID. suffix.
PROCEDURE DIVISION.
    MOVE FUNCTION LENGTH(s) TO len
    COMPUTE pos = (len - 5) + 1
    MOVE    s(pos:) TO t    	 	
STOP RUN.
48
Assign to variable s a string literal consisting in several lines of text, including newlines.
IDENTIFICATION DIVISION.
PROGRAM-ID. multi-line string.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 s    PIC X(20).
01 str  PIC X(5) VALUE 'COBOL'.
01 str1 PIC X(4) VALUE 'RULE'.
01 str2 PIC X(3) VALUE 'THE'.
01 str3 PIC X(5) VALUE 'WORLD'.
PROCEDURE  DIVISION.       
    STRING str ' ' str1 ' ' str2 ' ' str3 
    DELIMITED BY SIZE INTO s
STOP RUN.
50
Write a loop that has no end clause.
IDENTIFICATION DIVISION.
PROGRAM-ID. no end loop.
PROCEDURE DIVISION.
PERFORM UNTIL 1 < 0
   DISPLAY "Something"
END-PERFORM
STOP RUN.
61
Assign to the variable d the current date/time value, in the most standard type.
IDENTIFICATION DIVISION.
PROGRAM-ID. date.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 curr-date.
   03 year      pic 9(4).
   03 month     pic 9(2).
   03 day       pic 9(2).
PROCEDURE DIVISION.
   MOVE FUNCTION CURRENT-DATE TO curr-date
STOP RUN.
78
Execute a block once, then execute it again as long as boolean condition c is true.
IDENTIFICATION DIVISION.
PROGRAM-ID. "do while" loop.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 boolean-c    PIC x.
   88 c-true    PIC x VALUE 't'.
   88 c-false   PIC x VALUE 'f'.
PROCEDURE DIVISION.
    PERFORM WITH TEST AFTER UNTIL c-false
       PERFORM somthing
    END-PERFORM   
STOP RUN.
87
Exit immediately.
If some extra cleanup work is executed by the program runtime (not by the OS itself), describe it.
IDENTIFICATION DIVISION.
PROGRAM-ID. date format.
PROCEDURE DIVISION.
STOP RUN.
96
Set the boolean b to true if string s starts with prefix prefix, false otherwise.
IDENTIFICATION DIVISION.
PROGRAM-ID. prefix.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 BOOLEAN-B     PIC X.
   88 b-TRUE       VALUE "T".
   88 b-FALSE      VALUE "F".
PROCEDURE DIVISION.
    IF s(1:3) = prefix(1:3)
       SET b-TRUE  TO TRUE
    ELSE
       SET b-FALSE TO TRUE
    END-IF 	 	
STOP RUN.
99
Assign to the string x the value of the fields (year, month, day) of the date d, in format YYYY-MM-DD.
IDENTIFICATION DIVISION.
PROGRAM-ID. date format.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 curr-date.
   03 yyyy      pic 9(4).
   03 mm        pic 9(2).
   03 dd        pic 9(2).
01 d.
   03 year      pic 9(4). 
   03 FILLER    pic x VALUE '-'.
   03 month     pic 99. 
   03 FILLER    pic x VALUE '-'.
   03 day       pic 99. 
PROCEDURE DIVISION.
   MOVE FUNCTION CURRENT-DATE TO curr-date
   MOVE yyyy to year
   MOVE mm   to month
   MOVE dd   to day
   DISPLAY d 
STOP RUN.
110
Set the boolean blank to true if the string s is empty, or null, or contains only whitespace ; false otherwise.
IDENTIFICATION DIVISION.
PROGRAM-ID. blank string.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 BOOLEAN-BLANK     PIC X.
   88 BLANK          VALUE "T".
   88 NOT-BLANK      VALUE "F".
PROCEDURE DIVISION.
    IF s > SPACES
       SET BLANK     TO TRUE
    ELSE
       SET NOT-BLANK TO TRUE
    END-IF 	 	
STOP RUN.
120
Read an integer value from the standard input into the variable n
IDENTIFICATION DIVISION.
PROGRAM-ID. stdin.
PROCEDURE DIVISION.
    ACCEPT n
STOP RUN.
131
Execute f1 if condition c1 is true, or else f2 if condition c2 is true, or else f3 if condition c3 is true.
Don't evaluate a condition when a previous condition was true.
IDENTIFICATION DIVISION.
PROGRAM-ID. condition.
PROCEDURE DIVISION.
    EVALUATE TRUE
	WHEN c1
	   PERFORM f1
	WHEN c2
	   PERFORM f2
	WHEN c3
	   PERFORM f3
    END-EVALUATE  
STOP RUN.
169
Assign to the integer n the number of characters of the string s.
Make sure that multibyte characters are properly handled.
n can be different from the number of bytes of s.
IDENTIFICATION DIVISION.
PROGRAM-ID. length.
PROCEDURE DIVISION.
   MOVE FUNCTION LENGTH(s) TO n
STOP RUN.
206
Execute different procedures foo, bar, baz and barfl if the string str contains the name of the respective procedure. Do it in a way natural to the language.
IDENTIFICATION DIVISION.
PROGRAM-ID. reverse string.
PROCEDURE DIVISION.
    EVALUATE str
       WHEN foo
	  PERFORM foo
       WHEN bar
          PERFORM bar
       WHEN baz
	  PERFORM baz 
       WHEN barfl
	  PERFORM barfl
       WHEN OTHER
	  CONTINUE
    END-EVALUATE.	
STOP RUN.