[C++基础]goto的用法

原文: http://blog.csdn.net/shimazhuge/article/details/8448773

---------------------------------------------------------

小dome

[cpp] view plain copy

 
  1. #include <windows.h>    
  2. #include <stdio.h>  
  3. int main()  
  4. {  
  5.     int n=7;    
  6. number2:  
  7.     printf("hello world\n");   
  8.     if (n==7)  
  9.     {   
  10.         n=8;  
  11.         printf("n=7 start\n");  
  12.         goto number0;  
  13.         printf("n=7 end\n");  
  14.     }  
  15.     else  
  16.     {  
  17.         printf("n=8 start\n");  
  18.         goto number1;  
  19.         printf("n=8 end\n");  
  20.     }    
  21.   
  22. number0:   
  23.     printf("hi number0\n");  
  24.     goto number2;  
  25. number1:  
  26.     printf("hi number1\n");  
  27. number3:  
  28.     printf("number3\n");   
  29.     system("pause");  
  30.     return 0;  
  31. }   

输出结果

[C++基础]goto的用法

结论分析及优缺点

goto 语句可用于跳出深嵌套循环
goto语句可以往后跳,也可以往前跳,且一直往前执行

goto只能在函数体内跳转,不能跳到函数体外的函数。即goto有局部作用域,需要在同一个栈内。 

goto 语句标号由一个有效地标识符和符号";"组成,其中,标识符的命名规则与变量名称相同,即由字母、数字和下划线组成,且第一个字符必须是字母或下划线。执行goto语句后,程序就会跳转到语句标号处,并执行其后的语句。
通常goto语句与if条件语句连用,但是,goto语句在给程序带来灵活性的同时,也会使得使程序结构层次不清,而且不易读,所以要合理运用该语句。

原文链接: https://www.cnblogs.com/oxspirt/p/7485873.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    [C++基础]goto的用法

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/259243

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月14日 下午12:36
下一篇 2023年2月14日 下午12:38

相关推荐