goto 语句不能跨越变量的定义语句向前跳转: // ...
goto end;
int ix = 10; // error: goto bypasses declaration statement
end:
// error: code here could use ix but the goto bypassed its declaration
ix = 42;
如果确实需要在 goto 和其跳转对应的标号之间定义变量,则定义必须放在一个块语句中:
// ...
goto end; // ok: jumps to a point where ix is not defined
{
int ix = 10;
// ... code using ix
}
end: // ix no longer visible here
向后跳过已经执行的变量定义语句则是合法的。为什么?向前跳过未执行的变量定义语句,意味着变量可能在没有定义的情况下使用。向后跳回到一个变量定义之前,则会使系统撤销这个变量,然后再重新创建它:
// backward jump over declaration statement ok
begin:
int sz = get_size();
if (sz <= 0) {
goto begin;
}
注意:执行 goto 语句时,首先撤销变量 sz,然后程序的控制流程跳转到带 begin: 标号的语句继续执行,再次重新创建和初始化 sz 变量。