C语言中使用goto语句
关于C语言是否该使用goto语句这里不再辩论。只讲讲goto语句的用法。
不建议使用goto语句,但是遇到goto语句我们要知道是什么 意思。
goto语句又叫无条件转移语句。
先看一个例子:
void main(){
int a=2,b=3;
if(a<b)
goto aa;
printf("hello");
aa:printf("s");
return 0;
}
改程序的执行结果为s
所有在goto aa这句之后直接跳转到aa:printf("s");
aa:为标记行。冒号切记不可省略。
反之如果代码这样子
void main(){
int a=2,b=3;
if(a>b)
goto aa;
printf("hello");
aa:printf("s");
return 0;
}
那么执行结果就是hellos
可以看到执行了 printf("hello"); aa:printf("s");
aa:将没有意义。
完!!