c#
黑马程序员-凯发ag旗舰厅登录网址下载
------- windows phone 7手机开发、.net培训、期待与您交流! -------
c#中break,continue,return,,goto,throw的区别
break 语句用于终止最近的封闭循环或它所在的 switch 语句。
控制传递给终止语句后面的语句(如果有的话)。
continue 语句将控制权传递给它所在的封闭迭代语句的下一次迭代。
goto 语句将程序控制直接传递给标记语句。
goto 的一个通常用法是将控制传递给
特定的 switch-case 标签
或 switch 语句中的默认标签。
goto 语句还用于跳出深嵌套循环。
return 语句终止它出现在其中的方法的执行并将控制返回给调用方法。
它还可以返回一个可选值。
如果方法为 void 类型,则可以省略 return 语句。
throw 语句用于发出在程序执行期间出现反常情况(异常)的信号。
通常 throw 语句与 try-catch 或 try-finally 语句一起使用。
当引发异常时,程序查找处理此异常的 catch 语句。
也可以用 throw 语句重新引发已捕获的异常。
-------------------------------------------------------------
----------
break 示例
----------
在此例中,条件语句包含一个应该从 1 计数到 100 的计数器;
但 break 语句在计数达到 4 后终止循环。
// statements_break.cs
using system;
class breaktest
{
static void main()
{
for (int i = 1; i <= 100; i )
{
if (i == 5)
{
break;
}
console.writeline(i);
}
}
}
输出
1
2
3
4
--------------
continue 示例
--------------
在此示例中,计数器最初是从 1 到 10 进行计数,
但通过将 continue 语句与表达式 (i < 9) 一起使用,
跳过了 continue 与 for 循环体末尾之间的语句。
// statements_continue.cs
using system;
class continuetest
{
static void main()
{
for (int i = 1; i <= 10; i )
{
if (i < 9)
{
continue;
}
console.writeline(i);
}
}
}
输出
9
10
----------
goto 示例1
----------
下面的示例演示了 goto 在 switch 语句中的使用。
// statements_goto_switch.cs
using system;
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.");
}
}
输入
2
示例输出
coffee sizes: 1=small 2=medium 3=large
please enter your selection: 2
please insert 50 cents.
thank you for your business.
----------
goto 示例2
----------
下面的示例演示了使用 goto 跳出嵌套循环。
// statements_goto.cs
// nested search loops
using system;
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.");
}
}
输入
44
示例输出
enter the number to search for: 44
the number 44 is found.
end of search.
----------
throw 示例
----------
此例演示如何使用 throw 语句引发异常。
// throw example
using system;
public class throwtest
{
static void main()
{
string s = null;
if (s == null)
{
throw new argumentnullexception();
}
console.write("the string s is null"); // not executed
}
}
------- windows phone 7手机开发、.net培训、期待与您交流! -------
转载于:https://www.cnblogs.com/meloda/archive/2012/10/08/2715608.html
总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的黑马程序员--c#中break_continue_return__goto_throw的区别的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇:
- 下一篇: