欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

C++(Goto使用实例)

发布时间:2025/4/16 44 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C++(Goto使用实例) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

对于很多技术,看源码很重要,但还是看实例代码进步最快~

代码片段一:

#include <iostream> using namespace std; int main(){int val = 0;for (int i = 0; i < 100; ++i){for (int j = 0; j < 100; ++j) {val = i * 100 + j;if (val > 520) goto BUTTON;}}cout << "OutPut 1\n";BUTTON: // 这里关键cout << "Hello, Here is the "<< val<< "th.\n";cout << "OutPut 2\n"; }

输出的结果:

Hello, Here is the 521th.
OutPut 2

看到这个我想到了,汇编代码中的实例 哈哈哈

实例代码二

#include <iostream> using namespace std; int main(){int val = 0, i;for (i = 0; i < 100; ++i){for (int j = 0; j < 100; ++j) {if (val > 520) goto BUTTON;val = i * 100 + j;}BUTTON:if (i > 20) goto BUTTON2;}cout << "OutPut 1\n";BUTTON2:cout << "Hello, Here is the "<< val<< "th.\n";cout << "OutPut 2\n";cout << "i = "<< i<< endl; }

Hello, Here is the 521th.
OutPut 2
i = 21

总结

以上是生活随笔为你收集整理的C++(Goto使用实例)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。