资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

十八、流程控制之循环中断

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _18.流程控制之循环中断
{
    class Program
    {
        static void Main(string[] args)
        {
            /**
             * 循环的中断方式有四种:
             * (1) break语句立即终止当前所在的循环。
             * (2) continue语句立即终止本次循环,继续执行下一次循环。
             * (3) goto语句可以跳出循环,到已标记好的位置上。
             * (4) return语句跳出循环及其包含的函数。
             * 
             */
             
            // 使用break语句中断循环
            {
                int i = 1;
                
                while (i <= 10)
                {
                    if (i == 6)
                        break;
                    Console.WriteLine("{0}", i++);
                }
            }
            
            // 使用continue语句中断循环
            {
                int i;
                
                for (i = 1; i <= 10; i++)
                {
                    if ((i % 2) == 0)
                        continue;
                    Console.WriteLine(i);
                }
            }
            
            // 使用goto语句中断循环
            // 当使用goto语句跳出循环是合法的,但使用goto语句从外部进入循环是非法的。
            {
                int i = 1;
                
                while (i < 10)
                {
                    if (i == 6)
                        goto exitPoint;
                    Console.WriteLine("{0}", i++);
                }
                
                Console.WriteLine("This code will never be reached.");
                
            exitPoint:
                Console.WriteLine("This code is run when the loop is exited using goto.");
            }
            
            // 使用return语句中断循环
            {
                int i = 0;
                
                do
                {
                    if (i == 6)
                        return;
                    Console.WriteLine("{0}", i++);
                } while (i < 10);
            }
            
            Console.ReadKey();
        }
    }
}

新闻标题:十八、流程控制之循环中断
URL链接:http://cdkjz.cn/article/ihsoso.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220