MainframeMaster

COBOL Tutorial

COBOL NOT

The NOT operator represents comprehensive logical negation and boolean operation capabilities within COBOL programming environments, providing sophisticated condition reversal features, advanced truth value manipulation mechanisms, and intelligent logical control that enable precise conditional logic, complex boolean expressions, and systematic decision-making processes. This operator embodies modern logical programming principles by supporting comprehensive condition negation, enabling sophisticated boolean algebra workflows, and facilitating precise logical control requirements while maintaining expression clarity, ensuring predictable logical behavior, and enabling flexible conditional architectures across enterprise applications requiring complex logical operations, condition testing capabilities, and reliable boolean processing throughout sophisticated business logic scenarios and decision-making workflows.

NOT Operator Usage

NOT in Different Contexts
cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
*> Basic NOT operations IF NOT condition THEN statements END-IF *> NOT with relational operators IF NOT (amount > 1000) DISPLAY "Amount is 1000 or less" END-IF *> NOT with equality tests IF customer-code NOT = "INACTIVE" PERFORM PROCESS-CUSTOMER END-IF *> NOT with class tests IF input-field NOT NUMERIC DISPLAY "Invalid numeric input" MOVE "Y" TO error-flag END-IF *> NOT with condition names READ customer-file NOT AT END PERFORM PROCESS-CUSTOMER-RECORD AT END MOVE "Y" TO end-of-file-flag END-READ *> NOT with complex expressions IF NOT (status = "A" AND balance > 0) DISPLAY "Account not active or no balance" END-IF *> NOT with multiple conditions IF NOT (status = "CLOSED" OR status = "SUSPENDED") PERFORM NORMAL-PROCESSING ELSE PERFORM EXCEPTION-PROCESSING END-IF *> NOT with SET statements (condition variables) 88 ACCOUNT-ACTIVE VALUE "A". 88 ACCOUNT-CLOSED VALUE "C". IF NOT ACCOUNT-ACTIVE DISPLAY "Account is not active" END-IF *> NOT with SEARCH statements SEARCH customer-table AT END MOVE "N" TO found-flag WHEN NOT (customer-name(index) = SPACES) MOVE "Y" TO found-flag END-SEARCH *> NOT with file operations OPEN INPUT data-file IF file-status NOT = "00" DISPLAY "File open error: " file-status PERFORM ERROR-ROUTINE END-IF
Logical Operator
Boolean Logic
Condition Testing

