Skip to content

Commit f7796de

Browse files
authored
Merge pull request #334 from oathar/lab2
Add Lab: Case Sensitive Nature of COBOL Conditions (Lab 9)
2 parents 96a9cee + e1dc345 commit f7796de

File tree

5 files changed

+265
-0
lines changed

5 files changed

+265
-0
lines changed

COBOL Programming Course #2 - Learning COBOL/COBOL Programming Course #2 - Learning COBOL.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,6 +2773,77 @@ This exercise develops skills in COBOL conditional processing, program structure
27732773

27742774
**Need Help?** If you encounter difficulties, you can refer to the complete solution code `CBLC1.cobol` available in VS Code Zowe Explorer or find it on [GitHub](https://github.com/openmainframeproject/cobol-programming-course/tree/master/COBOL%20Programming%20Course%20%232%20-%20Learning%20COBOL/Labs/cbl) repository.
27752775

2776+
# Case Sensitive Nature of COBOL Conditions
2777+
2778+
COBOL syntax is not case-sensitive for keywords and identifiers, but string literal comparisons in conditional expressions ARE case-sensitive. This distinction between language syntax and data comparisons can lead to programming errors that cause issues when your program interacts with:
2779+
2780+
- **External input data**, such as text files or database records
2781+
- **Conditional expressions** that depend on exact string matches
2782+
- **Third-party systems** that expect a specific case format
2783+
2784+
## Why It Matters
2785+
2786+
The most dangerous aspect of case sensitivity errors is that they often go unnoticed during initial testing. Programs **compile successfully**, **run without errors**, but **produce incorrect results**. These kinds of "silent failures" can persist in production systems for long periods, potentially resulting in:
2787+
2788+
- Inaccurate or misleading reports
2789+
- Logic failures (e.g., skipping conditional branches)
2790+
- Reports that appear complete but miss values
2791+
- Data corruption or misrouting
2792+
2793+
### Real-World Example
2794+
2795+
Modern COBOL treats statements like:
2796+
`MOVE MY-NUMBER TO YOUR-NUMBER`
2797+
and
2798+
`Move my-Number to your-Number` as **equivalent** because keywords and identifiers are case-insensitive.
2799+
2800+
However, **COBOL conditions** are case-sensitive when checking **string values**:
2801+
2802+
`IF USA-STATE = 'new York'` = Fails, mismatch due to case
2803+
2804+
`IF USA-STATE = 'New York'` = Succeeds, case matches
2805+
2806+
The comparison **fails silently** COBOL won't throw an error, but the result won't be as expected.
2807+
2808+
## Lab
2809+
2810+
**Case Sensitivity in Data Comparison:**
2811+
Understand how case sensitivity in COBOL string comparisons can cause logical errors and learn to identify and fix these issues.
2812+
2813+
**Scenario:** You'll investigate why a program designed to count "New York" clients returns zero when New York clients actually exist in the input data file (id.DATA).
2814+
2815+
**Required Files:**
2816+
2817+
- `CBL006A.cobol` - COBOL program source
2818+
- `CBL006AJ.jcl` - JCL job to compile and execute
2819+
- `id.DATA` - Input data file
2820+
2821+
All files are available in your VS Code Zowe Explorer.
2822+
2823+
### Instructions
2824+
2825+
1. Open `CBL006A.cobol` from your VS Code Zowe Explorer.
2826+
2827+
This program reads account records and counts how many clients are from `"New York"`.
2828+
2829+
2. Submit the JCL job `CBL006AJ.jcl`. View the job output from the JOBS section.
2830+
- Confirm that no syntax or runtime errors occurred.
2831+
- Now carefully read the final line of the report. `New York Clients = 000`
2832+
2833+
![](Images/image071.png)
2834+
2835+
Ask yourself: *Is this the number of New York clients you expected?*
2836+
2837+
3. Based on your understanding, where do you think the bug lies?
2838+
- Consider the case format of both the **comparison string** and the **actual data** from the file (id.DATA).
2839+
4. Go and update the source code `CBL006A.cobol` .
2840+
- Modify the string literal in the IF condition to match the exact case format found in the data file
2841+
5. Save your changes to `CBL006A.cobol`
2842+
6. Recompile and resubmit the job `CBL006AJ.jcl`
2843+
7. Verify that the report now correctly shows: `New York Clients = 005`
2844+
2845+
![](Images/image072.png)
2846+
27762847
\newpage
27772848

27782849
# Arithmetic expressions
57.6 KB
Loading
51.3 KB
Loading
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
*-----------------------
2+
IDENTIFICATION DIVISION.
3+
*-----------------------
4+
PROGRAM-ID. CBL006A
5+
AUTHOR. Otto B. Boolean.
6+
*--------------------
7+
ENVIRONMENT DIVISION.
8+
*--------------------
9+
INPUT-OUTPUT SECTION.
10+
FILE-CONTROL.
11+
SELECT PRINT-LINE ASSIGN TO PRTLINE.
12+
SELECT ACCT-REC ASSIGN TO ACCTREC.
13+
*-------------
14+
DATA DIVISION.
15+
*-------------
16+
FILE SECTION.
17+
FD PRINT-LINE RECORDING MODE F.
18+
01 PRINT-REC.
19+
05 ACCT-NO-O PIC X(8).
20+
05 FILLER PIC X(02) VALUE SPACES.
21+
05 LAST-NAME-O PIC X(20).
22+
05 FILLER PIC X(02) VALUE SPACES.
23+
05 ACCT-LIMIT-O PIC $$,$$$,$$9.99.
24+
05 FILLER PIC X(02) VALUE SPACES.
25+
05 ACCT-BALANCE-O PIC $$,$$$,$$9.99.
26+
05 FILLER PIC X(02) VALUE SPACES.
27+
*
28+
FD ACCT-REC RECORDING MODE F.
29+
01 ACCT-FIELDS.
30+
05 ACCT-NO PIC X(8).
31+
05 ACCT-LIMIT PIC S9(7)V99 COMP-3.
32+
05 ACCT-BALANCE PIC S9(7)V99 COMP-3.
33+
05 LAST-NAME PIC X(20).
34+
05 FIRST-NAME PIC X(15).
35+
05 CLIENT-ADDR.
36+
10 STREET-ADDR PIC X(25).
37+
10 CITY-COUNTY PIC X(20).
38+
10 USA-STATE PIC X(15).
39+
05 RESERVED PIC X(7).
40+
05 COMMENTS PIC X(50).
41+
*
42+
WORKING-STORAGE SECTION.
43+
01 FLAGS.
44+
05 LASTREC PIC X VALUE SPACE.
45+
*
46+
* CHANGE 1: Modified variable structure to count New York clients
47+
* Original counted Virginia clients - now counts New York clients
48+
01 CLIENTS-PER-STATE.
49+
05 FILLER PIC X(19) VALUE
50+
'New York Clients = '.
51+
05 NEWYORK-CLIENTS PIC 9(3) VALUE ZERO.
52+
05 FILLER PIC X(59) VALUE SPACES.
53+
*
54+
01 HEADER-1.
55+
05 FILLER PIC X(20) VALUE 'Financial Report for'.
56+
05 FILLER PIC X(60) VALUE SPACES.
57+
*
58+
01 HEADER-2.
59+
05 FILLER PIC X(05) VALUE 'Year '.
60+
05 HDR-YR PIC 9(04).
61+
05 FILLER PIC X(02) VALUE SPACES.
62+
05 FILLER PIC X(06) VALUE 'Month '.
63+
05 HDR-MO PIC X(02).
64+
05 FILLER PIC X(02) VALUE SPACES.
65+
05 FILLER PIC X(04) VALUE 'Day '.
66+
05 HDR-DAY PIC X(02).
67+
05 FILLER PIC X(56) VALUE SPACES.
68+
*
69+
01 HEADER-3.
70+
05 FILLER PIC X(08) VALUE 'Account '.
71+
05 FILLER PIC X(02) VALUE SPACES.
72+
05 FILLER PIC X(10) VALUE 'Last Name '.
73+
05 FILLER PIC X(15) VALUE SPACES.
74+
05 FILLER PIC X(06) VALUE 'Limit '.
75+
05 FILLER PIC X(06) VALUE SPACES.
76+
05 FILLER PIC X(08) VALUE 'Balance '.
77+
05 FILLER PIC X(40) VALUE SPACES.
78+
*
79+
01 HEADER-4.
80+
05 FILLER PIC X(08) VALUE '--------'.
81+
05 FILLER PIC X(02) VALUE SPACES.
82+
05 FILLER PIC X(10) VALUE '----------'.
83+
05 FILLER PIC X(15) VALUE SPACES.
84+
05 FILLER PIC X(10) VALUE '----------'.
85+
05 FILLER PIC X(02) VALUE SPACES.
86+
05 FILLER PIC X(13) VALUE '-------------'.
87+
05 FILLER PIC X(40) VALUE SPACES.
88+
*
89+
01 WS-CURRENT-DATE-DATA.
90+
05 WS-CURRENT-DATE.
91+
10 WS-CURRENT-YEAR PIC 9(04).
92+
10 WS-CURRENT-MONTH PIC 9(02).
93+
10 WS-CURRENT-DAY PIC 9(02).
94+
05 WS-CURRENT-TIME.
95+
10 WS-CURRENT-HOUR PIC 9(02).
96+
10 WS-CURRENT-MINUTE PIC 9(02).
97+
10 WS-CURRENT-SECOND PIC 9(02).
98+
10 WS-CURRENT-CENTISECOND PIC 9(02).
99+
* This data layout is organized according to the ouput
100+
* format of the FUNCTION CURRENT-DATE.
101+
*
102+
*------------------
103+
PROCEDURE DIVISION.
104+
*------------------
105+
OPEN-FILES.
106+
OPEN INPUT ACCT-REC.
107+
OPEN OUTPUT PRINT-LINE.
108+
*
109+
WRITE-HEADERS.
110+
MOVE FUNCTION CURRENT-DATE TO WS-CURRENT-DATE-DATA.
111+
* The CURRENT-DATE function returns an alphanumeric value
112+
* that represents the calendar date and time of day
113+
* provided by the system on which the function is
114+
* evaluated.
115+
MOVE WS-CURRENT-YEAR TO HDR-YR.
116+
MOVE WS-CURRENT-MONTH TO HDR-MO.
117+
MOVE WS-CURRENT-DAY TO HDR-DAY.
118+
WRITE PRINT-REC FROM HEADER-1.
119+
WRITE PRINT-REC FROM HEADER-2.
120+
MOVE SPACES TO PRINT-REC.
121+
WRITE PRINT-REC AFTER ADVANCING 1 LINES.
122+
WRITE PRINT-REC FROM HEADER-3.
123+
WRITE PRINT-REC FROM HEADER-4.
124+
MOVE SPACES TO PRINT-REC.
125+
*
126+
READ-NEXT-RECORD.
127+
PERFORM READ-RECORD
128+
PERFORM UNTIL LASTREC = 'Y'
129+
* CHANGE 2: Updated paragraph name to reflect New York processing
130+
* Original was IS-STATE-VIRGINIA, now IS-STATE-NEWYORK
131+
PERFORM IS-STATE-NEWYORK
132+
PERFORM WRITE-RECORD
133+
PERFORM READ-RECORD
134+
END-PERFORM
135+
.
136+
*
137+
CLOSE-STOP.
138+
WRITE PRINT-REC FROM CLIENTS-PER-STATE.
139+
CLOSE ACCT-REC.
140+
CLOSE PRINT-LINE.
141+
GOBACK.
142+
*
143+
READ-RECORD.
144+
READ ACCT-REC
145+
AT END MOVE 'Y' TO LASTREC
146+
END-READ.
147+
*
148+
* CHANGE 3: Updated paragraph name and logic to check for New York
149+
* Original paragraph: IS-STATE-VIRGINIA
150+
* - Checked for 'Virginia' state
151+
* - Added to VIRGINIA-CLIENTS counter
152+
* Modified paragraph: IS-STATE-NEWYORK
153+
* - Now checks for 'New York' state
154+
* - Adds to NEWYORK-CLIENTS counter
155+
156+
IS-STATE-NEWYORK.
157+
IF USA-STATE = 'new York' THEN
158+
ADD 1 TO NEWYORK-CLIENTS
159+
END-IF.
160+
161+
* Boolean logic -- when the conditional expression
162+
* USA-STATE = 'New York' is true, the program
163+
* counts one more client from New York
164+
* Note -- the inclusion of the word THEN is optional
165+
* END-IF -- explicitly terminates the IF statement
166+
*
167+
WRITE-RECORD.
168+
MOVE ACCT-NO TO ACCT-NO-O.
169+
MOVE ACCT-LIMIT TO ACCT-LIMIT-O.
170+
MOVE ACCT-BALANCE TO ACCT-BALANCE-O.
171+
MOVE LAST-NAME TO LAST-NAME-O.
172+
WRITE PRINT-REC.
173+
*
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//CBL006AJ JOB 1,NOTIFY=&SYSUID
2+
//***************************************************/
3+
//* Copyright Contributors to the COBOL Programming Course
4+
//* SPDX-License-Identifier: CC-BY-4.0
5+
//***************************************************/
6+
//COBRUN EXEC IGYWCL
7+
//COBOL.SYSIN DD DSN=&SYSUID..CBL(CBL006A),DISP=SHR
8+
//LKED.SYSLMOD DD DSN=&SYSUID..LOAD(CBL006A),DISP=SHR
9+
//***************************************************/
10+
// IF RC = 0 THEN
11+
//***************************************************/
12+
//RUN EXEC PGM=CBL006A
13+
//STEPLIB DD DSN=&SYSUID..LOAD,DISP=SHR
14+
//ACCTREC DD DSN=&SYSUID..DATA,DISP=SHR
15+
//PRTLINE DD SYSOUT=*,OUTLIM=15000
16+
//SYSOUT DD SYSOUT=*,OUTLIM=15000
17+
//CEEDUMP DD DUMMY
18+
//SYSUDUMP DD DUMMY
19+
//***************************************************/
20+
// ELSE
21+
// ENDIF

0 commit comments

Comments
 (0)