简体   繁体   English

关于C语言中的“ goto”的问题

[英]Question about “goto” in C

I am trying to understand a C code. 我正在尝试理解C代码。 In some part there is: 在某些方面有:

for ...{
    if condition{
       a=1;
       break;
    }
}

which in a later version is changed to: 在更高版本中将其更改为:

for ...{
    if condition{
       goto done;
    }
}
done: a=1;

From my point of view, both vesions should give the same result, but it does not happen. 以我的观点,这两个观点应得出相同的结果,但不会发生。 Do you know why? 你知道为什么吗?

CORRECTION: The fix is: 更正的方法是:

for ...{
    if condition{
       goto done;
    }
}

            goto notdone;
            done: 
                ok=0;
            notdone:

It depends on whether the for-loop has any other exit conditions. 这取决于for循环是否还有其他退出条件。

  • In the first example, a=1 only happens for that specific exit condition in the if-statement. 在第一个示例中,仅在if语句中的特定退出条件下发生a=1

  • In the second example, a=1 happens in all scenarios that exit the loop. 在第二个示例中,在退出循环的所有情况下发生a=1 It can only be circumvented using a return statement, or another goto statement. 只能使用return语句或其他goto语句来规避它。

在第二个版本中, 即使 condition为假,最终也会执行a=1 ,这仅仅是因为控制流最终已done:在不再满足循环条件之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM