Goto Command is bad?

Pages: 123
Well, then again, maybe you are right. It was a quick thought that jumped into my brain. But then again, I might be right. :D After all, I'm quite good at this, ya know? :-D I rarely have to think small code designs twice. This is how I won second place at a programming contest against the clock.
I'd love to see a post here showing the assembly of a goto and the assembly of a function code.
1
2
goto label 
bra label
1 instruction, 2 clock cycles.
A subrutine (no arguments, possibly return (byte) in work register)
call subrutine 1 instruction, 2 clock cycles (however you must have a 'return')

¿what do you want to compare?

Function inlining.
Memory limit.
Well, I wanted to know the exact procedure of a function called, and then compare it to that of a goto. It was said that it was more costly. I wanted to know exactly how much costly. So it seems that it is just one additional instruction? How many clocks? Pardon the question if it is too obvious for you. I know nothing about this subject so even finding out the clocks is a no-no for me. :P

Of course, if you feel like not answering I guess I can still google it up. May take a while, but I suppose it will appear out there somewhere.
@webJose
Your commentary is totally out of line, and you twist what little facts you have. Knowledgeable people in the computer industry don't abolish the goto because they know what it is used for.

Fanatics who stand around and say, as you do, that "It is pure evil and everyone will hate you for it" are arguing fallacious nonsense. To proclaim that you should "NEVER" use it is hubris beyond your apparent qualification.

So far, you have argued using bandwagon, fallacious appeal to authority, false analogy, red herrings, misrepresentation of fact, and ad hominem. You refuse to read or accept any argument given against your position and then have the audacity to claim that everyone else is failing to prove against your argument from ignorance.

No one is going to take the time to educate you about mission critical systems engineering and extant hardware design.

You typically are very helpful here on the forum. You need to accept that your own blind bias against the goto is suspect.

BTW, I have already stated my opinions on the goto:
http://www.cplusplus.com/forum/beginner/2287/#msg8648
When using call instruction the return address is pushed onto the stack, before changing the program counter. That doesn't happen with goto.
Some implementations also push-pop the work, status and bank registers.
Both are 1 instruction, 2 clock cycles. *

So there is a difference in the program flow. With goto you end wherever you want, with call you execute the next instruction.

* PIC
I wrote a little program to solve a maths problem earlier today that used a goto. It was something along the lines of...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
while(condition)
{
    while(condition)
    {
        while(condition)
        {
            if(solution found)
            {
                goto end;
            }
        }
    }
}
end:
return 0;


goto seemed easy and useful here.
@Mats

I think the general consensus here is that there is room for goto however structured programming is preferred when possible. In your example, I would suggest sticking your while conditions into a void function and then replacing goto end; with a simple return;. However I agree that your sudo-code looks easy to read/rewrite/debug.
Well, sorry for not dropping by earlier. I missed Duoas' untimely intervention.

Without trying to make a big fire here I'll just try to set the record straight:

Duoas, first and foremost, my first 2 or 3 comments were meant to be humorous. If you or anyone else didn't get it, well, then I guess I won't be leaving my job to pursue comedian. Therefore your attack on that front is pretty much moot and I really can't care less.

Second, if I am filling the thread with fallacies, don't just say "your arguments are fallacious". That brings nothing to the table. Prove it. Say "this is a fallacy because...". Did it ever occur to you that in fact I have little knowledge and my conclusions are simply misunderstanings instead of attempts to skew things "my way"?? I bet you didn't, so next time don't be so quick in drawing out your pointing finger. I hereby guarantee you that I have not tried to skew things by means of fallacies intentionally. If you see a flaw in my logic (the definition of a fallacy), then call it. Otherwise your words are just words. One more opinion in the bunch. And also for the record: I have never said my knowlege is infinite. I may very well be lacking key information here and anyone can feel free to correct me as it has happened in the past.

Also note that I am not asking anyone to do teach me anything, especially you, so get over yourself. I externed my opinion that happened to contradict somebody else's, and that somebody else got mad for no reason. This has nothing to do with you. If you want to express your opinion on something in particular, go right ahead, but don't act like I'm asking a favor from you and don't put words in my mouth. Again: I didn't ask anything from you. You don't have to take the time for ANYTHING on my behalf or on my account.

And finally I'll summarize what happened here so you can easily call on my fallacies:

1. I posted a humorous couple of replies about goto.
2. CodeMonkey posted something that hinted there were cases goto was valid. Since I've never seen such a thing I just teased about it some more.
3. Moschops appeared along and stated 1 frame was life-threatening. That wow'ed me. But let's be honest: I bet other readers found this extreme as well. They just aren't saying it.
4. I said: I find it hard to believe. Too bad there is no hard facts that you can provide.
5. Then I added: My light bulb just turned on: Function inlining eliminates the need of goto and eliminates the frame problem and therefore the validity of goto in this case.
6. Moschops attacks me for just one reason that I can think of: His goto use case went down the drain and he/she is mad about that. So he/she decides it is best to call me an unintelligent person and compares me to a religious fanatic. Ha! Go figure. Aren't religious fanatics the ones that believe in things that can't be proven? Weren't I asking for proof?? That's a fallacy right there, on Moschops' part. That is skewed for sure, wouldn't you say?

