2020-10-09

C#中的几种跳转语句详解
使用跳转语句执行分支,该语句导致立即传递程序控制。跳转语句中使用下列关键字:
break
continue
goto
return
//--------------------------------------------------------------------------------------------------------------------
break 语句终止它所在的最近的封闭循环或 switch 语句。控制传递给终止语句后面的语句(如果有的话)。此语句的形式如下:
break;
示例
在此例中,条件语句包含一个可以从 1 计数到 100 的计数器;但 break 语句在计数达到 4 后终止循环。
// statements_break.cs
using System;
class Breakes
{
public static void Main()
{
for (int i = 1; i <= 100; i++)
{
if (i == 5)
break;
Console.WriteLine(i);
}
}
}
输出
1
2
3
4
示例
此例在 switch 语句中演示了 break 的用法。
// statements_break2.cs
// break and switch
using System;
class Switch
{
public static void Main()
{
Console.Write("Enter your selection (1, 2, or 3): ");
string s = Console.ReadLine();
int n = Int32.Parse(s);
switch(n)
{
case 1:
Console.WriteLine(“Current value is {0}”, 1);
break;
case 2:
Console.WriteLine(“Current value is {0}”, 2);
break;
case 3:
Console.WriteLine(“Current value is {0}”, 3);
break;
default:
Console.WriteLine(“Sorry, invalid selection.”);
break;
}
}
}
输入
1
示例输出
Enter your selection (1, 2, or 3): 1
Current value is 1
如果输入了 4,则输出为:
Enter your selection (1, 2, or 3): 4
Sorry, invalid selection.

//--------------------------------------------------------------------------------------------------------------------
continue 将控制传递给它所在的封闭迭代语句的下一个迭代。它的形式为:
continue;
示例
在此例中,计数器初始化为从 1 到 10 计数。通过与表达式一起使用 continue 语句 (i < 9),跳过了位于 continue 与 for 体结尾之间的语句。
// statements_continue.cs
using System;
class ContinueTest
{
public static void Main()
{
for (int i = 1; i <= 10; i++)
{
if (i < 9)
continue;
Console.WriteLine(i);
}
}
}
输出
9
10
//---------------------------------------------------------------------------------------------------------------
goto 语句将程序控制直接传递给标记语句。它的形式为下列之一:
goto identifier;
goto case constant-expression;
goto default;
其中:
identifier
一个标签。
constant-expression
一个 switch-case 标签。
备注
在第一种形式中,identifier 指示位于当前体中的标签、相同的词法范围或 goto 语句的封闭范围。
goto 的一个通常用法是将控制传递给特定的 switch-case 标签或 switch 语句中的默认标签。
goto 语句还用于跳出深嵌套循环。
如果程序中从未引用过标签,则可能发出一条警告消息。有关标签的更多信息,请参见 3.3 声明。
示例
有关使用 goto 将控制传递给特定 switch-case 标签的示例,请参见 switch 示例。
示例
下例演示了使用 goto 跳出嵌套循环。
// statements_goto.cs
// Nested search loops
using System;
public class GotoTest1
{
public static void Main()
{
int x = 200, y = 4;
int count = 0;
string[,] myArray = new string[x,y];
// Initialize the array:
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
myArray[i,j] = (++count).ToString();
// Read input:
Console.Write("Enter the number to search for: ");
// Input a string:
string myNumber = Console.ReadLine();
// Search:
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
if (myArray[i,j].Equals(myNumber))
goto Found;
Console.WriteLine(“The number {0} was not found.”, myNumber);
goto Finish;
Found:
Console.WriteLine(“The number {0} is found.”, myNumber);
Finish:
Console.WriteLine(“End of search.”);
}
}
输入
44
示例输出
Enter the number to search for: 44
The number 44 is found.
End of search.
示例
// statements_goto2.cs
// CS0159 expected
// Labels outside the scope
using System;
class UnreachableCode
{
public static void Main()
{
int x = 55;
Console.WriteLine(“x = {0}”, x);
if (x == 55)
{
x = 135;
goto A; // Error
}
x = x + 1;
for (int i=1; i<=5; i++)
{
A: Console.WriteLine(i);
}
Console.WriteLine(“x = {0}”, x);
}
}
在前面的示例中,goto 语句引用其范围之外的标签 A。编译器将发出错误信息:
No such label ‘A’ within the scope of the goto statement
由于从未引用过此标签,因此还可能发出一条警告消息。
如果将标签 A 移到 for 循环的开始处,程序将正常编译和运行,即:
A: for (int i=1; i<=5; i++) { // Now the program compiles.
//--------------------------------------------------------------------------------------------------------------------
return 语句终止它出现在其中的方法的执行并将控制返回给调用方法。它还可以返回可选 expression 的值。如果方法为 void 类型,则可以省略 return 语句。此语句的形式如下:
return [expression];
其中:
expression
由方法返回的值。expression 不与 void 类型的方法一起使用。
示例
在下例中,A() 方法以 double 值的形式返回 Area 变量。
// statements_return.cs
using System;
class ReturnTest
{
static double CalculateArea(int r)
{
double area;
area = rrMath.PI;
return area;
}
public static void Main()
{
int radius = 5;
Console.WriteLine(“The area is {0:0.00}”, CalculateArea(radius));
}
}
输出
The area is 78.54

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值