Can someone please explain the correct answer



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

Can someone please explain the correct answer

Postby wandilly » Tue May 31, 2016 9:56 pm

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
wandilly
 
Posts: 2
Joined: Thu May 26, 2011 1:22 am
Has thanked: 4 times
Been thanked: 0 time

Re: Can someone please explain the correct answer

Postby prino » Tue May 31, 2016 10:55 pm

I think it's 42, because that's the answer to everything.
Robert AH Prins
robert.ah.prins @ the.17+Gb.Google thingy

These users thanked the author prino for the post:
wandilly (Thu Jun 02, 2016 8:25 pm)
User avatar
prino
 
Posts: 635
Joined: Wed Mar 11, 2009 12:22 am
Location: Vilnius, Lithuania
Has thanked: 3 times
Been thanked: 28 times

Re: Can someone please explain the correct answer

Postby Akatsukami » Tue May 31, 2016 10:57 pm

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?
"You have sat too long for any good you have been doing lately ... Depart, I say; and let us have done with you. In the name of God, go!" -- what I say to a junior programmer at least once a day

These users thanked the author Akatsukami for the post:
wandilly (Thu Jun 02, 2016 8:26 pm)
User avatar
Akatsukami
Global moderator
 
Posts: 1058
Joined: Sat Oct 16, 2010 2:31 am
Location: Bloomington, IL
Has thanked: 6 times
Been thanked: 51 times

Re: Can someone please explain the correct answer

Postby Akatsukami » Tue May 31, 2016 10:59 pm

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:
"You have sat too long for any good you have been doing lately ... Depart, I say; and let us have done with you. In the name of God, go!" -- what I say to a junior programmer at least once a day

These users thanked the author Akatsukami for the post:
wandilly (Thu Jun 02, 2016 8:36 pm)
User avatar
Akatsukami
Global moderator
 
Posts: 1058
Joined: Sat Oct 16, 2010 2:31 am
Location: Bloomington, IL
Has thanked: 6 times
Been thanked: 51 times

Re: Can someone please explain the correct answer

Postby Terry Heinze » Tue May 31, 2016 11:26 pm

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.
.... Terry
Terry Heinze
 
Posts: 239
Joined: Wed Dec 04, 2013 11:08 pm
Location: Richfield, MN, USA
Has thanked: 12 times
Been thanked: 11 times

Re: Can someone please explain the correct answer

Postby Robert Sample » Wed Jun 01, 2016 12:25 am

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.

These users thanked the author Robert Sample for the post:
wandilly (Thu Jun 02, 2016 8:35 pm)
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: Can someone please explain the correct answer

Postby wandilly » Thu Jun 02, 2016 8:34 pm

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 !
wandilly
 
Posts: 2
Joined: Thu May 26, 2011 1:22 am
Has thanked: 4 times
Been thanked: 0 time


Return to IBM Cobol

 


  • Related topics
    Replies
    Views
    Last post