MainframeMaster

COBOL Tutorial

COBOL IF and END-IF Statements

The IF and END-IF statements represent the fundamental conditional logic mechanisms within COBOL programming environments, providing sophisticated decision-making capabilities that enable complex business rule implementation, data validation processes, and algorithmic control flow management. These statements embody the core principles of structured programming by supporting nested conditional logic, compound Boolean expressions, and multi-path execution scenarios while maintaining code readability, ensuring logical clarity, and facilitating comprehensive business logic implementation across diverse enterprise applications requiring robust decision-making capabilities, data-driven processing, and maintainable conditional structures.

IF Statement Syntax

IF and END-IF Structure
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
*> Basic IF statement syntax IF condition statement-1 statement-2 END-IF *> IF-ELSE structure IF condition statement-1 ELSE statement-2 END-IF *> Complex conditional example IF field-1 > 0 AND field-2 NOT = SPACES PERFORM PROCESS-VALID-DATA ELSE PERFORM HANDLE-INVALID-DATA END-IF *> Nested IF statements IF customer-type = "PREMIUM" IF order-amount > 1000 COMPUTE discount = order-amount * 0.15 ELSE COMPUTE discount = order-amount * 0.10 END-IF ELSE IF order-amount > 500 COMPUTE discount = order-amount * 0.05 ELSE MOVE 0 TO discount END-IF END-IF *> Multiple conditions with logical operators IF (salary > 50000 AND department = "IT") OR (years-service > 10 AND performance-rating = "EXCELLENT") MOVE "ELIGIBLE" TO bonus-status ELSE MOVE "NOT-ELIGIBLE" TO bonus-status END-IF.
Conditional Logic
Decision Making
Control Flow

