惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
Jina AI
Jina AI
雷峰网
雷峰网
月光博客
月光博客
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
B
Blog RSS Feed
美团技术团队
C
CXSECURITY Database RSS Feed - CXSecurity.com
小众软件
小众软件
Security Latest
Security Latest
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
A
Arctic Wolf
Latest news
Latest news
Attack and Defense Labs
Attack and Defense Labs
I
Intezer
F
Fortinet All Blogs
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
S
Secure Thoughts
Help Net Security
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
V
Visual Studio Blog
P
Proofpoint News Feed
博客园 - 【当耐特】
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
Stack Overflow Blog
Stack Overflow Blog
Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 最新话题
H
Help Net Security
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog

博客园 - rudyshen

我的自信到哪里去啦 了解一下自己 最近失业啦 XtraGrid控件3——3种GridView的属性 C#命名规范,控件、数据类型、ADO.NET等 Winform中通过一个字符串定位到和字符串相等ID的控件(将字符串转换成相应的控件名称) - rudyshen - 博客园 用户控件笔记<原创> XML笔记<原创> 什么时候应当使用interface?什么时候应当使用abstract class? XML操作<转> 怎样从DevExpress 7.1.1升级到DevExpress 7.3.5<原创> 怎样从DevExpress 7.1.1升级到DevExpress 7.3.5<原创> (C#)利用反射动态调用类成员[转载] Net中的反射使用入门 程序集的完全限定名与动态加载程序 DevExpress 7.3.5 笔记 C#控件属性 DevExpress 组件ToolBar、PopupMenu 使用有感 转 实现QQ中消息窗体闪烁功能 转
C# 跳转语句(break,continue,goto,return,throw)
rudyshen · 2008-04-12 · via 博客园 - rudyshen

今天下午遇到到一個問題,在循語句中使用分支分句,我想在分支中跳出分支和循環。一時差點沒想來用跳轉語句。所以在網上找了點資料溫習一下。
這篇文章自認為不錯,收藏一下。

---------------------------------------------------------
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
    }
}

轉自:http://www.cnblogs.com/freeliver54/archive/2007/01/30/634620.html