Variables in COBOL
In COBOL, variables are explicitly declared in the DATA DIVISION and used throughout the program. Here’s an example demonstrating variable declaration and usage:
In COBOL, variables are declared in the DATA DIVISION, specifically in the WORKING-STORAGE SECTION for this example.
The PIC
clause defines the type and size of the variable:
PIC X(7)
declares a string of 7 characters.PIC 9(4)
declares a numeric field of 4 digits.PIC X
declares a single character.
The VALUE
clause is used to initialize variables:
You can declare multiple variables, each on its own line:
COBOL will use the type specified in the PIC
clause:
Variables declared without a corresponding initialization are automatically initialized. For numeric fields, the default is zero:
To declare and initialize a variable in COBOL:
In the PROCEDURE DIVISION, we use the DISPLAY
verb to output the values of our variables:
To run this COBOL program, you would typically compile it and then execute the resulting program. The exact commands may vary depending on your COBOL compiler, but it might look something like this:
This demonstrates basic variable declaration and usage in COBOL. Remember that COBOL has a very different structure and syntax compared to more modern languages, but the concepts of variable declaration and initialization are still present.