这里没有关于C语言是否应该使用goto语句的争论。只谈goto语句的用法。
不建议使用Goto语句,但是我们需要知道当我们遇到goto语句时它意味着什么。
Goto语句也称为无条件转移语句。
让我们看一个例子:
void main(){ 0
int a=2,b=3;
if(ab)
goto aa
printf(' hello ');
aa : printf(' s ');
返回0;
}
将程序的执行结果改为s。
全部直接跳转到aa:printf('s ')后转到aa;
Aa:是标记线。记住冒号不能省略。
相反,如果代码看起来像这样
void main(){ 0
int a=2,b=3;
if(ab)
goto aa
printf(' hello ');
aa : printf(' s ');
返回0;
}
那么执行结果就是hellos。
您可以看到printf('hello ')已执行;aa : printf(' s ');
Aa:将毫无意义。