简单测试下 goto 语句

大家都建议不要用goto语句。我测试下goto语句怎么用。

goto语句执行是,跳转到标签位置后,又开始从标签位置往下执行。

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
class Program
{
static void Main(string[] args)
{
int i = 0;

aMark:
int a = 10;
Console.WriteLine(a);
int b = 20;
Console.WriteLine(b);
int c = a + b;
Console.WriteLine(c + "\n");

i++;
if (i > 2)
goto bMark;

goto aMark;

bMark:
Console.WriteLine("程序结束!!");

Console.ReadKey();
}

输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
10
20
30

10
20
30

10
20
30

程序结束!!

感谢支持!