goto statement

Feb 8, 2010 at 3:35pm
Hello Experts,

I am not a programming savvy. I need a help from you guys

can someone help me to understand the negative implications of goto statement. what is bad about it.

any help is greatly appreciated
Feb 8, 2010 at 4:06pm
Google "goto considered harmful".
Feb 8, 2010 at 4:07pm
Here is an example of how it can be used badly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main() {
    int a = 0, b = 0, c = 0;
    cin >> a >> b >> c; //assume error checking etc
start:
    if(a > 6) goto a;
    else if(b < 2) goto b;
    goto 5;
l1:
    goto start;
b:p:
    if(c == 5) goto z;
a:
    if(a < 6 && b > 1) goto aa;
    else goto p;
z:aa:
    goto g;
g:
    cout<<(a+b+c)%5<<endl;
    return 0;
}
Last edited on Feb 8, 2010 at 5:36pm
Feb 8, 2010 at 5:09pm
I believe labels have to be identifiers.
Feb 8, 2010 at 5:10pm
Can of worms.

Here's some more reading for you.
http://www.cplusplus.com/forum/beginner/2287/


[edit] In C and C++, yes, they have to be identifiers. In Pascal, they can be numbers.
Last edited on Feb 8, 2010 at 5:11pm
Feb 10, 2010 at 8:33am
You can also goto pointers (I think that's because in asm, you can jump to addresses (actually, all jumps to identifiers are precomputed to memory addresses IIRC); e.g. jmp 0x1000 (near jump) or jmp 8:0x1000 (far jump)).

Quote from K&R:
Pointers have also been lumped with goto to make impossible to understand programs.
(something like that, anyway).
Feb 10, 2010 at 11:09am
That's a GCC extension.
Feb 10, 2010 at 3:17pm
Are you talking about this? http://www.cplusplus.com/forum/general/13637/
Feb 10, 2010 at 3:37pm
Yes.
Topic archived. No new replies allowed.