In COBOL programming, "UP" refers to the concept of upward counting and incrementing values in loops and iterative operations. While "UP" itself is not a reserved keyword in COBOL, it represents the fundamental pattern of counting upward (increasing values) in PERFORM VARYING statements, loop counters, and iterative processing. Understanding upward counting patterns is essential for processing arrays, generating sequences, and implementing forward iteration in COBOL programs.
Upward counting in COBOL means incrementing a variable's value from a lower starting point to a higher ending point. This is the most common pattern in loop control, where you process data in a forward direction, starting from the beginning and moving toward the end. Upward counting is typically implemented using:
The most common way to implement upward counting in COBOL is using PERFORM VARYING with a positive increment value in the BY clause:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061IDENTIFICATION DIVISION. PROGRAM-ID. UPWARD-COUNTING. AUTHOR. Mainframe Master. DATE-WRITTEN. 2024. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-COUNTER PIC 9(3) VALUE 0. 01 WS-TOTAL PIC 9(5) VALUE 0. 01 WS-ARRAY. 05 WS-ARRAY-ELEMENT PIC 9(3) OCCURS 10 TIMES. PROCEDURE DIVISION. MAIN-PROCESSING. DISPLAY "=== Upward Counting Examples ===". DISPLAY " ". *> Example 1: Count from 1 to 10 (counting UP) DISPLAY "Example 1: Counting from 1 to 10" PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10 DISPLAY "Counter value: " WS-COUNTER ADD WS-COUNTER TO WS-TOTAL END-PERFORM DISPLAY "Total of numbers 1-10: " WS-TOTAL DISPLAY " ". *> Example 2: Count up by 2 (even numbers) DISPLAY "Example 2: Counting even numbers (2, 4, 6, 8, 10)" MOVE 0 TO WS-TOTAL PERFORM VARYING WS-COUNTER FROM 2 BY 2 UNTIL WS-COUNTER > 10 DISPLAY "Even number: " WS-COUNTER ADD WS-COUNTER TO WS-TOTAL END-PERFORM DISPLAY "Total of even numbers: " WS-TOTAL DISPLAY " ". *> Example 3: Process array upward DISPLAY "Example 3: Processing array from first to last" PERFORM INITIALIZE-ARRAY PERFORM PROCESS-ARRAY-UPWARD STOP RUN. INITIALIZE-ARRAY. *> Initialize array with values 10, 20, 30, etc. PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10 COMPUTE WS-ARRAY-ELEMENT(WS-COUNTER) = WS-COUNTER * 10 END-PERFORM. PROCESS-ARRAY-UPWARD. *> Process array elements in upward order (1 to 10) PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10 DISPLAY "Array element " WS-COUNTER ": " WS-ARRAY-ELEMENT(WS-COUNTER) END-PERFORM.
Key Points:
The most basic upward counting pattern increments by 1 each time:
12345678910WORKING-STORAGE SECTION. 01 WS-INDEX PIC 9(3) VALUE 0. PROCEDURE DIVISION. *> Count from 1 to 100 sequentially PERFORM VARYING WS-INDEX FROM 1 BY 1 UNTIL WS-INDEX > 100 DISPLAY "Processing item " WS-INDEX *> Process each item END-PERFORM.
Count upward by a step value greater than 1:
1234567891011WORKING-STORAGE SECTION. 01 WS-INDEX PIC 9(3) VALUE 0. 01 WS-STEP-SIZE PIC 9(2) VALUE 5. PROCEDURE DIVISION. *> Count from 5 to 100 by steps of 5 PERFORM VARYING WS-INDEX FROM 5 BY WS-STEP-SIZE UNTIL WS-INDEX > 100 DISPLAY "Processing every 5th item: " WS-INDEX *> Process items at 5, 10, 15, 20, etc. END-PERFORM.
Use a variable to control the step size dynamically:
12345678910111213141516WORKING-STORAGE SECTION. 01 WS-INDEX PIC 9(3) VALUE 0. 01 WS-STEP-VALUE PIC 9(2) VALUE 3. PROCEDURE DIVISION. *> Step size can be changed based on conditions IF WS-CONDITION = 'Y' MOVE 2 TO WS-STEP-VALUE ELSE MOVE 5 TO WS-STEP-VALUE END-IF PERFORM VARYING WS-INDEX FROM 1 BY WS-STEP-VALUE UNTIL WS-INDEX > 50 DISPLAY "Index: " WS-INDEX " (step: " WS-STEP-VALUE ")" END-PERFORM.
You can also implement upward counting manually using ADD statements within PERFORM UNTIL loops:
1234567891011121314151617181920WORKING-STORAGE SECTION. 01 WS-COUNTER PIC 9(3) VALUE 0. 01 WS-MAX-VALUE PIC 9(3) VALUE 10. PROCEDURE DIVISION. *> Manual upward counting MOVE 1 TO WS-COUNTER PERFORM UNTIL WS-COUNTER > WS-MAX-VALUE DISPLAY "Counter: " WS-COUNTER ADD 1 TO WS-COUNTER *> Count UP by 1 END-PERFORM. *> Alternative: Count up by different amounts MOVE 0 TO WS-COUNTER PERFORM UNTIL WS-COUNTER >= 100 DISPLAY "Counter: " WS-COUNTER ADD 5 TO WS-COUNTER *> Count UP by 5 END-PERFORM.
Upward counting is essential for processing arrays and tables from the first element to the last:
1234567891011121314151617181920212223242526272829303132333435IDENTIFICATION DIVISION. PROGRAM-ID. ARRAY-UPWARD-PROCESSING. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-EMPLOYEE-TABLE. 05 WS-EMPLOYEE OCCURS 50 TIMES INDEXED BY EMP-IDX. 10 WS-EMP-ID PIC 9(5). 10 WS-EMP-NAME PIC X(30). 10 WS-EMP-SALARY PIC 9(6)V99. 01 WS-TABLE-SIZE PIC 9(2) VALUE 50. 01 WS-TOTAL-SALARY PIC 9(9)V99 VALUE 0. 01 WS-AVERAGE-SALARY PIC 9(6)V99 VALUE 0. PROCEDURE DIVISION. MAIN-PROCESSING. *> Process employee table from first to last (upward) PERFORM VARYING EMP-IDX FROM 1 BY 1 UNTIL EMP-IDX > WS-TABLE-SIZE *> Process each employee record ADD WS-EMP-SALARY(EMP-IDX) TO WS-TOTAL-SALARY DISPLAY "Employee " EMP-IDX ": " WS-EMP-NAME(EMP-IDX) " Salary: $" WS-EMP-SALARY(EMP-IDX) END-PERFORM *> Calculate average COMPUTE WS-AVERAGE-SALARY = WS-TOTAL-SALARY / WS-TABLE-SIZE DISPLAY "Total salary: $" WS-TOTAL-SALARY DISPLAY "Average salary: $" WS-AVERAGE-SALARY STOP RUN.
Understanding the difference between counting up and counting down helps you choose the right pattern:
| Aspect | Upward Counting | Downward Counting |
|---|---|---|
| Direction | Low to high (1, 2, 3...) | High to low (10, 9, 8...) |
| BY Value | Positive (BY 1, BY 2, etc.) | Negative (BY -1, BY -2, etc.) |
| Common Use | Forward processing, sequential access | Reverse processing, stack operations |
| Example | FROM 1 BY 1 UNTIL COUNTER > 10 | FROM 10 BY -1 UNTIL COUNTER < 1 |
Here's a complete example demonstrating various upward counting patterns:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124IDENTIFICATION DIVISION. PROGRAM-ID. COMPREHENSIVE-UPWARD-COUNTING. AUTHOR. Mainframe Master. DATE-WRITTEN. 2024. DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-CONTROLS. 05 WS-COUNTER PIC 9(3) VALUE 0. 05 WS-STEP-SIZE PIC 9(2) VALUE 1. 05 WS-START-VALUE PIC 9(3) VALUE 1. 05 WS-END-VALUE PIC 9(3) VALUE 20. 01 WS-DATA-ARRAY. 05 WS-ARRAY-ELEMENT PIC 9(4) OCCURS 20 TIMES. 01 WS-STATISTICS. 05 WS-SUM PIC 9(6) VALUE 0. 05 WS-COUNT PIC 9(3) VALUE 0. 05 WS-AVERAGE PIC 9(4)V99 VALUE 0. PROCEDURE DIVISION. MAIN-PROCESSING. DISPLAY "=== Comprehensive Upward Counting Example ===". DISPLAY " ". *> Example 1: Basic upward counting PERFORM BASIC-UPWARD-COUNT *> Example 2: Step-based upward counting PERFORM STEP-BASED-COUNT *> Example 3: Array initialization upward PERFORM INITIALIZE-ARRAY-UPWARD *> Example 4: Array processing upward PERFORM PROCESS-ARRAY-UPWARD *> Example 5: Conditional upward counting PERFORM CONDITIONAL-UPWARD-COUNT DISPLAY " ". DISPLAY "All upward counting examples completed." STOP RUN. BASIC-UPWARD-COUNT. DISPLAY "Example 1: Basic upward counting (1 to 10)" MOVE 0 TO WS-SUM MOVE 0 TO WS-COUNT PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10 DISPLAY " Counter: " WS-COUNTER ADD WS-COUNTER TO WS-SUM ADD 1 TO WS-COUNT END-PERFORM DISPLAY " Sum: " WS-SUM DISPLAY " Count: " WS-COUNT DISPLAY " ". STEP-BASED-COUNT. DISPLAY "Example 2: Step-based upward counting (by 3)" MOVE 0 TO WS-SUM MOVE 0 TO WS-COUNT PERFORM VARYING WS-COUNTER FROM 3 BY 3 UNTIL WS-COUNTER > 30 DISPLAY " Counter: " WS-COUNTER ADD WS-COUNTER TO WS-SUM ADD 1 TO WS-COUNT END-PERFORM DISPLAY " Sum: " WS-SUM DISPLAY " Count: " WS-COUNT DISPLAY " ". INITIALIZE-ARRAY-UPWARD. DISPLAY "Example 3: Initialize array upward" *> Fill array with values 10, 20, 30, etc. PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 20 COMPUTE WS-ARRAY-ELEMENT(WS-COUNTER) = WS-COUNTER * 10 END-PERFORM DISPLAY " Array initialized with values 10, 20, 30, ..." DISPLAY " ". PROCESS-ARRAY-UPWARD. DISPLAY "Example 4: Process array upward" MOVE 0 TO WS-SUM MOVE 0 TO WS-COUNT *> Process array from first to last element PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 20 ADD WS-ARRAY-ELEMENT(WS-COUNTER) TO WS-SUM ADD 1 TO WS-COUNT IF FUNCTION MOD(WS-COUNTER, 5) = 0 DISPLAY " Processed element " WS-COUNTER ": " WS-ARRAY-ELEMENT(WS-COUNTER) END-IF END-PERFORM COMPUTE WS-AVERAGE = WS-SUM / WS-COUNT DISPLAY " Total sum: " WS-SUM DISPLAY " Average: " WS-AVERAGE DISPLAY " ". CONDITIONAL-UPWARD-COUNT. DISPLAY "Example 5: Conditional upward counting" *> Count upward but skip certain values PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 15 *> Only process even numbers (counting up) IF FUNCTION MOD(WS-COUNTER, 2) = 0 DISPLAY " Processing even number: " WS-COUNTER END-IF END-PERFORM DISPLAY " ".
> (greater than) for "until counter exceeds limit">= (greater than or equal) if you want to include the limitWrong:
123*> This counts DOWN, not UP! PERFORM VARYING WS-COUNTER FROM 1 BY -1 UNTIL WS-COUNTER > 10
Correct:
123*> Use positive BY value for counting UP PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10
Wrong:
123*> This creates an infinite loop if start > end PERFORM VARYING WS-COUNTER FROM 10 BY 1 UNTIL WS-COUNTER < 1
Correct:
123*> Use > for upward counting PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10
Wrong:
12345*> Array has 10 elements, but counting to 20 01 WS-ARRAY PIC 9(3) OCCURS 10 TIMES. PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 20 DISPLAY WS-ARRAY(WS-COUNTER) *> Error!
Correct:
12345*> Count only up to array size 01 WS-ARRAY PIC 9(3) OCCURS 10 TIMES. PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10 DISPLAY WS-ARRAY(WS-COUNTER)
Imagine you're counting your toys:
When you count your toys, you start with toy number 1, then 2, then 3, and keep going up until you've counted all of them. That's "counting up" or "counting upward"!
In COBOL, when we want to process a list of things (like all your toys), we use "upward counting" to go through them one by one from the first item to the last item. It's like reading a book from page 1 to the end, instead of reading it backwards.
The computer starts at a small number (like 1) and keeps adding 1 to it (1, 2, 3, 4, 5...) until it reaches the end. This way, we make sure we look at everything in order, from the beginning to the end!
Write a COBOL program that:
Hint: Use PERFORM VARYING with FROM 1 BY 1 UNTIL COUNTER > 50
Create a program that:
Write a program that:
1. What does "counting up" mean in COBOL?
2. What BY value should you use for upward counting?
3. Which PERFORM VARYING statement counts upward from 1 to 10?
4. What is the most common use of upward counting?
5. What happens if you use a negative BY value in upward counting?