Comprehensive NOT Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
IDENTIFICATION DIVISION. PROGRAM-ID. NOT-OPERATOR-DEMO. DATA DIVISION. WORKING-STORAGE SECTION. *> Test data and flags 01 WS-TEST-DATA. 05 WS-CUSTOMER-STATUS PIC X(8). 05 WS-ACCOUNT-BALANCE PIC S9(8)V99. 05 WS-TRANSACTION-AMOUNT PIC 9(6)V99. 05 WS-USER-INPUT PIC X(20). 05 WS-RECORD-COUNT PIC 9(6). 05 WS-ERROR-COUNT PIC 9(4). *> Condition variables (88-level) 01 WS-STATUS-FLAGS. 05 WS-PROCESSING-FLAG PIC X. 88 PROCESSING-ACTIVE VALUE "A". 88 PROCESSING-STOPPED VALUE "S". 88 PROCESSING-ERROR VALUE "E". 05 WS-VALIDATION-FLAG PIC X. 88 VALIDATION-PASSED VALUE "P". 88 VALIDATION-FAILED VALUE "F". 05 WS-FILE-FLAG PIC X. 88 FILE-OPEN VALUE "O". 88 FILE-CLOSED VALUE "C". 88 FILE-ERROR VALUE "E". *> Business logic fields 01 WS-BUSINESS-RULES. 05 WS-MINIMUM-BALANCE PIC S9(6)V99 VALUE -500.00. 05 WS-MAXIMUM-TRANSACTION PIC 9(8)V99 VALUE 50000.00. 05 WS-DAILY-LIMIT PIC 9(6)V99 VALUE 10000.00. 05 WS-VIP-THRESHOLD PIC 9(8)V99 VALUE 100000.00. *> Counters and statistics 01 WS-STATISTICS. 05 WS-VALID-RECORDS PIC 9(6) VALUE 0. 05 WS-INVALID-RECORDS PIC 9(6) VALUE 0. 05 WS-PROCESSED-AMOUNT PIC 9(10)V99 VALUE 0. 05 WS-REJECTED-AMOUNT PIC 9(10)V99 VALUE 0. *> Customer data structure 01 WS-CUSTOMER-DATA. 05 WS-CUSTOMER-ID PIC X(8). 05 WS-CUSTOMER-NAME PIC X(30). 05 WS-CUSTOMER-TYPE PIC X(10). 05 WS-CREDIT-RATING PIC X(5). 05 WS-LAST-ACTIVITY PIC 9(8). PROCEDURE DIVISION. MAIN-NOT-DEMONSTRATION. DISPLAY "=== COBOL NOT OPERATOR DEMONSTRATION ===" DISPLAY SPACES PERFORM DEMONSTRATE-BASIC-NOT-OPERATIONS PERFORM DEMONSTRATE-NOT-WITH-CONDITIONS PERFORM DEMONSTRATE-COMPLEX-NOT-EXPRESSIONS PERFORM DEMONSTRATE-NOT-WITH-VALIDATION PERFORM DEMONSTRATE-BUSINESS-LOGIC-NOT PERFORM DEMONSTRATE-NOT-WITH-FILE-OPERATIONS DISPLAY "=== NOT OPERATOR DEMO COMPLETE ===" STOP RUN. DEMONSTRATE-BASIC-NOT-OPERATIONS. DISPLAY "=== BASIC NOT OPERATIONS ===" DISPLAY SPACES DISPLAY "Testing basic NOT with different conditions..." *> Test NOT with numeric comparison MOVE 500.00 TO WS-ACCOUNT-BALANCE IF NOT (WS-ACCOUNT-BALANCE > 1000) DISPLAY " ✓ Balance is NOT greater than 1000: $" WS-ACCOUNT-BALANCE ELSE DISPLAY " Balance is greater than 1000: $" WS-ACCOUNT-BALANCE END-IF *> Test NOT with equality MOVE "ACTIVE" TO WS-CUSTOMER-STATUS IF WS-CUSTOMER-STATUS NOT = "INACTIVE" DISPLAY " ✓ Customer status is NOT inactive: " WS-CUSTOMER-STATUS ELSE DISPLAY " Customer status is inactive" END-IF *> Test NOT with class tests MOVE "ABC123" TO WS-USER-INPUT IF WS-USER-INPUT NOT NUMERIC DISPLAY " ✓ Input is NOT numeric: " WS-USER-INPUT ELSE DISPLAY " Input is numeric: " WS-USER-INPUT END-IF *> Test NOT with zero/space conditions MOVE 0 TO WS-RECORD-COUNT IF NOT (WS-RECORD-COUNT > 0) DISPLAY " ✓ Record count is NOT positive: " WS-RECORD-COUNT DISPLAY " No records to process" ELSE DISPLAY " Records available for processing: " WS-RECORD-COUNT END-IF DISPLAY SPACES. DEMONSTRATE-NOT-WITH-CONDITIONS. DISPLAY "=== NOT WITH CONDITION VARIABLES ===" DISPLAY SPACES DISPLAY "Testing NOT with 88-level condition variables..." *> Set up test conditions MOVE "S" TO WS-PROCESSING-FLAG MOVE "F" TO WS-VALIDATION-FLAG MOVE "C" TO WS-FILE-FLAG *> Test NOT with condition names IF NOT PROCESSING-ACTIVE DISPLAY " ⚠️ Processing is NOT active" DISPLAY " Current status: STOPPED or ERROR" ELSE DISPLAY " Processing is currently active" END-IF IF NOT VALIDATION-PASSED DISPLAY " ❌ Validation has NOT passed" DISPLAY " Data validation failed" ELSE DISPLAY " ✅ Validation passed successfully" END-IF IF NOT FILE-OPEN DISPLAY " 📁 File is NOT open" DISPLAY " File status: CLOSED or ERROR" ELSE DISPLAY " 📁 File is currently open" END-IF *> Demonstrate NOT with multiple conditions IF NOT (PROCESSING-ACTIVE AND VALIDATION-PASSED AND FILE-OPEN) DISPLAY " " DISPLAY " 🚫 System NOT ready for processing" DISPLAY " One or more prerequisites not met:" IF NOT PROCESSING-ACTIVE DISPLAY " - Processing not active" END-IF IF NOT VALIDATION-PASSED DISPLAY " - Validation not passed" END-IF IF NOT FILE-OPEN DISPLAY " - Required file not open" END-IF ELSE DISPLAY " " DISPLAY " ✅ All systems ready for processing" END-IF DISPLAY SPACES. DEMONSTRATE-COMPLEX-NOT-EXPRESSIONS. DISPLAY "=== COMPLEX NOT EXPRESSIONS ===" DISPLAY SPACES DISPLAY "Testing complex boolean expressions with NOT..." *> Sample business data MOVE "VIP" TO WS-CUSTOMER-TYPE MOVE "AAA" TO WS-CREDIT-RATING MOVE 150000.00 TO WS-ACCOUNT-BALANCE MOVE 2500.00 TO WS-TRANSACTION-AMOUNT *> Complex NOT expression for transaction approval IF NOT ((WS-CUSTOMER-TYPE = "VIP" AND WS-CREDIT-RATING = "AAA") OR (WS-ACCOUNT-BALANCE > WS-VIP-THRESHOLD)) DISPLAY " 📋 Standard processing required" DISPLAY " Customer does NOT qualify for VIP processing" DISPLAY " Customer type: " WS-CUSTOMER-TYPE DISPLAY " Credit rating: " WS-CREDIT-RATING DISPLAY " Balance: $" WS-ACCOUNT-BALANCE ELSE DISPLAY " ⭐ VIP processing approved" DISPLAY " Customer qualifies for expedited service" END-IF *> NOT with transaction limits IF NOT (WS-TRANSACTION-AMOUNT <= WS-DAILY-LIMIT AND WS-TRANSACTION-AMOUNT <= WS-MAXIMUM-TRANSACTION) DISPLAY " " DISPLAY " 🚫 Transaction NOT approved - Exceeds limits" DISPLAY " Transaction amount: $" WS-TRANSACTION-AMOUNT IF WS-TRANSACTION-AMOUNT > WS-DAILY-LIMIT DISPLAY " Exceeds daily limit: $" WS-DAILY-LIMIT END-IF IF WS-TRANSACTION-AMOUNT > WS-MAXIMUM-TRANSACTION DISPLAY " Exceeds maximum: $" WS-MAXIMUM-TRANSACTION END-IF ELSE DISPLAY " " DISPLAY " ✅ Transaction approved within limits" END-IF *> NOT with business hours check (simulated) MOVE 8 TO WS-ERROR-COUNT *> Simulating hour IF NOT (WS-ERROR-COUNT >= 9 AND WS-ERROR-COUNT <= 17) DISPLAY " " DISPLAY " 🕐 Transaction NOT during business hours" DISPLAY " Current hour: " WS-ERROR-COUNT DISPLAY " After-hours processing fee may apply" ELSE DISPLAY " " DISPLAY " 🕒 Transaction during business hours" DISPLAY " Standard processing rates apply" END-IF DISPLAY SPACES. DEMONSTRATE-NOT-WITH-VALIDATION. DISPLAY "=== NOT WITH VALIDATION LOGIC ===" DISPLAY SPACES DISPLAY "Using NOT for comprehensive data validation..." *> Sample customer data for validation MOVE "CUST001" TO WS-CUSTOMER-ID MOVE SPACES TO WS-CUSTOMER-NAME MOVE "PREMIUM" TO WS-CUSTOMER-TYPE MOVE "A+" TO WS-CREDIT-RATING MOVE 0 TO WS-LAST-ACTIVITY MOVE 0 TO WS-ERROR-COUNT *> Validate customer ID IF NOT (WS-CUSTOMER-ID NOT = SPACES AND WS-CUSTOMER-ID NOT = LOW-VALUES) ADD 1 TO WS-ERROR-COUNT DISPLAY " ❌ Customer ID validation failed" DISPLAY " Customer ID is missing or invalid" ELSE DISPLAY " ✅ Customer ID validation passed" END-IF *> Validate customer name IF NOT (WS-CUSTOMER-NAME NOT = SPACES) ADD 1 TO WS-ERROR-COUNT DISPLAY " ❌ Customer name validation failed" DISPLAY " Customer name is required" ELSE DISPLAY " ✅ Customer name validation passed" END-IF *> Validate customer type IF NOT (WS-CUSTOMER-TYPE = "STANDARD" OR WS-CUSTOMER-TYPE = "PREMIUM" OR WS-CUSTOMER-TYPE = "VIP") ADD 1 TO WS-ERROR-COUNT DISPLAY " ❌ Customer type validation failed" DISPLAY " Invalid customer type: " WS-CUSTOMER-TYPE ELSE DISPLAY " ✅ Customer type validation passed" END-IF *> Validate credit rating format IF NOT (WS-CREDIT-RATING NOT = SPACES AND (WS-CREDIT-RATING = "AAA" OR WS-CREDIT-RATING = "AA" OR WS-CREDIT-RATING = "A+" OR WS-CREDIT-RATING = "A" OR WS-CREDIT-RATING = "BBB" OR WS-CREDIT-RATING = "BB" OR WS-CREDIT-RATING = "B")) ADD 1 TO WS-ERROR-COUNT DISPLAY " ❌ Credit rating validation failed" DISPLAY " Invalid rating: " WS-CREDIT-RATING ELSE DISPLAY " ✅ Credit rating validation passed" END-IF *> Overall validation result IF NOT (WS-ERROR-COUNT = 0) DISPLAY " " DISPLAY " 🚫 Customer data validation FAILED" DISPLAY " Total errors found: " WS-ERROR-COUNT DISPLAY " Customer record rejected" ELSE DISPLAY " " DISPLAY " ✅ Customer data validation PASSED" DISPLAY " All validations successful" END-IF DISPLAY SPACES. DEMONSTRATE-BUSINESS-LOGIC-NOT. DISPLAY "=== BUSINESS LOGIC WITH NOT ===" DISPLAY SPACES DISPLAY "Implementing business rules using NOT logic..." *> Sample account data MOVE -200.00 TO WS-ACCOUNT-BALANCE MOVE 5000.00 TO WS-TRANSACTION-AMOUNT MOVE "DEBIT" TO WS-CUSTOMER-STATUS *> Business Rule 1: Overdraft protection IF NOT (WS-ACCOUNT-BALANCE >= WS-MINIMUM-BALANCE) DISPLAY " ⚠️ Account balance below minimum" DISPLAY " Current balance: $" WS-ACCOUNT-BALANCE DISPLAY " Minimum required: $" WS-MINIMUM-BALANCE DISPLAY " Overdraft protection triggered" ELSE DISPLAY " ✅ Account balance meets minimum requirements" END-IF *> Business Rule 2: Transaction authorization COMPUTE WS-PROCESSED-AMOUNT = WS-ACCOUNT-BALANCE - WS-TRANSACTION-AMOUNT IF NOT (WS-PROCESSED-AMOUNT >= WS-MINIMUM-BALANCE AND WS-CUSTOMER-STATUS NOT = "SUSPENDED" AND WS-CUSTOMER-STATUS NOT = "CLOSED") DISPLAY " " DISPLAY " 🚫 Transaction NOT authorized" DISPLAY " Reasons:" IF NOT (WS-PROCESSED-AMOUNT >= WS-MINIMUM-BALANCE) DISPLAY " - Would exceed overdraft limit" DISPLAY " Resulting balance: $" WS-PROCESSED-AMOUNT END-IF IF WS-CUSTOMER-STATUS = "SUSPENDED" DISPLAY " - Account is suspended" END-IF IF WS-CUSTOMER-STATUS = "CLOSED" DISPLAY " - Account is closed" END-IF ADD WS-TRANSACTION-AMOUNT TO WS-REJECTED-AMOUNT ELSE DISPLAY " " DISPLAY " ✅ Transaction authorized" DISPLAY " New balance: $" WS-PROCESSED-AMOUNT ADD WS-TRANSACTION-AMOUNT TO WS-PROCESSED-AMOUNT END-IF *> Business Rule 3: Risk assessment IF NOT (WS-TRANSACTION-AMOUNT <= 1000.00 OR (WS-CREDIT-RATING = "AAA" OR WS-CREDIT-RATING = "AA")) DISPLAY " " DISPLAY " 🔍 Enhanced verification required" DISPLAY " High-value transaction from non-premium customer" DISPLAY " Amount: $" WS-TRANSACTION-AMOUNT DISPLAY " Credit rating: " WS-CREDIT-RATING DISPLAY " Manual approval needed" ELSE DISPLAY " " DISPLAY " ✅ Standard processing approved" END-IF DISPLAY SPACES. DEMONSTRATE-NOT-WITH-FILE-OPERATIONS. DISPLAY "=== NOT WITH FILE OPERATIONS ===" DISPLAY SPACES DISPLAY "Using NOT in file processing scenarios..." *> Simulate file status codes MOVE "00" TO WS-CUSTOMER-STATUS *> Simulating file status *> File open validation IF NOT (WS-CUSTOMER-STATUS = "00") DISPLAY " ❌ File NOT opened successfully" DISPLAY " File status: " WS-CUSTOMER-STATUS EVALUATE WS-CUSTOMER-STATUS WHEN "30" DISPLAY " Error: File not found" WHEN "35" DISPLAY " Error: File not opened (invalid mode)" WHEN "37" DISPLAY " Error: File not opened (incompatible mode)" WHEN "39" DISPLAY " Error: File not opened (record inconsistent)" WHEN OTHER DISPLAY " Error: Unknown file status" END-EVALUATE ELSE DISPLAY " ✅ File opened successfully" END-IF *> Simulate record processing MOVE 250 TO WS-RECORD-COUNT MOVE 5 TO WS-ERROR-COUNT *> Processing validation IF NOT (WS-RECORD-COUNT > 0 AND WS-ERROR-COUNT = 0) DISPLAY " " DISPLAY " ⚠️ File processing issues detected" IF NOT (WS-RECORD-COUNT > 0) DISPLAY " No records found in file" END-IF IF NOT (WS-ERROR-COUNT = 0) DISPLAY " Errors encountered: " WS-ERROR-COUNT END-IF DISPLAY " Review file and error logs" ELSE DISPLAY " " DISPLAY " ✅ File processing completed successfully" DISPLAY " Records processed: " WS-RECORD-COUNT DISPLAY " Errors: " WS-ERROR-COUNT END-IF *> End-of-file simulation MOVE "Y" TO WS-PROCESSING-FLAG *> Simulating EOF flag IF NOT (WS-PROCESSING-FLAG = "N") DISPLAY " " DISPLAY " 📄 End of file reached" DISPLAY " Processing complete or file empty" ELSE DISPLAY " " DISPLAY " 📄 More records available" DISPLAY " Continue processing" END-IF DISPLAY SPACES DISPLAY "NOT operator demonstration summary:" DISPLAY " ✓ Basic NOT conditions tested" DISPLAY " ✓ Complex boolean expressions evaluated" DISPLAY " ✓ Validation logic implemented" DISPLAY " ✓ Business rules enforced" DISPLAY " ✓ File operations validated" DISPLAY SPACES. *> Additional utility paragraphs for demonstration RESET-TEST-DATA. MOVE SPACES TO WS-CUSTOMER-STATUS MOVE 0 TO WS-ACCOUNT-BALANCE MOVE 0 TO WS-TRANSACTION-AMOUNT MOVE SPACES TO WS-USER-INPUT MOVE 0 TO WS-RECORD-COUNT MOVE 0 TO WS-ERROR-COUNT. DISPLAY-TEST-RESULTS. DISPLAY "Test Results Summary:" DISPLAY " Valid records: " WS-VALID-RECORDS DISPLAY " Invalid records: " WS-INVALID-RECORDS DISPLAY " Processed amount: $" WS-PROCESSED-AMOUNT DISPLAY " Rejected amount: $" WS-REJECTED-AMOUNT.

