I’m currently taking an Introduction to Programming class online. My classmates and I are having a debate on something and the professor hasn’t been around to clarify. Curiosity is getting the best of me, so I’m asking here.
(This doesn’t run afoul of the ‘No homework’ rule because my assignment is already complete and submitted. And as this is my first experience with computer programming, please - use small words and assume I’m an idiot. Because I am.)
Here’s the question:
In the following, to what line does the return statement go?
start
a = 2
b = 4
c = 10
while c > 6
perform changeBAndC()
endwhile
if a = 2 then
perform changeAAndB()
endif
if c = 10 then
perform changeAAndB()
else
perform changeBAndC()
endif
print a, b, c
stop
changeBAndC()
b = b + 1
C = C - 1
return
changeAAndB()
a = a + 1
b = B - 1
return
Let’s just look at the first part:
while c > 6
perform changeBAndC()
endwhile
…
changeBAndC()
b = b + 1
C = C - 1
return
I’m confused on this point:
A. Does the return statement return control back to the while line after performing the change once, where it re-evaluates if c > 6? This would mean method changeBAndC() runs four separate times, each time returning to the while line.
Or,
B. Does it run once, looping four times, and returning to the endwhile line?
In other words, is the return statement at the end of changeBAndC() executed once or four separate times.
Or,
C. Does it do something else I haven’t thought of?
Our book shows an example of the return statement, and it points to a place in the main program after the method. This makes me think the answer is B. But if that’s the case, how does the method know to run until c <= 6?
Am I making any sense?