C# goto 语句
来源:互联网 发布:如何破解快刀软件 编辑:程序博客网 时间:2024/06/05 10:52
参考:https://msdn.microsoft.com/zh-cn/library/13940fs2.aspx
goto 语句将程序控制直接传递给标记语句。
goto 的一个通常用法是将控制传递给特定的 switch-case 标签或 switch 语句中的默认标签。
goto 语句还用于跳出深嵌套循环。
下面的示例演示了 goto 在 switch 语句中的使用。
class SwitchTest { static void Main() { Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large"); Console.Write("Please enter your selection: "); string s = Console.ReadLine(); int n = int.Parse(s); int cost = 0; switch (n) { case 1: cost += 25; break; case 2: cost += 25; goto case 1; case 3: cost += 50; goto case 1; default: Console.WriteLine("Invalid selection."); break; } if (cost != 0) { Console.WriteLine("Please insert {0} cents.", cost); } Console.WriteLine("Thank you for your business."); // Keep the console open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } /* Sample Input: 2 Sample Output: Coffee sizes: 1=Small 2=Medium 3=Large Please enter your selection: 2 Please insert 50 cents. Thank you for your business. */
下面的示例演示了使用 goto 跳出嵌套循环。
public class GotoTest1{ static void Main() { int x = 200, y = 4; int count = 0; string[,] array = new string[x, y]; // Initialize the array: for (int i = 0; i < x; i++) for (int j = 0; j < y; j++) array[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 (array[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."); // Keep the console open in debug mode. Console.WriteLine("Press any key to exit."); Console.ReadKey(); }}/*Sample Input: 44Sample OutputEnter the number to search for: 44The number 44 is found.End of search.*/
0 0
- C#使用goto语句
- C# goto 语句
- C#中的goto跳转语句
- C# 中的 break ,goto ,contitue ,return 语句
- goto语句
- goto语句
- goto语句
- goto语句
- goto语句
- goto语句
- goto语句
- goto语句
- goto语句
- goto语句
- goto语句
- goto语句
- goto语句
- goto语句
- Longest Common Prefix
- ios 开发关键字IBInspectable
- shell中的${},##和%%的使用
- java中null和""的区别
- linux下tar.gz、tar、bz2、zip等解压缩、压缩命令小结
- C# goto 语句
- 指针的问题5 指针访问数组的方式
- 开发团队周总结流水20150717
- 二维数组
- 关于建立Android工程R文件丢失的问题
- 人丑就要多读书(12)
- BCB使用线程删除文件夹中的图片
- fflush(stdin)小知识
- Linux shell 编程简单使用