NOT Operator Features

Logical Operations
  • • Truth value reversal
  • • Highest logical precedence
  • • Complex expression support
  • • Condition negation
Usage Contexts
  • • Relational comparisons
  • • Class tests (NUMERIC, etc.)
  • • Condition name variables
  • • File operation results

Interactive Tutorial

Hands-On Exercise: Validation System
Practice using NOT for comprehensive data validation and business logic

Exercise 1: Account Validation System

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
01 ACCOUNT-DATA. 05 ACCOUNT-NUMBER PIC X(10). 05 ACCOUNT-TYPE PIC X(8). 05 BALANCE PIC S9(8)V99. 05 STATUS-CODE PIC X(6). 01 VALIDATION-RESULTS. 05 VALIDATION-PASSED PIC X VALUE 'Y'. 05 ERROR-COUNT PIC 9(3) VALUE 0. 05 WARNING-COUNT PIC 9(3) VALUE 0. 88 ACCOUNT-ACTIVE VALUE "ACTIVE". 88 ACCOUNT-CLOSED VALUE "CLOSED". 88 ACCOUNT-SUSPENDED VALUE "SUSP". PROCEDURE DIVISION. VALIDATE-ACCOUNT. *> Reset validation flags MOVE 'Y' TO VALIDATION-PASSED MOVE 0 TO ERROR-COUNT MOVE 0 TO WARNING-COUNT *> Validate account number IF NOT (ACCOUNT-NUMBER NOT = SPACES AND ACCOUNT-NUMBER NOT = LOW-VALUES) ADD 1 TO ERROR-COUNT MOVE 'N' TO VALIDATION-PASSED DISPLAY "❌ Account number is required" END-IF *> Validate account type IF NOT (ACCOUNT-TYPE = "CHECKING" OR ACCOUNT-TYPE = "SAVINGS" OR ACCOUNT-TYPE = "CREDIT" OR ACCOUNT-TYPE = "LOAN") ADD 1 TO ERROR-COUNT MOVE 'N' TO VALIDATION-PASSED DISPLAY "❌ Invalid account type: " ACCOUNT-TYPE END-IF *> Check account status IF NOT ACCOUNT-ACTIVE IF ACCOUNT-CLOSED ADD 1 TO ERROR-COUNT MOVE 'N' TO VALIDATION-PASSED DISPLAY "❌ Cannot process closed account" ELSE IF ACCOUNT-SUSPENDED ADD 1 TO WARNING-COUNT DISPLAY "⚠️ Account is suspended" END-IF END-IF *> Validate balance constraints IF NOT (BALANCE >= -5000.00) ADD 1 TO ERROR-COUNT MOVE 'N' TO VALIDATION-PASSED DISPLAY "❌ Balance exceeds overdraft limit" END-IF *> Display validation summary IF VALIDATION-PASSED = 'Y' AND WARNING-COUNT = 0 DISPLAY "✅ Account validation passed completely" ELSE IF VALIDATION-PASSED = 'Y' DISPLAY "⚠️ Account validation passed with warnings" DISPLAY " Warnings: " WARNING-COUNT ELSE DISPLAY "❌ Account validation failed" DISPLAY " Errors: " ERROR-COUNT DISPLAY " Warnings: " WARNING-COUNT END-IF.

