Buy your COBOL t-shirt here... (opens in a new window)

|   HOME   |    COBOL basics   |    The Four Divisions   |    Defining Data Part 1   |    Defining Data Part 2   |    Commands and logic   |    File handling   |    Debugging COBOL   |

10. Sample COBOL code

                 10.1 Add line number program
                 10.2 Refresh line numbers program
                 10.3 League table program
                 10.4 Calculate prime numbers program
                 10.5 Create INDEXED file program
                 10.6 Read INDEXED file program



The sample code here was written while learning COBOL so they aren't particularly well structured. Also, they are not the usual type of COBOL program that you would normally come across. COBOL is more likely written for business applications such as payroll programs or stock control etc... Hopefully they might give an indication of how COBOL works.

 
10.1 Add line numbers program

This program is designed to add line numbers to COBOL code that has been typed into a text editor (e.g. Notepad) in the following format:

   :
PROCEDURE DIVISION.

MAIN-PARAGRAPH.
      MOVE X TO Y
*the comment asterisk will be placed in position 7
/as will the page break solidus
      IF Y > Z THEN 
          ADD Z TO X
          MOVE X TO Z
      ELSE DISPLAY 'The hypen for continuing a string
-              'onto the next line also goes into position 7'
      END-IF
*all other text is placed from position 8 
*so you still need to indent where required
STOP RUN.

*lastly, there is a limit of about 
*70 characters per line (from position 8)

The text file containing COBOL code as above should be call named input.txt. Following execution, the program will produce a new file called output.cob although it will still be a simply text file, but can be compiled. The output.cob file for the above code would be:
   :
000010 PROCEDURE DIVISION.
000020
000030 MAIN-PARAGRAPH.
000040     MOVE X TO Y
000050*the comment asterisk will be placed in position 7
000060/as will the page break solidus
000070     IF Y > Z THEN 
000080        ADD Z TO X
000090        MOVE X TO Z
000100     ELSE DISPLAY 'The hypen for continuing a string
000110-              'onto the next line also goes into position 7'
000120     END-IF
000130*all other text is placed from position 8 
000140*so you still need to indent where required
000150     STOP RUN.
000160
000170*lastly, there is a limit of about 
000180*70 characters per line (from position 8)


 
10.2 Refresh line numbers program

This program is designed to refresh COBOL code line numbers following editing that would result in uneven line number increases (or even no line number at all) where lines have been inserted or deleted.

 
10.3 League table program

This program is designed to update a football league table and print out a table when any scores have been added. The diplay prompts the user to input the score from a game. The points for each team involved are updated, as are the goals for, against and difference. The program will search the data file and update the relevant team record. When score input is complete, the program then sorts the data into a temporary file before printing out an updated league table. An OUTPUT PROCEDURE could have been used instead of producing a temporary sorted file.

This program would probably benefit from using an indexed file for the team records rather than searching the sequentail file, as done here.

This code is written for the 1999-2000 season of the English FA Premiership. The team data is stored on a sequential file in alphabetical order. If you wish to download a copy of this data file (with mostly fictional scores etc..) click here and a better program description click here.

 
10.4 Calculate prime numbers program

This is a little program that calulates prime numbers. You are prompted to enter a number (up to 1999) and the program will produce a file, 'PRIME-NO.TXT, which contains a table of all prime numbers up to the value entered.

 
10.5 Create INDEXED file program

This program takes a line sequential record file and converts it to an indexed file. The records must contain a unique key field that is in strict ascending order. The input file (from a text editor) should be called 'LINESEQFILE.TXT'. The program output will be 'INDEXEDFILE.DAT'. You can change these in the ENVIRONMENT DIVISION if you want.

 
10.6 Read INDEXED file program

This program allows you to view the contents of an indexed file by generating a line sequential file of the original indexed file. If you tried to open an indexed file with a text editor you would just see gibberish. The input file for this program is 'INDEXEDFILE.DAT' giving an output text file called READFILE.TXT. Again, you can change these in the ENVIRONMENT DIVISION if you wish.




1. Getting started 7. File handling
2. COBOL basics 8. Debugging COBOL
3. The Four Divisions 9. Useful links
4. Defining Data Part 1 10. Sample Code
5. Defining Data Part 2 11. Feedback
6. Commands and logic 12. Quick reference
   Awards    HOME
Copyright Timothy R P Brown 2002