Page 1 of 1

Help with While Loop

PostPosted: Fri Aug 20, 2010 2:16 pm
by skurakid
When I run the following code, for some reason, it only loops on the statement "DISPLAY "Do a calculation? Y or N"." and I'm never given the chance to input for the statement "ACCEPT calculateBool.". Anybody know what's the problem? Thanks!


PERFORM UNTIL calculateBool = "N"                   
                                                   
DISPLAY "Do a calculation? Y or N".                 
ACCEPT calculateBool.                               
                                                   
DISPLAY "Enter the number of calcs requires : ".   
ACCEPT numOfCalcs.                                 
                                                   
DISPLAY "Enter first number: ".                     
ACCEPT firstNum.                                   
                                                   
DISPLAY "Enter second number: ".                   
ACCEPT secondNum.                                   
                                                   
DISPLAY "Enter operator: ".                         
ACCEPT operator.                                   
                                                   
IF operator = "+" THEN                             
    ADD firstNum, secondNum GIVING result           
END-IF                                             
IF operator = "*" THEN                             
    MULTIPLY firstNum BY secondNum GIVING result   
END-IF                                             
                                                   
DISPLAY "Result is = ", result.                     
                                                   
STOP RUN.                                           


Re: Help with While Loop

PostPosted: Fri Aug 20, 2010 4:17 pm
by NicC
Try removing all the periods except the one after DISPLAY result.

Re: Help with While Loop

PostPosted: Fri Aug 20, 2010 8:20 pm
by skurakid
Hi. Thanks! Removing all the periods within the PERFORM statement fixed the problem! I'm guessing any statement that isn't guaranteed to be executed shouldn't have a period after it.

procedure division.                                       
                                                         
   PERFORM UNTIL calculateBool = "N"                     
                                                         
   DISPLAY "Do a calcultion? Y or N"                     
   ACCEPT calculateBool                                   
                                                         
   DISPLAY "Enter the number of calcs requires : "       
   ACCEPT numOfCalcs                                     
                                                         
   DISPLAY "Enter first number: "                         
   ACCEPT firstNum                                       
                                                         
   DISPLAY "Enter second number: "                       
   ACCEPT secondNum                                       
                                                         
   DISPLAY "Enter operator: "                             
   ACCEPT operator                                       
                                                         
   IF operator = "+" THEN                                 
       ADD firstNum, secondNum GIVING result             
   END-IF                                                 
   IF operator = "*" THEN                                 
       MULTIPLY firstNum BY secondNum GIVING result       
   END-IF                                                 
                                                         
   DISPLAY "Result is = ", result                         
                                                         
   STOP RUN.                                             


Re: Help with While Loop

PostPosted: Fri Aug 20, 2010 8:38 pm
by Robert Sample
You're guessing wrong. COBOL IF, EVALUATE, PERFORM statements (and so forth) continue until terminated -- either by a scope terminator such as END-IF, END-EVALUATE, END-PERFORM, or by a period. You would be wise to start using scope terminators to explicitly indicate the range of the statement. If COBOL disagrees with you over the scope terminator, you at least have a compile error to fix instead of a run-time error.

Re: Help with While Loop

PostPosted: Fri Aug 20, 2010 10:24 pm
by skurakid
Thank you Robert. That clears everything up :).