MainframeMaster

COBOL Tutorial

COBOL BOOLEAN - Quick Reference

Progress0 of 0 lessons

Overview

BOOLEAN in COBOL is a logical data type that can hold true or false values. It is used for representing logical conditions, flags, and boolean logic in COBOL programs, providing clearer code logic and better readability for conditional processing and control flow.

Purpose and Usage

  • Logical conditions and control flow
  • Status flags and indicators
  • Boolean logic operations
  • Conditional processing
  • Program state management

Syntax

BOOLEAN follows specific syntax patterns for logical data handling:

Basic BOOLEAN Declaration

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
* Basic BOOLEAN declarations 01 flag PIC 1 VALUE 0. 01 status PIC 1 VALUE 1. 01 is-valid BOOLEAN VALUE FALSE. 01 is-active BOOLEAN VALUE TRUE. * BOOLEAN with 88-level conditions 01 processing-flag PIC 1. 88 processing-complete VALUE 1. 88 processing-pending VALUE 0. * Multiple boolean flags 01 system-status. 05 file-open PIC 1 VALUE 0. 05 data-valid PIC 1 VALUE 0. 05 processing-done PIC 1 VALUE 0. 05 error-occurred PIC 1 VALUE 0.

BOOLEAN variables can be declared using PIC 1 or BOOLEAN keyword.

BOOLEAN Operations

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
* BOOLEAN operations and logic PROCEDURE DIVISION. BOOLEAN-OPERATIONS. * Set boolean values MOVE 1 TO flag. MOVE TRUE TO is-valid. * Boolean comparisons IF flag = 1 DISPLAY "Flag is true" END-IF. IF is-valid DISPLAY "Data is valid" END-IF. * Logical operations IF file-open = 1 AND data-valid = 1 PERFORM process-data END-IF. IF error-occurred = 1 OR processing-done = 0 PERFORM error-handling END-IF. * Boolean assignment MOVE 1 TO processing-done. MOVE FALSE TO error-occurred.

Practical Examples

Here are some practical uses of BOOLEAN in COBOL:

File Processing with Boolean Flags

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
* File processing with boolean flags DATA DIVISION. WORKING-STORAGE SECTION. 01 processing-status. 05 file-open PIC 1 VALUE 0. 05 end-of-file PIC 1 VALUE 0. 05 record-valid PIC 1 VALUE 0. 05 processing-complete PIC 1 VALUE 0. 05 error-occurred PIC 1 VALUE 0. PROCEDURE DIVISION. PROCESS-FILE-WITH-FLAGS. * Initialize flags MOVE 0 TO file-open. MOVE 0 TO end-of-file. MOVE 0 TO processing-complete. MOVE 0 TO error-occurred. * Open file OPEN INPUT data-file. IF file-status = "00" MOVE 1 TO file-open DISPLAY "File opened successfully" ELSE MOVE 1 TO error-occurred DISPLAY "Error opening file" STOP RUN END-IF. * Process records PERFORM UNTIL end-of-file = 1 OR error-occurred = 1 READ data-file AT END MOVE 1 TO end-of-file NOT AT END PERFORM validate-record IF record-valid = 1 PERFORM process-record ELSE DISPLAY "Invalid record skipped" END-IF END-READ END-PERFORM. * Close file IF file-open = 1 CLOSE data-file MOVE 0 TO file-open END-IF. MOVE 1 TO processing-complete. DISPLAY "File processing completed". VALIDATE-RECORD. * Validate record logic MOVE 0 TO record-valid. IF record-data IS NUMERIC MOVE 1 TO record-valid END-IF. PROCESS-RECORD. * Process valid record DISPLAY "Processing record: " record-data. * Additional processing logic here.

File processing using boolean flags for status tracking.

Data Validation with Boolean Logic

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
* Data validation using boolean logic DATA DIVISION. WORKING-STORAGE SECTION. 01 validation-flags. 05 id-valid PIC 1 VALUE 0. 05 name-valid PIC 1 VALUE 0. 05 email-valid PIC 1 VALUE 0. 05 phone-valid PIC 1 VALUE 0. 01 overall-valid PIC 1 VALUE 0. PROCEDURE DIVISION. VALIDATE-CUSTOMER-DATA. * Initialize validation flags MOVE 0 TO id-valid. MOVE 0 TO name-valid. MOVE 0 TO email-valid. MOVE 0 TO phone-valid. MOVE 0 TO overall-valid. * Validate customer ID IF customer-id IS NOT NUMERIC AND customer-id IS NOT SPACES MOVE 1 TO id-valid END-IF. * Validate customer name IF customer-name IS ALPHABETIC AND customer-name IS NOT SPACES MOVE 1 TO name-valid END-IF. * Validate email format IF customer-email CONTAINS "@" AND customer-email CONTAINS "." MOVE 1 TO email-valid END-IF. * Validate phone number IF customer-phone IS NUMERIC AND customer-phone IS NOT SPACES MOVE 1 TO phone-valid END-IF. * Check overall validation IF id-valid = 1 AND name-valid = 1 AND email-valid = 1 AND phone-valid = 1 MOVE 1 TO overall-valid DISPLAY "All customer data is valid" PERFORM process-valid-customer ELSE DISPLAY "Customer data validation failed" PERFORM handle-invalid-data END-IF. PROCESS-VALID-CUSTOMER. * Process customer with valid data DISPLAY "Processing valid customer: " customer-name. * Additional processing logic here. HANDLE-INVALID-DATA. * Handle invalid customer data DISPLAY "Invalid customer data detected". * Error handling logic here.

Data validation using boolean logic and flags.

Best Practices

  • Use meaningful names for boolean variables to improve code readability.
  • Initialize boolean variables to appropriate default values.
  • Use 88-level conditions for clearer boolean logic.
  • Combine boolean flags logically for complex conditions.
  • Document the meaning and usage of boolean variables.
  • Use boolean variables for status tracking and control flow.

Common Pitfalls

  • Not initializing boolean variables to known values.
  • Using unclear or ambiguous boolean variable names.
  • Creating overly complex boolean logic that is hard to understand.
  • Not documenting the meaning of boolean values.
  • Using boolean variables for purposes other than logical conditions.

Test Your Knowledge

1. What is BOOLEAN in COBOL?

  • A numeric data type
  • A logical data type with true/false values
  • A string data type
  • A file data type

2. What values can a BOOLEAN variable hold?

  • Only numbers
  • Only strings
  • True and false values
  • Any data type

3. How is BOOLEAN typically declared in COBOL?

  • PIC 9
  • PIC X
  • PIC 1 or BOOLEAN
  • PIC A

4. What is the primary use of BOOLEAN in COBOL?

  • Arithmetic calculations
  • Logical conditions and control flow
  • String operations
  • File operations

5. What operators can be used with BOOLEAN values?

  • Only arithmetic operators
  • Logical operators (AND, OR, NOT)
  • Only string operators
  • Only comparison operators

Frequently Asked Questions