hot_ice
January 21st, 2002, 04:50 PM
OK, now why am I doing a COBOL tutorial?? I got no idea. It's an old language and many of today's newer languages can do the things it does and even more. So, basically, I'm doing it cos I just finished an assignment on it and it's fresh in my mind so I though I'll just write about it...it won't hurt. Keep in mind, there are many large legacy systems that use COBOL, so it is good to know it. Like for instance today, I had an interview with the National Australia Bank about a placement there and they said their main system is coded in COBOL and they have to use it.
So anyways, enough crap, on with this kinda crappy COBOL tutorial - hopefully some of you will actually try do it...or not... :) (mind you, I only know the basics, so hopefully I can pass them on to you)
OK, first of all, in COBOL you have strict rules on where your code is - ie. what position on a line. (NOTE: This is for COBOL 85)
-each line can only have 80 characters
-only use positions 7 thru to 72
-position 7 is for special purpose - ie. * marks comment
-position 8 thru 11 is for division names, section names, paragraph names, etc
- position 12 thru to 72 is for all other things - the actual code (if statements, variables, etc...)
division names:
-------------------
4 divisions - identification division (used for program ID, author name, date written, etc.)
- environment division (used to state which files - if any - you use)
- data division (use to declare variables)
- procedure division (the actual coding)
ok - let me simply start by giving you code from a sample program
0001 IDENTIFICATION DIVISION.
0002 PROGRAM-ID.
0003 SAMPLE1.
0004
0005 ENVIRONMENT DIVISION.
0006
0007 DATA DIVISION.
0008 WORKING-STORAGE SECTION.
0009 01 USER-NAME PIC X(20).
0010 01 MSG PIC X(17) VALUE 'WELCOME TO COBOL!'.
0011
0012 PROCEDURE DIVISION.
0013 000-GREETING.
0014 DISPLAY 'ENTER YOUR NAME'
0015 ACCEPT USER-NAME
0016 DISPLAY 'HELLO ', USER-NAME, MSG
0017 STOP RUN.
OK, this program simply accepts your name and then prints a welcome message with your name in it.
-the numbers along the left side are the line numbers
-as you can see 'ENVIRONMENT DIVISION' is there, but nothing is under it. I just put it there to show you where it goes, however, if it is not in use, you can exclude it.
-under 'DATA DIVISION' you have 'WORKING-STORAGE SECTION'. This is where all your variables go.
-PIC (PICTURE) defines what the variable is. Here are some examples:
PIC X. --this would be a single character which can store anything (eg. '1', 'W', '%')
PIC 9. --this is a numeric field, which stores one number (eg. 3, 6, 8)
PIC X(10). -- this would store a string 10 chars long (eg. 'HELLO MATE', 'DI&$9DHH3K')
PIC 9(10). --this would store numeric field 10 digits long (eg. 1234567890, 7483909847)
I think you get the point with the PIC.
OK, you'll notice the '.' (full-stops) everywhere. You basically put them at the end of division, section and paragraph names. You also put them at the end of each variable defined. It is confusing, but once you get the hang of it - it'll be sweet.
After the 'PROCEDURE DIVISION' you'll notice '000-GREETING'. This could have been 'MESSAGE' or '345-HELLO'. It is just a paragraph name and I used 000 so that if I had like 10 pages of code and 8 different paragraphs, I can easily find where 000 is, as it would be in order. (for example, next paragraph could be 001-VALIDATE or something).
In the 'PROCEDURE DIVISION' you then have:
'DISPLAY...' - This does what it says - displays 'ENTER YOUR NAME' on the screen.
'ACCEPT...' - This basically is a prompt for the user to enter their name (not difficult is it!!)
...and I'm sure you can understand what the lines below it do.
You'll notice at the end of STOP RUN. (which ends the program) - we have a full-stop. You always have a full-stop when a paragraph finishes. So this ends the 000-GREETING paragraph.
OK, well, there it is. I hope some of you actual take time to look at it. The compiler that I use is RMCOSTER. If you would like it, feel free to contact me and I will inform you on how you can get it. :) My contacts are any of the following:
ICQ: 16488150
AIM: hot ice GLS
e-mail: hot_ice@antionline.org
If you have any questions, feel free to ask or simply post in this thread.
Greg
So anyways, enough crap, on with this kinda crappy COBOL tutorial - hopefully some of you will actually try do it...or not... :) (mind you, I only know the basics, so hopefully I can pass them on to you)
OK, first of all, in COBOL you have strict rules on where your code is - ie. what position on a line. (NOTE: This is for COBOL 85)
-each line can only have 80 characters
-only use positions 7 thru to 72
-position 7 is for special purpose - ie. * marks comment
-position 8 thru 11 is for division names, section names, paragraph names, etc
- position 12 thru to 72 is for all other things - the actual code (if statements, variables, etc...)
division names:
-------------------
4 divisions - identification division (used for program ID, author name, date written, etc.)
- environment division (used to state which files - if any - you use)
- data division (use to declare variables)
- procedure division (the actual coding)
ok - let me simply start by giving you code from a sample program
0001 IDENTIFICATION DIVISION.
0002 PROGRAM-ID.
0003 SAMPLE1.
0004
0005 ENVIRONMENT DIVISION.
0006
0007 DATA DIVISION.
0008 WORKING-STORAGE SECTION.
0009 01 USER-NAME PIC X(20).
0010 01 MSG PIC X(17) VALUE 'WELCOME TO COBOL!'.
0011
0012 PROCEDURE DIVISION.
0013 000-GREETING.
0014 DISPLAY 'ENTER YOUR NAME'
0015 ACCEPT USER-NAME
0016 DISPLAY 'HELLO ', USER-NAME, MSG
0017 STOP RUN.
OK, this program simply accepts your name and then prints a welcome message with your name in it.
-the numbers along the left side are the line numbers
-as you can see 'ENVIRONMENT DIVISION' is there, but nothing is under it. I just put it there to show you where it goes, however, if it is not in use, you can exclude it.
-under 'DATA DIVISION' you have 'WORKING-STORAGE SECTION'. This is where all your variables go.
-PIC (PICTURE) defines what the variable is. Here are some examples:
PIC X. --this would be a single character which can store anything (eg. '1', 'W', '%')
PIC 9. --this is a numeric field, which stores one number (eg. 3, 6, 8)
PIC X(10). -- this would store a string 10 chars long (eg. 'HELLO MATE', 'DI&$9DHH3K')
PIC 9(10). --this would store numeric field 10 digits long (eg. 1234567890, 7483909847)
I think you get the point with the PIC.
OK, you'll notice the '.' (full-stops) everywhere. You basically put them at the end of division, section and paragraph names. You also put them at the end of each variable defined. It is confusing, but once you get the hang of it - it'll be sweet.
After the 'PROCEDURE DIVISION' you'll notice '000-GREETING'. This could have been 'MESSAGE' or '345-HELLO'. It is just a paragraph name and I used 000 so that if I had like 10 pages of code and 8 different paragraphs, I can easily find where 000 is, as it would be in order. (for example, next paragraph could be 001-VALIDATE or something).
In the 'PROCEDURE DIVISION' you then have:
'DISPLAY...' - This does what it says - displays 'ENTER YOUR NAME' on the screen.
'ACCEPT...' - This basically is a prompt for the user to enter their name (not difficult is it!!)
...and I'm sure you can understand what the lines below it do.
You'll notice at the end of STOP RUN. (which ends the program) - we have a full-stop. You always have a full-stop when a paragraph finishes. So this ends the 000-GREETING paragraph.
OK, well, there it is. I hope some of you actual take time to look at it. The compiler that I use is RMCOSTER. If you would like it, feel free to contact me and I will inform you on how you can get it. :) My contacts are any of the following:
ICQ: 16488150
AIM: hot ice GLS
e-mail: hot_ice@antionline.org
If you have any questions, feel free to ask or simply post in this thread.
Greg