3. C# break,continue

2023-01-31 03:01:34 break continue 3.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class Program
{
        static void Main(string[] args)
{
            Console.WriteLine("Output do{.. break;} while() result");
            int i = 1;
            do 
{
                if ((i % 2) == 0)
                    break;
                Console.WriteLine("{0}",i++);
}
            while(i<=10);
            //break, 立即终止循环;执行后面语句
 
            Console.WriteLine("Output do{.. continue;} while() result");
            int j= 1;
            do
{
                if ((j++ % 2) == 0)                    
                Console.WriteLine("{0}", j);
                continue;
}
            while (j <= 10);
            //continue, 终止当前循环;执行下一个循环;
 
 
            Console.WriteLine("Output do{.. break;} while(true) result");
            int k = 1;
            do
{
                if ((k++ % 2) == 0)
                    Console.WriteLine("{0}", k);
                break;
}
            while (true);
            //死循环,用break跳出循环;
            Console.WriteLine("Output do{.. continue;} while(true) result");
            int  z= 1;
            do
{
                if ((z % 2) == 0)
                    continue;
                    Console.WriteLine("{0}", z++);
}
            while (true);
            //死循环,continue,关闭程序跳出循环;
            Console.ReadLine();
}
}
}


相关文章