0

Could anyone look at this and tell me if I got it right? I had to rewrite the following code without GOTOs:

Y:  A
     B
     if a GOTO X
     C
     if b GOTO Y
X:  D

Here is my solution (I added an additional flag to get it done):

i=TRUE
while i
  A
  B
  if not a then
    i=FALSE
  else
    C
    if b then
      i=FALSE
D

Please correct me if I'm doing something wrong.

Thanks, Waldis

2
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by waldis
0

Okay, let's make some observations on the goto code:

Y:  A
     B
     if a GOTO X
     C
     if b GOTO Y
X:  D

First, Y is always executed at least once because the test to GOTO Y is at the end of the "block". That suggests a loop that tests the condition at the end rather than at the beginning, so we'll throw in a do..while for Y (# denotes a comment):

#Y:
DO
  A
  B
  if a GOTO X
  C
WHILE b
X: D

If you're not allowed to use a special loop like that in your pseudocode, then you have the right of it in setting a flag to a successful condition for the first iteration.

GOTO X is a classic break from the loop, so if your pseudocode can support it, that's the easiest way:

#Y:
DO
  A
  B
  if a BREAK
  C
WHILE b
D

However, even though that's likely the solution that you would take in real code, BREAK is akin to GOTO in that it makes an unconditional jump, so it might not be an acceptable solution in this case. The alternative is what you did, to use flags to drive the loop:

#Y:
done = false
DO
  A
  B
  if a done = true
  C
WHILE b AND done <> true
D

All in all, this saves you a potentially confusing conditional branch inside the loop. But your solution is still correct. :)

0

Thank you. If I'm correct there should be an else clause before C. Let me rewrite the pseudocoude the way I'm supposed to submit it:

done=FALSE
repeat
  A
  B
  if a then
    done=TRUE
  else
    C
  while b AND done NOT TRUE
D

I hope I'm not missing the mark by adding 'else' in there, since if done becomes TRUE it has to skip (exclude) C.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.