Help with While Loop



Support for OS/VS COBOL, VS COBOL II, COBOL for OS/390 & VM and Enterprise COBOL for z/OS

Help with While Loop

Postby skurakid » Fri Aug 20, 2010 2:16 pm

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.                                           

skurakid
 
Posts: 3
Joined: Fri Aug 20, 2010 3:19 am
Has thanked: 0 time
Been thanked: 0 time

Re: Help with While Loop

Postby NicC » Fri Aug 20, 2010 4:17 pm

Try removing all the periods except the one after DISPLAY result.
The problem I have is that people can explain things quickly but I can only comprehend slowly.
Regards
Nic
NicC
Global moderator
 
Posts: 3025
Joined: Sun Jul 04, 2010 12:13 am
Location: Pushing up the daisies (almost)
Has thanked: 4 times
Been thanked: 136 times

Re: Help with While Loop

Postby skurakid » Fri Aug 20, 2010 8:20 pm

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.                                             

skurakid
 
Posts: 3
Joined: Fri Aug 20, 2010 3:19 am
Has thanked: 0 time
Been thanked: 0 time

Re: Help with While Loop

Postby Robert Sample » Fri Aug 20, 2010 8:38 pm

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.
Robert Sample
Global moderator
 
Posts: 3719
Joined: Sat Dec 19, 2009 8:32 pm
Location: Dubuque, Iowa, USA
Has thanked: 1 time
Been thanked: 279 times

Re: Help with While Loop

Postby skurakid » Fri Aug 20, 2010 10:24 pm

Thank you Robert. That clears everything up :).
skurakid
 
Posts: 3
Joined: Fri Aug 20, 2010 3:19 am
Has thanked: 0 time
Been thanked: 0 time


Return to IBM Cobol