Progress0 of 0 lessons

COBOL UP - Upward Counting and Incrementing

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.

What is Upward Counting in COBOL?

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:

Upward Counting with PERFORM VARYING

The most common way to implement upward counting in COBOL is using PERFORM VARYING with a positive increment value in the BY clause:

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
IDENTIFICATION 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:

Counting Up Patterns

1. Sequential Counting (1, 2, 3, 4...)

The most basic upward counting pattern increments by 1 each time:

cobol
1
2
3
4
5
6
7
8
9
10
WORKING-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.

2. Step Counting (2, 4, 6, 8...)

Count upward by a step value greater than 1:

cobol
1
2
3
4
5
6
7
8
9
10
11
WORKING-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.

3. Variable Step Counting

Use a variable to control the step size dynamically:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WORKING-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.

Manual Upward Counting

You can also implement upward counting manually using ADD statements within PERFORM UNTIL loops:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
WORKING-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.

Array Processing with Upward Counting

Upward counting is essential for processing arrays and tables from the first element to the last:

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
IDENTIFICATION 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.

Upward Counting vs. Downward Counting

Understanding the difference between counting up and counting down helps you choose the right pattern:

AspectUpward CountingDownward Counting
DirectionLow to high (1, 2, 3...)High to low (10, 9, 8...)
BY ValuePositive (BY 1, BY 2, etc.)Negative (BY -1, BY -2, etc.)
Common UseForward processing, sequential accessReverse processing, stack operations
ExampleFROM 1 BY 1 UNTIL COUNTER > 10FROM 10 BY -1 UNTIL COUNTER < 1

Complete Example: Upward Counting Application

Here's a complete example demonstrating various upward counting patterns:

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
IDENTIFICATION 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 " ".

Best Practices for Upward Counting

1. Choose Appropriate Starting Values

2. Set Correct Termination Conditions

3. Optimize Step Sizes

4. Validate Loop Boundaries

Common Mistakes to Avoid

Mistake 1: Using Negative BY Values for Upward Counting

Wrong:

cobol
1
2
3
*> This counts DOWN, not UP! PERFORM VARYING WS-COUNTER FROM 1 BY -1 UNTIL WS-COUNTER > 10

Correct:

cobol
1
2
3
*> Use positive BY value for counting UP PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10

Mistake 2: Incorrect Termination Condition

Wrong:

cobol
1
2
3
*> This creates an infinite loop if start > end PERFORM VARYING WS-COUNTER FROM 10 BY 1 UNTIL WS-COUNTER < 1

Correct:

cobol
1
2
3
*> Use > for upward counting PERFORM VARYING WS-COUNTER FROM 1 BY 1 UNTIL WS-COUNTER > 10

Mistake 3: Array Index Out of Bounds

Wrong:

cobol
1
2
3
4
5
*> 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:

cobol
1
2
3
4
5
*> 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)

Explain It Like I'm 5 Years Old

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!

Exercises

Exercise 1: Basic Upward Counting

Write a COBOL program that:

  • Counts upward from 1 to 50
  • Displays each number
  • Calculates and displays the sum of all numbers

Hint: Use PERFORM VARYING with FROM 1 BY 1 UNTIL COUNTER > 50

Exercise 2: Step-Based Upward Counting

Create a program that:

  • Counts upward from 5 to 100 by steps of 5
  • Displays only the numbers that are multiples of 5
  • Counts how many numbers were displayed

Exercise 3: Array Processing Upward

Write a program that:

  • Creates an array of 20 elements
  • Initializes each element with its index multiplied by 10 (10, 20, 30, ...)
  • Processes the array upward from first to last
  • Finds and displays the maximum value in the array

Quiz

Test Your Knowledge

1. What does "counting up" mean in COBOL?

  • A) Decreasing a variable's value
  • B) Increasing a variable's value from low to high
  • C) Setting a variable to zero
  • D) Reversing an array

2. What BY value should you use for upward counting?

  • A) Negative values (BY -1)
  • B) Positive values (BY 1, BY 2, etc.)
  • C) Zero (BY 0)
  • D) Any value works

3. Which PERFORM VARYING statement counts upward from 1 to 10?

  • A) PERFORM VARYING WS-C FROM 10 BY -1 UNTIL WS-C < 1
  • B) PERFORM VARYING WS-C FROM 1 BY 1 UNTIL WS-C > 10
  • C) PERFORM VARYING WS-C FROM 1 BY -1 UNTIL WS-C > 10
  • D) PERFORM VARYING WS-C FROM 10 BY 1 UNTIL WS-C < 1

4. What is the most common use of upward counting?

  • A) Processing arrays from last to first
  • B) Processing arrays from first to last
  • C) Reversing data
  • D) Stack operations

5. What happens if you use a negative BY value in upward counting?

  • A) The loop counts upward correctly
  • B) The loop counts downward instead
  • C) The loop doesn't execute
  • D) A compilation error occurs

Related Pages