All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
动态网页常用技术:用PHP追随变量(cookie和session)_php资料_编程技术
SqlServer教程:经典SQL语句集锦_[SQL Server教程]
ASP.NET超时设置_[Asp.Net教程]
.NET2.0在Repeater中实现删除_[Asp.Net教程]
Asp.net2.0实现多语言网页的方法_[Asp.Net教程]
javascript实现广告页面的定时关闭_JavaScript技术_编程技术
asp.net2.0,treeview控件用数据表做为数据源实现n级动态菜单_[Asp.Net教程]
ASP.NET开发经验(1):解决ASP.NET与CSS中定义的中文字体名的冲突_[Asp.Net教程]
ASP.NET Remoting体系结构(三)
著名的SQL流量注入(SQL注入)攻击法_[SQL,Server教程]
Reading number is top 10 pictures
天安门景物系列(一)
南昌铁路局宜春车务段攸县车站铁路职工福利房被开发商侵占
Go to the national museum2
Chinese paper-cut grilles art appreciation4
Ashlynn Brooke a group sexy photo2
The Soviet union swimsuit exposure in the 70 year2
赵惟依写真2
So beauty, will let you spray blood1
中国处女图鉴1
Terrorist smile the largest human history an explosion1
Download software ranking
The Bermuda triangle3
apache-tomcat-6.0.33
Boxer's Top ten classic battle5
Popkart Cracked versions Mobile phone games
C语言教程TXT
Call Of Duty2
Tram sex maniac 2 (H) rar bag1
Tram sex maniac 2 (H) rar bag9
Boxer vs Yellow4
Tram sex maniac 2 (H) rar bag15
qq published in(发表于) 2014/7/11 9:15:53 Edit(编辑)
C#中break语句的使用方法

C#中break语句的使用方法

C#中break语句的使用方法

C#中break语句的使用方法

break语句可以用来终止一条迭代语句,使控制流程跳转到迭代语句的下一条语句。break语句可以放在循环体的任意位置来实现这一目的,若要中止整个嵌套循环,可以使用return语句。

示例

break在if语句中的使用

在条件语句中包含一个1~10的计数器,但break语句在计数达到4后终止循环。程序的流程如图1所示。




图1 break语句流程

程序代码如下:

using System;

class BreakTest

{

static void Main()

{

Console.WriteLine("输出结果为:");

for (int i = 1; i <= 10; i++)

{

if (i > 4)

{

break;

}

Console.WriteLine(i);

}

Console.Read();

}

}

键运行程序,运行结果如图2所示。



图3 break语句

示例

break语句在switch语句中的使用

通过判断用户输入的值显示相应信息,程序代码如下:

using System;

class Switch

// http://www.isstudy.com

{

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;

}

}

}


键运行程序,运行结果如图4所示。



图4 break语句与switch语句连用

完整程序代码如下:

★★★★★主程序文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.Text;

// http://www.isstudy.com

namespace _3_10

{

class Program

{

static void Main()

{

Console.WriteLine("输出结果为:");

for (int i = 1; i <= 10; i++)

{

if (i > 4)

{

break;

}

Console.WriteLine(i);

}

Console.Read();

}

}



添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.