Exercise 2: Transaction Authorization

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
01 TRANSACTION-REQUEST. 05 TRANS-TYPE PIC X(8). 05 TRANS-AMOUNT PIC 9(8)V99. 05 CUSTOMER-ID PIC X(8). 05 MERCHANT-CODE PIC X(6). 01 AUTHORIZATION-RULES. 05 MAX-DAILY-LIMIT PIC 9(6)V99 VALUE 5000.00. 05 MAX-SINGLE-TRANS PIC 9(6)V99 VALUE 2000.00. 05 MIN-BALANCE PIC S9(6)V99 VALUE -500.00. 01 AUTH-RESULT. 05 AUTHORIZED PIC X VALUE 'N'. 05 REASON-CODE PIC X(3). 05 RISK-SCORE PIC 9(3) VALUE 0. PROCEDURE DIVISION. AUTHORIZE-TRANSACTION. MOVE 'Y' TO AUTHORIZED MOVE SPACES TO REASON-CODE MOVE 0 TO RISK-SCORE *> Basic transaction validation IF NOT (TRANS-AMOUNT > 0) MOVE 'N' TO AUTHORIZED MOVE '001' TO REASON-CODE DISPLAY "❌ Invalid transaction amount" EXIT PARAGRAPH END-IF *> Check transaction limits IF NOT (TRANS-AMOUNT <= MAX-SINGLE-TRANS) MOVE 'N' TO AUTHORIZED MOVE '002' TO REASON-CODE DISPLAY "❌ Exceeds single transaction limit" EXIT PARAGRAPH END-IF *> Validate transaction type IF NOT (TRANS-TYPE = "PURCHASE" OR TRANS-TYPE = "WITHDRAW" OR TRANS-TYPE = "TRANSFER") MOVE 'N' TO AUTHORIZED MOVE '003' TO REASON-CODE DISPLAY "❌ Invalid transaction type" EXIT PARAGRAPH END-IF *> Risk assessment IF NOT (MERCHANT-CODE NOT = SPACES) ADD 10 TO RISK-SCORE DISPLAY "⚠️ Merchant verification required" END-IF IF NOT (TRANS-AMOUNT <= 100.00) ADD 5 TO RISK-SCORE END-IF *> High-risk transaction check IF NOT (RISK-SCORE <= 15) MOVE 'N' TO AUTHORIZED MOVE '004' TO REASON-CODE DISPLAY "❌ High-risk transaction detected" DISPLAY " Risk score: " RISK-SCORE EXIT PARAGRAPH END-IF *> Final authorization IF AUTHORIZED = 'Y' DISPLAY "✅ Transaction authorized" DISPLAY " Amount: $" TRANS-AMOUNT DISPLAY " Type: " TRANS-TYPE DISPLAY " Risk score: " RISK-SCORE END-IF.

Best Practices

Knowledge Check

Test Your Understanding

Question 1: NOT Operator Function

What does the NOT operator do to a condition?

Answer: The NOT operator reverses the truth value of a condition. It converts TRUE conditions to FALSE and FALSE conditions to TRUE, allowing for logical negation and inverse conditional testing.

Question 2: Operator Precedence

What is the precedence order of logical operators in COBOL?

Answer: The precedence order is: NOT (highest), then AND, then OR (lowest). NOT is evaluated first, then AND operations, then OR operations. Use parentheses to override default precedence and clarify complex expressions.

Question 3: Best Practices

What are the best practices for using NOT in complex expressions?

Answer: Use parentheses to clarify complex expressions, avoid double negatives when possible, place NOT close to the condition it negates, and consider positive logic alternatives for better readability. Document complex NOT expressions for maintainability.