Comprehensive IF 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
IDENTIFICATION DIVISION. PROGRAM-ID. IF-STATEMENT-EXAMPLES. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-EMPLOYEE-DATA. 05 WS-EMP-ID PIC 9(6). 05 WS-EMP-NAME PIC X(30). 05 WS-DEPARTMENT PIC X(10). 05 WS-SALARY PIC 9(6)V99. 05 WS-YEARS-SERVICE PIC 9(2). 05 WS-PERFORMANCE PIC X(10). 01 WS-CALCULATIONS. 05 WS-BONUS PIC 9(5)V99. 05 WS-TAX-RATE PIC 9V999. 05 WS-NET-PAY PIC 9(7)V99. 05 WS-GRADE-LEVEL PIC 9(2). 01 WS-FLAGS. 05 WS-ELIGIBLE-FLAG PIC X. 88 ELIGIBLE VALUE 'Y'. 88 NOT-ELIGIBLE VALUE 'N'. 05 WS-ERROR-FLAG PIC X. 88 ERROR-FOUND VALUE 'Y'. 88 NO-ERROR VALUE 'N'. 01 WS-CONSTANTS. 05 WS-MIN-SALARY PIC 9(6)V99 VALUE 30000.00. 05 WS-MAX-SALARY PIC 9(6)V99 VALUE 200000.00. 05 WS-SENIOR-YEARS PIC 9(2) VALUE 10. PROCEDURE DIVISION. MAIN-PROCESSING. PERFORM INITIALIZE-DATA PERFORM DEMONSTRATE-BASIC-IF PERFORM DEMONSTRATE-NESTED-IF PERFORM DEMONSTRATE-COMPLEX-CONDITIONS PERFORM DEMONSTRATE-DATA-VALIDATION PERFORM DEMONSTRATE-BUSINESS-LOGIC STOP RUN. INITIALIZE-DATA. MOVE 123456 TO WS-EMP-ID MOVE "JOHN SMITH" TO WS-EMP-NAME MOVE "IT" TO WS-DEPARTMENT MOVE 75000.00 TO WS-SALARY MOVE 8 TO WS-YEARS-SERVICE MOVE "EXCELLENT" TO WS-PERFORMANCE. DEMONSTRATE-BASIC-IF. DISPLAY "=== BASIC IF STATEMENT EXAMPLES ===" *> Simple comparison IF WS-SALARY > WS-MIN-SALARY DISPLAY "Employee salary is above minimum" END-IF *> IF-ELSE structure IF WS-DEPARTMENT = "IT" DISPLAY "Employee works in IT department" ELSE DISPLAY "Employee works in other department" END-IF *> Condition name usage IF WS-YEARS-SERVICE >= WS-SENIOR-YEARS SET ELIGIBLE TO TRUE DISPLAY "Employee is eligible for senior benefits" ELSE SET NOT-ELIGIBLE TO TRUE DISPLAY "Employee is not eligible for senior benefits" END-IF DISPLAY SPACES. DEMONSTRATE-NESTED-IF. DISPLAY "=== NESTED IF STATEMENT EXAMPLES ===" *> Nested salary and performance evaluation IF WS-SALARY > 50000 IF WS-PERFORMANCE = "EXCELLENT" COMPUTE WS-BONUS = WS-SALARY * 0.15 DISPLAY "High performer bonus: $" WS-BONUS ELSE IF WS-PERFORMANCE = "GOOD" COMPUTE WS-BONUS = WS-SALARY * 0.10 DISPLAY "Good performer bonus: $" WS-BONUS ELSE COMPUTE WS-BONUS = WS-SALARY * 0.05 DISPLAY "Standard bonus: $" WS-BONUS END-IF END-IF ELSE IF WS-PERFORMANCE = "EXCELLENT" COMPUTE WS-BONUS = WS-SALARY * 0.08 DISPLAY "Entry level excellent bonus: $" WS-BONUS ELSE COMPUTE WS-BONUS = WS-SALARY * 0.03 DISPLAY "Entry level standard bonus: $" WS-BONUS END-IF END-IF *> Department and grade level determination IF WS-DEPARTMENT = "IT" IF WS-SALARY > 80000 MOVE 5 TO WS-GRADE-LEVEL ELSE IF WS-SALARY > 60000 MOVE 4 TO WS-GRADE-LEVEL ELSE MOVE 3 TO WS-GRADE-LEVEL END-IF END-IF ELSE IF WS-DEPARTMENT = "FINANCE" IF WS-SALARY > 90000 MOVE 5 TO WS-GRADE-LEVEL ELSE MOVE 4 TO WS-GRADE-LEVEL END-IF ELSE MOVE 3 TO WS-GRADE-LEVEL END-IF END-IF DISPLAY "Employee grade level: " WS-GRADE-LEVEL DISPLAY SPACES. DEMONSTRATE-COMPLEX-CONDITIONS. DISPLAY "=== COMPLEX CONDITIONAL LOGIC ===" *> Multiple conditions with AND/OR IF (WS-SALARY > 70000 AND WS-YEARS-SERVICE > 5) OR (WS-PERFORMANCE = "EXCELLENT" AND WS-YEARS-SERVICE > 3) DISPLAY "Employee qualifies for promotion consideration" ELSE DISPLAY "Employee does not qualify for promotion" END-IF *> Complex business rule IF WS-DEPARTMENT = "SALES" AND WS-PERFORMANCE = "EXCELLENT" AND WS-YEARS-SERVICE >= 2 COMPUTE WS-BONUS = WS-SALARY * 0.20 DISPLAY "Sales excellence bonus: $" WS-BONUS ELSE IF WS-DEPARTMENT = "SALES" AND WS-PERFORMANCE = "GOOD" AND WS-YEARS-SERVICE >= 1 COMPUTE WS-BONUS = WS-SALARY * 0.12 DISPLAY "Sales performance bonus: $" WS-BONUS END-IF END-IF *> Tax bracket calculation IF WS-SALARY <= 40000 MOVE 0.12 TO WS-TAX-RATE ELSE IF WS-SALARY <= 80000 MOVE 0.22 TO WS-TAX-RATE ELSE IF WS-SALARY <= 120000 MOVE 0.32 TO WS-TAX-RATE ELSE MOVE 0.37 TO WS-TAX-RATE END-IF END-IF END-IF COMPUTE WS-NET-PAY = WS-SALARY * (1 - WS-TAX-RATE) DISPLAY "Tax rate: " WS-TAX-RATE " Net pay: $" WS-NET-PAY DISPLAY SPACES. DEMONSTRATE-DATA-VALIDATION. DISPLAY "=== DATA VALIDATION WITH IF STATEMENTS ===" SET NO-ERROR TO TRUE *> Validate employee ID IF WS-EMP-ID = 0 OR WS-EMP-ID > 999999 DISPLAY "Error: Invalid employee ID" SET ERROR-FOUND TO TRUE END-IF *> Validate employee name IF WS-EMP-NAME = SPACES DISPLAY "Error: Employee name is required" SET ERROR-FOUND TO TRUE END-IF *> Validate salary range IF WS-SALARY < WS-MIN-SALARY DISPLAY "Error: Salary below minimum wage" SET ERROR-FOUND TO TRUE ELSE IF WS-SALARY > WS-MAX-SALARY DISPLAY "Error: Salary exceeds maximum limit" SET ERROR-FOUND TO TRUE END-IF END-IF *> Validate years of service IF WS-YEARS-SERVICE < 0 OR WS-YEARS-SERVICE > 50 DISPLAY "Error: Invalid years of service" SET ERROR-FOUND TO TRUE END-IF *> Validate department IF WS-DEPARTMENT NOT = "IT" AND WS-DEPARTMENT NOT = "FINANCE" AND WS-DEPARTMENT NOT = "SALES" AND WS-DEPARTMENT NOT = "HR" AND WS-DEPARTMENT NOT = "OPERATIONS" DISPLAY "Error: Invalid department code" SET ERROR-FOUND TO TRUE END-IF *> Validate performance rating IF WS-PERFORMANCE NOT = "EXCELLENT" AND WS-PERFORMANCE NOT = "GOOD" AND WS-PERFORMANCE NOT = "SATISFACTORY" AND WS-PERFORMANCE NOT = "NEEDS IMPROVEMENT" DISPLAY "Error: Invalid performance rating" SET ERROR-FOUND TO TRUE END-IF IF NO-ERROR DISPLAY "All data validation passed" ELSE DISPLAY "Data validation failed - check errors above" END-IF DISPLAY SPACES. DEMONSTRATE-BUSINESS-LOGIC. DISPLAY "=== BUSINESS LOGIC IMPLEMENTATION ===" *> Comprehensive employee benefits calculation IF WS-DEPARTMENT = "IT" IF WS-SALARY > 80000 AND WS-YEARS-SERVICE > 5 DISPLAY "Eligible for: Senior IT benefits package" DISPLAY " - 25 vacation days" DISPLAY " - $5000 training budget" DISPLAY " - Stock options" ELSE IF WS-SALARY > 60000 DISPLAY "Eligible for: Standard IT benefits" DISPLAY " - 20 vacation days" DISPLAY " - $3000 training budget" ELSE DISPLAY "Eligible for: Basic IT benefits" DISPLAY " - 15 vacation days" DISPLAY " - $1500 training budget" END-IF END-IF ELSE IF WS-DEPARTMENT = "SALES" IF WS-PERFORMANCE = "EXCELLENT" DISPLAY "Eligible for: Sales excellence package" DISPLAY " - Commission bonus eligibility" DISPLAY " - Company car allowance" DISPLAY " - Premium health insurance" ELSE DISPLAY "Eligible for: Standard sales benefits" DISPLAY " - Base commission structure" DISPLAY " - Standard health insurance" END-IF ELSE DISPLAY "Eligible for: Standard employee benefits" DISPLAY " - 15 vacation days" DISPLAY " - Standard health insurance" DISPLAY " - 401k matching" END-IF END-IF *> Performance improvement plan determination IF WS-PERFORMANCE = "NEEDS IMPROVEMENT" IF WS-YEARS-SERVICE < 1 DISPLAY "Action: Extended probation period" ELSE IF WS-YEARS-SERVICE < 3 DISPLAY "Action: 90-day improvement plan" ELSE DISPLAY "Action: Formal review process" END-IF END-IF END-IF DISPLAY SPACES. *> Advanced conditional patterns ADVANCED-CONDITIONAL-PATTERNS. *> Range checking with multiple conditions IF WS-SALARY >= 25000 AND WS-SALARY <= 35000 DISPLAY "Salary range: Entry level" ELSE IF WS-SALARY >= 35001 AND WS-SALARY <= 55000 DISPLAY "Salary range: Mid level" ELSE IF WS-SALARY >= 55001 AND WS-SALARY <= 85000 DISPLAY "Salary range: Senior level" ELSE IF WS-SALARY > 85000 DISPLAY "Salary range: Executive level" ELSE DISPLAY "Salary range: Below standard" END-IF END-IF END-IF END-IF *> Complex eligibility determination IF (WS-DEPARTMENT = "IT" AND WS-SALARY > 70000) OR (WS-DEPARTMENT = "FINANCE" AND WS-SALARY > 65000) OR (WS-DEPARTMENT = "SALES" AND WS-PERFORMANCE = "EXCELLENT") IF WS-YEARS-SERVICE >= 3 DISPLAY "Eligible for leadership development program" ELSE DISPLAY "Meets criteria but needs more experience" END-IF ELSE DISPLAY "Does not meet leadership program criteria" END-IF.

