MainframeMaster

COBOL Tutorial

COBOL IF Statement Quick Reference

The IF statement is COBOL's primary conditional construct, enabling decision-making based on conditions. It evaluates whether a condition is true or false and executes the appropriate code path.

Basic Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*> Simple IF statement IF condition statement-1 statement-2 END-IF *> IF with ELSE IF condition statements (when true) ELSE statements (when false) END-IF *> Multiple conditions with logical operators IF condition-1 AND condition-2 statements ELSE statements END-IF

Comparison Operators

Equality Operators
  • = or EQUAL TO - Equal
  • NOT = or NOT EQUAL TO - Not equal
Inequality Operators
  • > or GREATER THAN - Greater than
  • < or LESS THAN - Less than
  • >= - Greater than or equal
  • <= - Less than or equal

Logical Operators

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
*> AND - both conditions must be true IF field-1 > 0 AND field-2 NOT = SPACES PERFORM PROCESS-DATA END-IF *> OR - either condition can be true IF department = "IT" OR department = "ENGINEERING" MOVE "TECH" TO category END-IF *> NOT - negates the condition IF NOT end-of-file PERFORM READ-NEXT-RECORD END-IF *> Complex combinations with parentheses IF (salary > 50000 AND years > 5) OR (rating = "EXCELLENT") PERFORM AWARD-BONUS END-IF

Practical 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
IDENTIFICATION DIVISION. PROGRAM-ID. IF-EXAMPLES. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-AMOUNT PIC 9(5)V99 VALUE 1234.56. 01 WS-RATE PIC 9V999 VALUE 0.15. 01 WS-DISCOUNT PIC 9(5)V99. 01 WS-CUSTOMER PIC X(20). 88 VIP-CUSTOMER VALUE "PREMIUM", "GOLD". 01 WS-QUANTITY PIC 9(3) VALUE 50. PROCEDURE DIVISION. MAIN-LOGIC. *> Simple comparison IF WS-AMOUNT > 1000 COMPUTE WS-DISCOUNT = WS-AMOUNT * WS-RATE DISPLAY "Discount applied: " WS-DISCOUNT END-IF *> IF-ELSE structure IF VIP-CUSTOMER DISPLAY "VIP customer - special treatment" COMPUTE WS-DISCOUNT = WS-AMOUNT * 0.20 ELSE DISPLAY "Regular customer" COMPUTE WS-DISCOUNT = WS-AMOUNT * 0.10 END-IF *> Nested IF statements IF WS-QUANTITY > 100 IF VIP-CUSTOMER COMPUTE WS-DISCOUNT = WS-AMOUNT * 0.30 ELSE COMPUTE WS-DISCOUNT = WS-AMOUNT * 0.15 END-IF ELSE IF VIP-CUSTOMER COMPUTE WS-DISCOUNT = WS-AMOUNT * 0.20 ELSE COMPUTE WS-DISCOUNT = WS-AMOUNT * 0.10 END-IF END-IF *> Multiple conditions with logical operators IF WS-AMOUNT > 1000 AND WS-QUANTITY > 50 DISPLAY "Bulk order discount eligible" ELSE IF WS-AMOUNT > 500 OR VIP-CUSTOMER DISPLAY "Standard discount eligible" END-IF END-IF STOP RUN.

Common Patterns

Best Practices

  • Always use END-IF to explicitly terminate IF statements for clarity
  • Indent nested IF statements consistently to show hierarchy
  • Use condition names (88-level items) instead of literal comparisons when possible
  • Limit nesting to 3-4 levels deep; use EVALUATE for complex multi-way branches
  • Use parentheses to group complex logical expressions
  • Place most likely or most critical conditions first for performance

Key Takeaways

  • IF statements provide conditional execution based on true/false conditions
  • Use END-IF to explicitly terminate IF blocks, especially with nested structures
  • Combine conditions using AND, OR, and NOT logical operators
  • Comparisons use =, >, <, >=, <=, or NOT = operators
  • Nested IF statements allow for complex multi-level decision logic
  • Consider using EVALUATE for multi-way branches with discrete values

Explain It Like I'm 5 Years Old:

Think of an IF statement like making decisions! If you're deciding whether to play outside, you might say "IF it's sunny outside, THEN I'll go play in the park, ELSE I'll stay inside and read." Just like that, COBOL programs use IF to make choices. The program checks if something is true (like sunny weather) and does one thing, or if it's false (rainy) and does something different. It's like the computer asking questions and doing different things based on the answers!