跳转语句:break
break 语句用于终止最近的封闭循环或它所在的
示例
在此例中,条件语句包含一个应该从 1 计数到 100 的计数器;但 break 语句在计数达到 4 后终止循环。
C# | |
---|---|
class BreakTest { static void Main() { for (int i = 1; i <= 100; i++) { if (i == 5) { break; } Console.WriteLine(i); } } } /* Output: 1 2 3 4 */ |
本在线速查手册由www.w:3:x:u:e.com提供,请勿盗用!
下面的示例演示 break 在
C# | |
---|---|
class Switch { 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; } } } /* Sample Input: 1 Sample Output: Enter your selection (1, 2, or 3): 1 Current value is 1 */ |
本在线速查手册由www.w:3:x:u:e.com提供,请勿盗用!
如果输入了 4,则输出为:
Enter your selection (1, 2, or 3): 4 Sorry, invalid selection. |
本在线速查手册由www.w:3:x:u:e.com提供,请勿盗用!