Stop Program from going to goto label

Nov 21, 2013 at 12:10am
Hello. My program has a goto label in it and when the program is not instructed to goto the label, it does. When control goes to the line that it is on, even if no where does it say to execute the label's content, it does execute it. Please instruct me on how to fix this. Thank you. Any help would be greatly appreciated!
Nov 21, 2013 at 2:55am
can you show us the code (using code tags please) :P
Nov 21, 2013 at 10:42am
Well, that's how labels work. When the natural flow of control reaches the line of the label, it simply proceeds to execute the code. Labels don't mean "only execute this code if the label is specifically referenced in a goto statement". It's just a label.

It sounds like what you really want is a function, not a goto label.

In fact, you should almost never use goto at all in C/C++. They almost always cause more problems than they solve. C/C++ provides many control constructs to write your program in an organised, structured way, and you use them instead of goto statements wherever possible.
Nov 21, 2013 at 10:46am

I agree with @MikeeyBoy - in C/C++ its best to avoid goto labels, the use of one indicates that the design is poor.

In assembly code you must use labels, but when you do, you either goto labelA OR goto labelB and thus avoid running the code at labelA when that isn't wanted i.e. code execution jumps over that code.
Nov 21, 2013 at 10:37pm
Ok thanks. what I'm really trying to do is to have an if statement in a loop in a loop break both loops.
Nov 21, 2013 at 11:19pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while(true)
{
    while(true)
    {
        if(condition == true) //same condition 
        {
            break;
         }
    }
    if(condition == true) //same condition
    {
        break;
    }
}
Nov 22, 2013 at 12:38am
Oh thanks! That works. Thank you!
Topic archived. No new replies allowed.