Question

I am trying to figure out why my perform is not being executed?

It looks like to me that all of the variables are set the way they are supposed to be but the loop still is never being executed.

Question: Loop is never being entered, why?

Code:

C-MATCH.                                                   
    MOVE 0 TO SUB.                                             
    DISPLAY 'BEFORE PERFORM'.                                  
    DISPLAY 'TABLECOUNTER ' TABLECOUNTER.                      
    DISPLAY 'SUB ' SUB.                                        
    PERFORM VARYING SUB FROM 0 BY 1 UNTIL SUB < TABLECOUNTER   
           DISPLAY 'BEFORE IF STATEMENT'                       
      IF PROVM(SUB) <= P-PROVIDER AND                 
         P-PROVIDER <= PTHRU(SUB) THEN                
           DISPLAY 'FOUND'                                     
         ADD 1 TO T-REC-FOUND                                  
      END-IF                                                   
    END-PERFORM.                                               
X-C-MATCH. EXIT. 
Was it helpful?

Solution

A few things to point out here.

First COBOL subscripts start with 1 when referenceing table elements. If you are used to languages such as Java and C, subscripts generally start with 0. A subscript of 0 is out of bounds and can cause all kinds of wierd behaviour if you do not have bounds checking turned on.

Next, your condition for loop termination is wrong. You may be used to DO WHILE some-condition-is-true. In COBOL we generally PERFORM UNTIL some-condition-is-true, meaning stop performing when the condition becomes true. So look at:

PERFORM VARYING SUB FROM 0 BY 1 UNTIL SUB < TABLECOUNTER   
       DISPLAY 'BEFORE IF STATEMENT'                       
  IF PROVM(SUB) <= P-PROVIDER AND                 
     P-PROVIDER <= PTHRU(SUB) THEN                
       DISPLAY 'FOUND'                                     
     ADD 1 TO T-REC-FOUND                                  
  END-IF                                                   
END-PERFORM.      

Since SUB is initialized to zero, and I presume TABLECOUNTER is 1 or greater, the condition is true so the loop body is never executed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top