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.
BOOLEAN follows specific syntax patterns for logical data handling:
1234567891011121314151617* 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.
12345678910111213141516171819202122232425262728* 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.
Here are some practical uses of BOOLEAN in COBOL:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364* 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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859* 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.
1. What is BOOLEAN in COBOL?
2. What values can a BOOLEAN variable hold?
3. How is BOOLEAN typically declared in COBOL?
4. What is the primary use of BOOLEAN in COBOL?
5. What operators can be used with BOOLEAN values?