A question on how goto statement works

Here's the thing. I've written some code like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
if (statement1)
{
if (statement2)
{
    code1
}
else
mark1:if(statement3)
    {
     code2
    }
    else
    {
       code3
    }
}
else
{
    code4
}

if(statement4)
{
    goto mark1;
}


And I'm confused if the program would work in the way I'm expecting it to. I'm expecting the program to work on all the statement below that mark again and again until statement4 is no longer true.

And all my confuse came from my lack of understanding on how goto statement really work. Is it simply go to that line and run, which is a bad statement because the structure would be not complete, or is it load the structure where the mark is located and run from that mark forth?
Last edited on
For future reference, goto statements are very bad programming practices. there is almost always a better way of solving. For example you could put this in a function and re-call itself whenever statement 4 is correct.
Right, goto is a bad practice. Do not use it unless it's required for hw.
Well, the problem is, when statement4 is correct, I have to run only code2, code3 and code4... So the structure is a bit complicated...
That is a problem then, because that's not what your code does.
I know I'm just echoing everyone else here.... but...

And I'm confused if the program would work in the way I'm expecting it to


Which is exactly why people say you shouldn't use goto. It's confusing.
Indentation helps make things clearer, there are plenty of sources if you search for them, and different styles of doing it, but essentially your code would be less confusing if it was written like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
if (statement1)
{
    if (statement2)
    {
        code1
    }
    else
    if(statement3)
    {
        mark1: code2
    }
    else
    {
        code3
    }
}
else
{
    code4
}

if(statement4)
{
    goto mark1;
}


learn how to use while/for loops, 1000% better than goto, and learn about "else if" as well, doing else and then if on another line is a waste of a line.
OK, thanks for everyone, but I'm not stupid enough to be unaware of for loop or while loop or that shiny else if. And I don't use them for a reason. And I know that 'goto' command is what every programmer should avoid, but again, I use it for a reason.

As for the problem itself. I tested and find the result seemingly like this: the if - else structure form line 8 to line 15 will be ran.but code for will be totally skipped, seemingly because that else is part of the if statement, and won't be run when the 'if' statement is ran. I use seemingly, because I don't know how those statements really work. I simply did the experiment and checked the result.

As those single "}" brackets might be confusing. I suggest any one who do have to use the "goto" statement place the marks out side of all structure, perhaps on the first layer of the function. And my example on top, that is a typical go-home-and-do-it-again example.

And again, it's only a suggestion. I don't know how those statements really work, and actually I was expecting someone who know to answer me. But... it's OK. Like people said, avoid it if you can. And don't use it before you know what you actually are doing. Do some little test on it first, because it can be really confusing before you know what it really is.
I got it. And I know now how while loop can replace "goto". And it works for my case too. Sorry for my ignorance before.
Topic archived. No new replies allowed.