And Moschops: Insulting someone and then trying to act like a good guy taking the "high road" and not saying anything on a FIGHT THAT YOU STARTED is not scoring you any points. The high road is not that high in this case. Sorry to break it to ya, mate.

In my eyes, I was attacked for no other reason than having a different opinion on something. I stated my contrary opinion without insulting anyone and I got insults back.

So there. Call on my fallacies please.
First of all I am glad for all the info. Here is what I am getting-->
The goto code can be used, but it should only be done sparingly.
If I have a lot of loops deep within other loops the goto command is a good way to exit the loop and return to a certain point in the program. Other than that it should be avoided due to memory and stacking issues.

PS:With regard to the bolded area I don't know that much about stacking issues. I know memory can lead to a "leaking" issue, but I have not gotten far enough along to start manipulating memory that, but I do plan on looking inot it later.

PS2:Could we disagree politely? :)
Last edited on
Regarding the bold part, ¿eh? AFAIK the jumps can't bypass constructors, and destructors are called when leaving its scope.
¿Can anyone provide an example?
This is a bogus example - but to me illustrates certain weaknesses of using the goto statement w.r.t. compilers and the logical flow of the code layout. I tried this on vs 2008 and it compiled but crashed!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
   char ch;
   cout << "Enter ch: ";
   cin >> ch;


   if (ch == 'H') goto hell;
   int x = 1;
   x = x * 2;
   cout << "x = " << x << endl;

hell:
   x--;
   cout << "x' = " << x << endl;


The GNU GCC compiler on windows threw an error - I suspect the one on linux will also - surprisingly the ancient vs6 compiler also threw an error correctly while compiling.

I guess such types of discrepancies will make compiler developers give you the feeling of [Y-e-s the goto statement is there but rather try using ...] - sort of like when one directs ones users of usage of own code. You would give them some indication of things that may not be so great to try, but will offer better alternatives.


lmfaooo wow webJose is eithering trying to troll hella hard or he is that ignorant...

SIK thats a weird of way of using goto in my opinion... ( but I know you just trying to get a point across) :)

But anyways back on topic goto like many things has its advantages and its disavantages. The reason why people say is it use sparingly is because if you have a lot of jumps it will become harder to follow. Also anyone can argue that the difficulty of following the code can be due to the skill of the programmer.

@concerro just think of what would happen if you you where chasing after someone and he jump 10 yards behind you... then you turn and start following him and then he jump 50 yards further ahead..... then 40 yards back, and etc... you can see how that could become a headache.

I think as computer languages developed goto was replaced by control loops and function calls as a more sophisticated and structured way to organise program flow. So goto has always been viewed as an inferior, awkward solution.

However there still exist a (small) class of problems for which goto is the more elegant solution. This fact seems to have gotten lost in the drive to turn people away from the 'goto' style of programming and onto more advanced 'structured' programming techniques.

I think it is probably good that programmers who are learning are steered well away from goto. That is because goto is frequently not the best way to achieve your goal. In fact often it is a terrible solution.

Therefore I would say that goto is probably much better left to the 10th dan black-belt programmers extraordinaire.

Last edited on
SIK thats a weird of way of using goto in my opinion... ( but I know you just trying to get a point across) :)


No arguments there - I totally agree - however if anyone else has tried the same and found the code to compile successfully on vs2008 but crashes when you run it, then you might also share the same concerns I do.

ie, This is scary for me in the sense that vs2008 is usually considered to have a good c++ compiler and even if MS were to fix it with an update, I would still be cautious of using the goto statement, due to my concerns of whether this was an innocent bug in compiler or something deeper relating to an architectural flaw of goto not being well defined.

As pointed out - goto was replaced by control loops and function calls which would kind of imply that goto was 'depracated' - If this was the case in the vs2008 compiler then who is to say the same isn't true for some other compilers out there - ie there goto statements may not also be well defined and produce other discrepancies then the one listed here.
11: jumpt to label 'hell'
6: from here
7: crosses initialization of 'int x'
http://stackoverflow.com/a/2392693
They screw it up. You may want to check out some flags (geez, it compiles void main() ¿does it?)
Stormhawk wrote:
lmfaooo wow webJose is eithering trying to troll hella hard or he is that ignorant...


Do you have a problem with me? State it. We'll discuss it.
Do you have a problem with me? State it. We'll discuss it.


Personal issues to PM please.
Ok. Then PM me your problem and we'll discuss privately. Fair enough.
my comment was just from what I saw you say things like they didn't do this and that and while I read through it from begin to end things you were saying was never said you were blatantly ignoring them.. thats why I said you are either trolling for the lolz or you are truely ignorant in trying to open your mind to see it in another light
I did read each and every post made. I took into consideration the statement given by Moschops and I believe I provided a viable alternative. Instead of a reasonable debate on the topic at hand (goto), my partner-in-debate decided it was best to insult me and leave as opposed to enlight me why my alternative was no good. Do you call that trolling? Or am I missing something here that you seem to so clearly see? Read the summary of the events I posted for Duoas to criticize (which by the way seems to be another guy that just insults and leaves) and let me know if you disagree in any of the points I enumerate. You tell me if I am mistaken in thinking that I was insulted just because I had a different opinion.
Pages: 123