MainframeMaster

COBOL Tutorial

COBOL ZEROS - Quick Reference

Progress0 of 0 lessons

Overview

ZEROS in COBOL is a figurative constant that represents the numeric value zero. It is used for initializing variables, performing comparisons, and representing zero in calculations. ZEROS is the plural form of the zero constant, with ZERO being the singular form and ZEROES being an alternative plural form, though all represent the same value.

Key Features

  • Numeric constant - Represents the numeric value zero
  • Initialization - Used to initialize numeric variables
  • Comparisons - Used in conditional statements and comparisons
  • Arithmetic operations - Can be used in mathematical calculations

Syntax and Usage

ZEROS usage patterns for initialization, comparisons, and calculations.

Basic ZEROS Initialization

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
IDENTIFICATION DIVISION. PROGRAM-ID. ZEROS-INITIALIZATION. DATA DIVISION. WORKING-STORAGE SECTION. 01 COUNTER PIC 9(4) VALUE ZEROS. 01 ACCUMULATOR PIC 9(8) VALUE ZEROS. 01 TOTAL-AMOUNT PIC 9(10)V99 VALUE ZEROS. 01 ITEM-COUNT PIC 9(3) VALUE ZEROS. PROCEDURE DIVISION. DISPLAY "Counter: " COUNTER DISPLAY "Accumulator: " ACCUMULATOR DISPLAY "Total Amount: " TOTAL-AMOUNT DISPLAY "Item Count: " ITEM-COUNT STOP RUN.

Basic ZEROS initialization of numeric variables.

ZEROS in Comparisons

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
IDENTIFICATION DIVISION. PROGRAM-ID. ZEROS-COMPARISONS. DATA DIVISION. WORKING-STORAGE SECTION. 01 AMOUNT PIC 9(6)V99 VALUE 150.75. 01 BALANCE PIC 9(8)V99 VALUE ZEROS. 01 TRANSACTION-COUNT PIC 9(4) VALUE 0. PROCEDURE DIVISION. * Check if amount is zero IF AMOUNT = ZEROS DISPLAY "Amount is zero" ELSE DISPLAY "Amount is not zero: " AMOUNT END-IF * Check if balance is zero IF BALANCE = ZEROS DISPLAY "Balance is zero" ELSE DISPLAY "Balance is not zero: " BALANCE END-IF * Check if transaction count is zero IF TRANSACTION-COUNT = ZEROS DISPLAY "No transactions processed" ELSE DISPLAY "Transactions processed: " TRANSACTION-COUNT END-IF STOP RUN.

ZEROS usage in conditional statements and comparisons.

ZEROS in Arithmetic 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
29
30
31
32
IDENTIFICATION DIVISION. PROGRAM-ID. ZEROS-ARITHMETIC. DATA DIVISION. WORKING-STORAGE SECTION. 01 VALUE-1 PIC 9(4) VALUE 100. 01 VALUE-2 PIC 9(4) VALUE ZEROS. 01 RESULT PIC 9(6). 01 QUOTIENT PIC 9(6). 01 REMAINDER PIC 9(4). PROCEDURE DIVISION. * Addition with ZEROS ADD ZEROS TO VALUE-1 GIVING RESULT DISPLAY "Value + ZEROS = " RESULT * Subtraction with ZEROS SUBTRACT ZEROS FROM VALUE-1 GIVING RESULT DISPLAY "Value - ZEROS = " RESULT * Multiplication with ZEROS MULTIPLY VALUE-1 BY ZEROS GIVING RESULT DISPLAY "Value * ZEROS = " RESULT * Division by ZEROS (error handling) IF VALUE-2 = ZEROS DISPLAY "Cannot divide by ZEROS" ELSE DIVIDE VALUE-1 BY VALUE-2 GIVING QUOTIENT REMAINDER REMAINDER DISPLAY "Division result: " QUOTIENT END-IF STOP RUN.

ZEROS usage in arithmetic operations with error handling.

Usage Patterns

Variable Initialization

Initialize numeric variables to zero.

cobol
1
2
3
01 COUNTER PIC 9(4) VALUE ZEROS. 01 TOTAL PIC 9(8)V99 VALUE ZEROS. 01 BALANCE PIC S9(10)V99 VALUE ZEROS.

Conditional Checks

Check if values are zero.

cobol
1
2
3
4
5
6
7
IF AMOUNT = ZEROS DISPLAY "Amount is zero" END-IF IF BALANCE NOT = ZEROS PERFORM PROCESS-BALANCE END-IF

Arithmetic Operations

Use ZEROS in calculations.

cobol
1
2
3
4
5
ADD ZEROS TO TOTAL MULTIPLY VALUE BY ZEROS GIVING RESULT IF DIVISOR = ZEROS DISPLAY "Division by zero error" END-IF

Loop Control

Use ZEROS for loop initialization.

cobol
1
2
3
4
5
MOVE ZEROS TO LOOP-COUNTER PERFORM UNTIL LOOP-COUNTER >= MAX-COUNT ADD 1 TO LOOP-COUNTER PERFORM PROCESS-ITEM END-PERFORM

Best Practices

  • Initialization - Use ZEROS to initialize numeric variables
  • Readability - Use ZEROS instead of literal 0 for better code readability
  • Error handling - Check for division by ZEROS to prevent runtime errors
  • Consistency - Use ZEROS consistently throughout your program
  • Documentation - ZEROS makes code self-documenting

ZEROS Quick Reference

UsageSyntaxPurpose
InitializationVALUE ZEROSInitialize variables to zero
ComparisonIF X = ZEROSCheck if value equals zero
ArithmeticADD ZEROS TO XUse in calculations
Error CheckIF DIVISOR = ZEROSPrevent division by zero

Test Your Knowledge

1. What is the primary purpose of ZEROS in COBOL?

  • To represent the numeric value zero in calculations and comparisons
  • To generate random numbers
  • To format output
  • To handle text strings

2. How does ZEROS compare to ZERO and ZEROES in COBOL?

  • ZEROS is the plural form, ZERO is singular, ZEROES is alternative plural
  • They are all exactly the same
  • ZEROS is faster than ZERO and ZEROES
  • ZEROS only works with integers

3. What is the main use case for ZEROS in COBOL?

  • To generate random numbers
  • To initialize variables, perform comparisons, and represent zero in calculations
  • To format output strings
  • To handle text processing

4. How does ZEROS behave in arithmetic operations?

  • It causes errors in all operations
  • It behaves as the numeric value zero in all arithmetic operations
  • It only works in addition
  • It only works in subtraction

5. What is a common use case for ZEROS in COBOL programs?

  • To generate random numbers
  • To initialize counters, accumulators, and perform zero-based comparisons
  • To format output
  • To handle text strings

Frequently Asked Questions