IF Statement Features

Logical Operators
  • AND: Both conditions must be true
  • OR: Either condition can be true
  • NOT: Negates the condition
  • • Parentheses for grouping operations
Comparison Operators
  • = or EQUAL: Equal to
  • > or GREATER: Greater than
  • < or LESS: Less than
  • >=, <=, NOT =: Other comparisons

Interactive Tutorial

Hands-On Exercise: Conditional Logic
Practice using IF statements for business logic implementation

Exercise 1: Grade Calculation

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
01 STUDENT-DATA. 05 STUDENT-SCORE PIC 9(3). 05 STUDENT-GRADE PIC X. PROCEDURE DIVISION. CALCULATE-GRADE. IF STUDENT-SCORE >= 90 MOVE "A" TO STUDENT-GRADE ELSE IF STUDENT-SCORE >= 80 MOVE "B" TO STUDENT-GRADE ELSE IF STUDENT-SCORE >= 70 MOVE "C" TO STUDENT-GRADE ELSE IF STUDENT-SCORE >= 60 MOVE "D" TO STUDENT-GRADE ELSE MOVE "F" TO STUDENT-GRADE END-IF END-IF END-IF END-IF.

Exercise 2: Shipping Cost Calculator

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
01 ORDER-INFO. 05 ORDER-WEIGHT PIC 9(3)V99. 05 ORDER-TOTAL PIC 9(5)V99. 05 CUSTOMER-TYPE PIC X(10). 05 SHIPPING-COST PIC 9(3)V99. CALCULATE-SHIPPING. IF CUSTOMER-TYPE = "PREMIUM" IF ORDER-TOTAL > 100 MOVE 0 TO SHIPPING-COST ELSE COMPUTE SHIPPING-COST = ORDER-WEIGHT * 0.50 END-IF ELSE IF ORDER-TOTAL > 75 COMPUTE SHIPPING-COST = ORDER-WEIGHT * 0.75 ELSE COMPUTE SHIPPING-COST = ORDER-WEIGHT * 1.00 END-IF END-IF.

Best Practices

Knowledge Check

Test Your Understanding

Question 1: Syntax

When is END-IF required in COBOL?

Answer: END-IF is always recommended for clarity and is required when using nested IF statements or when the IF statement contains multiple statements or ELSE clauses.

Question 2: Logical Operators

How do you combine multiple conditions in an IF statement?

Answer: Use logical operators: AND (both conditions true), OR (either condition true), NOT (negates condition). Use parentheses to group complex expressions.

Related Pages