原文: http://blog.csdn.net/shimazhuge/article/details/8448773
---------------------------------------------------------
小dome
- #include <windows.h>
- #include <stdio.h>
- int main()
- {
- int n=7;
- number2:
- printf("hello world\n");
- if (n==7)
- {
- n=8;
- printf("n=7 start\n");
- goto number0;
- printf("n=7 end\n");
- }
- else
- {
- printf("n=8 start\n");
- goto number1;
- printf("n=8 end\n");
- }
- number0:
- printf("hi number0\n");
- goto number2;
- number1:
- printf("hi number1\n");
- number3:
- printf("number3\n");
- system("pause");
- return 0;
- }
輸出結果
結論分析及優缺點
goto 語句可用於跳出深嵌套循環
goto語句可以往后跳,也可以往前跳,且一直往前執行
goto只能在函數體內跳轉,不能跳到函數體外的函數。即goto有局部作用域,需要在同一個棧內。
goto 語句標號由一個有效地標識符和符號";"組成,其中,標識符的命名規則與變量名稱相同,即由字母、數字和下划線組成,且第一個字符必須是字母或下划線。執行goto語句后,程序就會跳轉到語句標號處,並執行其后的語句。
通常goto語句與if條件語句連用,但是,goto語句在給程序帶來靈活性的同時,也會使得使程序結構層次不清,而且不易讀,所以要合理運用該語句。