Page 1 of 1

Can someone please explain the correct answer

PostPosted: Tue May 31, 2016 9:56 pm
by wandilly
Could someone please explain why the correct answer is B.60 instead of C.90?
Initially A=30, B=60
IF A = 40      
       DISPLAY 'ABC'  
 ELSE      
       NEXT SENTENCE
END-IF
MOVE 90 TO B.
DISPLAY B.
 

What is the output of the program?

A.
30

B.
60

C.
90

D.
Can’t perform the operation
Coded

Re: Can someone please explain the correct answer

PostPosted: Tue May 31, 2016 10:55 pm
by prino
I think it's 42, because that's the answer to everything.

Re: Can someone please explain the correct answer

PostPosted: Tue May 31, 2016 10:57 pm
by Akatsukami
In the future, please enclose code, data, etc. in Code tags so as to maintain alignment.

Now, as to the answer: Where does the next sentence begin after the NEXT SENTENCE clause?

Re: Can someone please explain the correct answer

PostPosted: Tue May 31, 2016 10:59 pm
by Akatsukami
prino wrote:I think it's 42, because that's the answer to everything.

Not true; I've been thanked 42 times, but I don't have the answer to everything :mrgreen:

Re: Can someone please explain the correct answer

PostPosted: Tue May 31, 2016 11:26 pm
by Terry Heinze
This is a good example of the potential problems with mixing NEXT SENTENCE and scope delimiters in the same program. Personally, I stopped using NEXT SENTENCE years ago in favor of scope delimiters. I also code only 2 periods per paragraph; one for the paragraph name and the other to end the paragraph.

Re: Can someone please explain the correct answer

PostPosted: Wed Jun 01, 2016 12:25 am
by Robert Sample
The Enterprise COBOL Language Reference manual should be your FIRST reference for such questions (if you don't have it bookmarked in your browser, you should), wandilly, and it says
NEXT SENTENCE
The NEXT SENTENCE phrase transfers control to an implicit CONTINUE statement immediately following the next separator period.
When NEXT SENTENCE is specified with END-IF, control does not pass tothe statement following the END-IF. Instead, control passes to the
statement after the closest following period.

Re: Can someone please explain the correct answer

PostPosted: Thu Jun 02, 2016 8:34 pm
by wandilly
Thanks everyone who commented on my question :))

I just copied and pasted the code from a online test that I was taking sometime ago

I am not sure if the code for that question was incorrect or if I was mistaking to think
that the "NEXT SENTENSE" should transfer control to the command immediately after the END-IF.

Thanks to Robert sample, I now know that the control passes to the
statement after the closest following period.

Again, thank you all very much !