2014-04-13第15周周日:goto语句的标志只包含一句话,而且程序总会执行goto的标志。

来源:互联网 发布:淘宝客服常见问题汇总 编辑:程序博客网 时间:2024/06/11 16:01
对于下面这段代码,
int main(){int i, j;for ( i = 0; i < 10; i++ ){printf( "Outer loop executing. i = %d\n", i );for ( j = 0; j < 3; j++ ){printf( " Inner loop executing. j = %d\n", j );if ( i == 10 )goto stop;printf( " i = %d , j = %d \n",i, j );}}/* This message does not print: */printf( "Loop exited. i = %d\n", i );stop: printf( "Jumped to stop. i = %d\n", i );//stop: 就是标签  return 0;}

最后两行显示的是:

Loop exited. i =10Jumped to stop.i=10
可见,程序未执行goto stop;这条语句,但还是经过了stop的标志。


以下代码是执行了goto stop;这条语句并跳到stop标志处的情况(只是将以上程序的if(i==5)改成了if(i==10)):

#include <stdio.h>int main(){int i, j;for ( i = 0; i < 10; i++ ){printf( "Outer loop executing. i = %d\n", i );for ( j = 0; j < 3; j++ ){printf( " Inner loop executing. j = %d\n", j );if ( i == 5 )goto stop;printf( " i = %d , j = %d \n",i, j );}}/* This message does not print: */printf( "Loop exited. i = %d\n", i );stop: printf( "Jumped to stop. i = %d\n", i );//stop: 就是标签  return 0;}

最后三行显示的是:

Outer loop executing. i=5Inner loop executing. j=0Jumped to stop. i=5

1 0