目录

goto statement

C编程中的goto语句提供了从'goto'到同一函数中带标签语句的无条件跳转。

NOTE - 在任何编程语言中都不鼓励使用goto语句,因为它很难跟踪程序的控制流程,使程序难以理解且难以修改。 任何使用goto的程序都可以重写以避免它们。

语法 (Syntax)

C语言中goto语句的语法如下 -

goto label;
..
.
label: statement;

这里label可以是除C关键字之外的任何纯文本,它可以在C程序的上方或下方的任何地方设置为goto语句。

流程图 (Flow Diagram)

C转到声明

例子 (Example)

#include <stdio.h>
int main () {
   /* local variable definition */
   int a = 10;
   /* do loop execution */
   LOOP:do {
      if( a == 15) {
         /* skip the iteration */
         a = a + 1;
         goto LOOP;
      }
      printf("value of a: %d\n", a);
      a++;
   }while( a < 20 );
   return 0;
}

编译并执行上述代码时,会产生以下结果 -

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
↑回到顶部↑
WIKI教程 @2018