GOTO

Posts 1–11 of 11 · Page 1 of 1
GOTO
Ok, this is a simple question. Though i have never felt the need to use goto, why is it 'evil'. I mean i can understand in cases like this where it hurts your brain to look at it :
Code:
//includes

int main() {
   int a, b, c;
   cin>>a;
   cin>>b;
   cin>>c;
   if(a == 0) goto one;
   else goto two;
   one:if(b == 0) goto nine;
   else goto ten;
   two:if(c == 0) goto six;
   else goto seven;
   nine:if(a > 0) goto two;
   //etc etc
}
Code from random person giving example of why goto is evil

But suppose i just want to use one or two, and it helps program flow rather than hinders it? I'm starting to become paranoid that one day i will need to use GOTO then everyone will eat my brain, or i will get run over by a raptor(If you figure out where this reference is from, you get thanked =D).
It basically just goes against the Object-Oriented Approach high level languages are all about. (of course if you look at assembly all you got is versions of 'goto' ) and it does basically make the code harder to follow etc.
Spaghetti anyone?

Anything that you may feel needs a goto can usually be rewritten without
one in a clearer fashion by either breaking the code into multiple methods or by nested if-statements etc. In all my years of coding I've NEVER had the use for a goto in C++

You CAN use it if you want (heck it's supported by C++ as a 'left over' feature) but usually it's simply not necessary.. so don't sweat it
Hmm i see. If you use it well i don't see a problem with them, then again i've never really seen them in use. Secondary question. When break is used, does it exit to then next statement in the first level in nested loop blocks?

PseudoCode:
Code:
{
    {
        {
            {
                 cout << stuff 0;
                 break;

                 cout << stuff 1;
            }
            
            cout << stuff 2;
        }
        
        cout << stuff 3;
    }
    
    cout << stuff 4;
}
Am i correct in thinking that stuff 0, and stuff 4 would be the only things to be displayed? I'm not exactly sure. Thanks for your help
is it just me or is the goto command in like every coding language
im not sure but I think break breaks out of the last while/for loop.
Code:
while(1)
{
    for(int i=0;i<100;i++)
    {
        if(i==66)
        {
            break;
        }
        if(i==72)
        {
            cout<<"i is now 72 o__O";
        }
    }
    //ends up here all the time so the 2nd if statement is never triggered i think
}
I've never seen a break out of an instance that wasn't a case, but you know what I believe try it out and find out!

Here's my code:
Code:
#include <iostream>
using namespace std;
int main()
{
    {
        {
            {
                 cout << "stuff 0";
                 break;

                 cout << "stuff 1";
            }
            
            cout << "stuff 2";
        }
        
        cout << "stuff 3";
    }
    
    cout << "stuff 4";
    cin.get();
    return 0;
}
And here's my error:
Code:
9 C:\Dev-Cpp\test break.cpp break statement not within loop or switch
I think it pretty much explains itself!
Quote Originally Posted by why06 View Post
I've never seen a break out of an instance that wasn't a case, but you know what I believe try it out and find out!

Here's my code:
Code:
#include <iostream>
using namespace std;
int main()
{
    {
        {
            {
                 cout << "stuff 0";
                 break;

                 cout << "stuff 1";
            }
            
            cout << "stuff 2";
        }
        
        cout << "stuff 3";
    }
    
    cout << "stuff 4";
    cin.get();
    return 0;
}
And here's my error:
Code:
9 C:\Dev-Cpp\test break.cpp break statement not within loop or switch
I think it pretty much explains itself!
Why06, there is something by the name of pseudocode, wherein you just type a general base. Put the cout's in loops that only run once then re comment. Breaks and continue's can only be used in loops >_<, or switches which is the cousin of a loop. =D
Quote Originally Posted by zeco View Post
Why06, there is something by the name of pseudocode, wherein you just type a general base. Put the cout's in loops that only run once then re comment. Breaks and continue's can only be used in loops >_<, or switches which is the cousin of a loop. =D
I know what psuedocode is >_> ... I just didn't know that was what you wanted. Well either way you got ur answer. gah! I'm gonna go crawl back under a rock.
doesn't break only quit with the code between the { } ?

so it would be
stuff 0
stuff 2
stuff 3
stuff 4

and is HD his 77 printed ( i think)
Nope - Curly Braces by themselves (not going with an if, or loop etc etc) don't mean anything. For instance:
Code:
    {
        {
              {
                    DoSomething();
              }
              DoSomethingElse();
         }
     }
To the compiler is no different than saying
Code:
DoSomething();
DoSomethingElse();
OK so i take it Hell's answer is correct ^_^
Posts 1–11 of 11 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Talk with us