Author | Title | Year |
---|---|---|
Miss Google. | Cobol tutorial for Beginners | Cobol Essential Training,3H49 | 2022. |
Are you ready to get started?
COBOL is a procedural language.
- Environment: Windows
- WSL: Windows Subsystem for Linux
- Linux: Debian
- IDE: VSCode (WSL extention + Cobol extensions)
- Compiler: GNUCobol
What is JCL?
JCL, Job Control Language- the scripting language for running COBOL programs
- this course won't cover this because we're not using a mainframe
Obtain input from the user
- Variables are declared in the working-storage section of the data division.
- Strings are concatenated using "," then the name of the variable.
COBOL naming standards
- Hyphen is the only special character allowed in user defined names
- WS is used to prefix variables for working storage
- DET is used to prefix variables used to write to files
How to define types of data
Variables are defined in cobol in the following way:
- level number
- data name
- picture clause
- Value (optional)
Level Numbers
- 01 Field description or group level items
- 02-49 elementary items
- 66 reserved for the rename clause
- 77 items that cannot be subdivided
- 88 condition name entities
- "HIGH-VALUES" is often used to denote the end of a file.
- "FILLER" means something that isn't used anywhere else.
Comp-3 data types
comp-3 in cobol is a "packed decimal". This compresses numbers to half their size.
This tells computers to use decimal arithmatic and not floating point.
comp-3 is declared immediately after the pic clause
Literal and figurative constants
Numeric literal can have 18 digits
- can have a decimal
figurative constants can be things like "high-values", "zeroes", etc.
Editing characters for writing reports
- Z suppresses zeroes
- , prints a comma if there is a significant figure to the left
- * is called "check protection", and it keeps a symbol between a fixed dollar sign and actual values.
Introducing to verbs
- open - opens
- initialize - sets numbers to 0 and letters to spaces
- accept - get data from computer or user
- move - moves information from one data field to another
some verbs have ending verbs: read/end-read, if/end-if
Computational verbs
Verbs that perform calculations
ADD add digits
Suppose A is 5, B is 7, C is 1
- "ADD A TO B" adds 5 to B, changing B to 12
- "ADD A TO BE GIVING C" adds 5 and 7, changing C to 12
- "ADD A B TO C" adds 5 and 7 to C, changing C to 13
- "ADD A TO B C" adds 5 to B giving 12 & adds 5 to C giving 6.
SUBTRACT performs subtraction
A 5, B 7, C 20, D 10
- "SUBTRACT A FROM B" is B-5, giving 2
- "SUBTRACT A B FROM C" is C-5-7, giving 8
- "SUBTRACT AB FROM C GIVING D" is C-5-7, storing the 8 in D
MULTIPLY
A 5, B 7, C 20
- "MULTIPLY A BY B GIVING C" 5*7 = C (35)
- "MULTIPLY A BY B" multiplies A*7, changing A to 25
- "MULTIPLY A BY 3 GIVING B C" multiplies A*5, changing B and C to 15
DIVIDE
A 5, B 7, C 20
- "DIVIDE 2 INTO C" is c/2, giving 10
- "DIVIDE 2 INTO C GIVING B" is c/2, changing B to 10
- "DIVIDE 3 INTO A GIVING B REMAINDER C" is modulo division. A/2 equals 1 R2, so B becomes 1 and C becomes 2
COMPUTE
Allows you to do math using operators
classic method
- MULTIPLY A * A GIVING A-SQUARED
- MULTIPLY B * B GIVING B-SQUARED
- ADD A-SQUARED TO B-SQUARED GIVING C-SQUARED
- COMPUTE C = (C-SQUARED)**.5
pure compute method
- COMPUTE C = (A * A + B * B)**.5
compute statements always begin with the resulting variable set equal to the desired equation.
Conditional expressions
IF, END-IF
IF, ELSE, END-IF
Nested IF statements
can test for things:
- IS NUMERIC
- IS ALPHABETIC, IS ALPHABETIC-UPPER, IS ALPHABETIC-LOWER
- IS POSITIVE, IS NEGATIVE
- AND, OR, NOT
COMPARE
- IS GREATER THAN (>), IS LESS THAN (<)
- IS EQUAL TO (=)
- IS NOT
- IS GREATER THAN OR EQUAL TO (>=), IS LESS THAN OR EQUAL TO (<=)
EVALUATE
- EVALUATE, END-EVALUATE - basically a switch statement
Perform statement
Cobol is sequential. It starts at the beginning and executes commands until told otherwise.
The GO TO command has a limited use case, generally only for error handling, to close the files and end the program.
Perform times
"PERFORM __ UNTIL __" lets you define loops.