Goto Statements

I'm reading through some legacy code trying to figure out what it's doing. I've run into some goto statements and don't know exactly what they are doing.

Here is an example of the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 88 DO_SOMETHING:
 89     if (value == 0)
 90         goto EOJ;
 91
 92     if (value == 10)
 93         goto EOJ;
 94
 95 DO_SOMETHING_ELSE:
 96     if (someOtherValue == 0)
 97         goto SOMEWHERE;
 98
 99 EOJ:
100     if (somethingHappened == 1)
101         doSomething();
102
103     else
104         doSomethingElse();


This, of course, isn't the actual code but it will suffice for what I'm asking.

So, suppose it is the case that value == 0 at line 89. The next execution after the goto would be on line 100. After the contents of EOJ are satisfied what happens? Do we go back up to line 92 or do we continue to line 105?

That is, once a goto has "gone to" does it come back afterward (like you would expect if you called a function) or does it continue until it hits a return?

Any help would be appreciated.

Caveat: I don't like goto statements and would never use them. I'm in a situation where they are in existing code (which I'm trying to update) so I need to understand how they work.
That is, once a goto has "gone to" does it come back afterward

How could it? How is it supposed to know when "afterwards" is? There is no block anywhere.

or does it continue until it hits a return?

goto jumps to the specified label. No more, no less.
That is, once a goto has "gone to" does it come back afterward (like you would expect if you called a function) or does it continue until it hits a return?

It doesn't go back. In your example, it goes on to line 105.

As you probably know, goto statements are frowned upon, and shouldn't be used at all. They make code hard to follow, write and debug and it's really rare to come by a use which couldn't be replaced by a function or a loop.
Ok, I didn't expect that it wouldn't come back afterward. However, what would it do in the case that value = 15?

Would it move into DO_SOMETHING_ELSE if it didn't explicitly get a goto DO_SOMETHING_ELSE?

I'm thinking it would just continue into DO_SOMETHING_ELSE without prejudice. Is that right?
Yes, that's correct.
This kind of spaghetti code is exactly why people say you shouldn't use goto. What a mess.

Where did you find this code?
ricomoss,

Back in the day when I was taking my first programming course using BASIC, we used the goto a good deal. As I remember it, we usually used it to invoke a subroutine. I seem to remember that either goto or subroutine would return the 'pointer' to the line after the goto or subroutinestatement. But I think that was due to specific instruction and not a default setting.

As I am learning in C++, it seems that subroutines of BASIC are the functions of C++. So I would imagine that the goto statement should be replaced by a function call. And of course, a function call returns to the line of its call be default.
closed account (z05DSL3A)
Using gotos from Code Complete: A Practical Handbook of Software Construction
by Steve McConnell

http://www.stevemcconnell.com/ccgoto.htm


Computer scientists are zealous in their beliefs, and when the discussion turns to gotos, they get out their jousting poles, armor, and maces, mount their horses, and charge through the gates of Camelot to the holy wars.
Topic archived. No new replies allowed.