Latest web development tutorials

C goto statement

C cycle C cycle

C languagegoto statement allows the control unconditionally to a labeled statement within the same function.

Note: In any programming language, does not recommend the use of the goto statement.Because it makes it difficult to track the control flow of the program, making the program difficult to understand and difficult to modify. Any program that uses goto statements can be rewritten as the wording does not require the use of the goto statement.

grammar

C language syntaxgoto statement:

goto label;
..
.
label: statement;

Here, label can be any other than the C keywords plain text, it can be set before or after the program in C gotostatement.

flow chart

C goto statement

Examples

#include <stdio.h>
 
int main ()
{
   /* 局部变量定义 */
   int a = 10;

   /* do 循环执行 */
   LOOP:do
   {
      if( a == 15)
      {
         /* 跳过迭代 */
         a = a + 1;
         goto LOOP;
      }
      printf("a 的值: %d\n", a);
      a++;
     
   }while( a < 20 );
 
   return 0;
}

When the above code is compiled and executed, it produces the following results:

a 的值: 10
a 的值: 11
a 的值: 12
a 的值: 13
a 的值: 14
a 的值: 16
a 的值: 17
a 的值: 18
a 的值: 19

C cycle C cycle