c# 流程控制

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

namespace CSTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("----------1.GOTO----------");
            TestGoTo();
            Console.WriteLine("----------2.三元表达式----------");
            TestThreeExp();
            Console.WriteLine("----------3.IF----------");
            TestIF();
            Console.WriteLine("----------4.SWITCH/CASE----------");
            TestSwitch();
            Console.WriteLine("----------5.CONTINUE----------");
            TestContiune();
            Console.WriteLine("----------6.WHILE----------");
            TestWhile();
            Console.WriteLine("----------7.DOWHILE----------");
            TestDoWhile();
            Console.WriteLine("----------8.FOR----------");
            TestFor();
            Console.WriteLine("----------9.FOREACH----------");
            TestForeach();
            Console.ReadKey();      
        }
        //GOTO语句
        public static void TestGoTo()
        {
            int num = 5;
            goto OutPut;
            num += 10;  //无法访问的代码          
        OutPut:
            Console.WriteLine("Num = " + num);
        }

        //三元表达式
        public static void TestThreeExp()
        {
            int num = 5;
            string Msg = num == 0 ? "num = 0" : "num !=0";
            Console.WriteLine(Msg);
        }

        //IF 语句
        public static void TestIF()
        {
            if (true)
            {
                Console.WriteLine("IF 判断结果为True");
            }
            else
            {
                Console.WriteLine("IF 判断结果非True");
            }
            if (false)
            {
                Console.WriteLine("IF 判断结果为False");
            }
        }

        //Switch case
        public static void TestSwitch()
        {
            string StrSwitch = "C";
            switch (StrSwitch)
            {
                case "A":
                    Console.WriteLine("Switch A");
                    break;
                case "B":
                    Console.WriteLine("Switch B");
                    break;
                case "C":
                    Console.WriteLine("Switch C");
                    break;
                case "D":
                    Console.WriteLine("Switch D");
                    break;
                default:
                    Console.WriteLine("Switch Default");
                    break;
            }          
        }

        //Contiune
        public static void TestContiune()
        {
            for (int i = 0; i < 10;i++ )
            {
                if (i < 5) continue;
                Console.WriteLine("Continue" + i);
            }
        }

        //While
        public static void TestWhile()
        {
            int i = 5;
            int count = 5;
            while (i < 10)
            {              
                count++;
                Console.WriteLine("While 执行次数" + count + "; i = " + i);
                i++;
            }
        }

        //DoWhile
        public static void TestDoWhile()
        {
            int i = 5;
            int count = 0;
            do
            {
                count++;
                Console.WriteLine("While 执行次数" + count + "; i = " + i);
                i++;
            } while (i < 10);
        }

        //FOR
        public static void TestFor()
        {
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("i = " + i);
                i++;
            }
        }

        public static void TestForeach()
        {
            int[] Arr_i = new int[] { 1, 2, 3, 5 };
            foreach (int i in Arr_i)
            {
                Console.WriteLine("i = " + i);
            }
        }
    }
}

 

posted @ 2016-09-07 15:29  sky&&dan  阅读(431)  评论(0编辑  收藏  举报