正常计算机代码的执行顺序是从上至下,除非有逻辑语句跳转。
以前没了解goto的使用,现在研究一下goto还是挺有意思的。
下面是c++简单的代码demo。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26void Main::xecutionTest() { for (int i = 0; i<10; i++) { switch (getCode()) { case 0: cout << "Ignore this item"; continue; case 1: mCase: cout << "End the whole cycle"; break; case 2: cout << "Jump to the specified position"; goto mCase; case 3: cout << "End this function"; return; default: cout <<"Invalid selection"; break; } } cout << "Normal execution"; }
下面是c#简单的代码(c++也适用c#)demo。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25void xecutionTest() { for (int i = 0; i < 10; i++) { switch (getCode()) { case 0: Console.WriteLine("Ignore this item"); continue; case 1: Console.WriteLine("End the whole cycle"); break; case 2: Console.WriteLine("Jump to the specified position"); goto case 1; case 3: Console.WriteLine("End this function"); return; default: Console.WriteLine("Invalid selection"); break; } } Console.WriteLine("Normal execution"); }
下面是java简单的代码demo 。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25public static void main(String[] args) { for (int i = 0; i<10; i++) { switch (getCode()) { case 0: System.out.println("Ignore this item"); continue; case 1: mCase: System.out.println("End the whole cycle"); break; case 2: System.out.println("Jump to the specified position"); break mCase; case 3: System.out.println("End this function"); return; default: System.out.println("Invalid selection"); break; } } System.out.println("Normal execution"); }
下面是php简单的代码demo 。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27function xecutionTest(){ for (int i = 0; i<10; i++) { switch (getCode()) { case 0: echo "Ignore this item"; continue; case 1: mCase: echo "End the whole cycle"; break; case 2: echo "Jump to the specified position"; goto mCase; case 3: echo "End this function"; return; default: echo "Invalid selection"; break; } } echo "Normal execution"; } }
最后
以上就是傲娇镜子最近收集整理的关于代码执行顺序 continue、break、goto、return。的全部内容,更多相关代码执行顺序内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复