首页 > 代码库 > 十四、流程控制之switch语句

十四、流程控制之switch语句

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

namespace _14.流程控制之switch语句
{
    class Program
    {
        static void Main(string[] args)
        {
            // switch语句
            
            /**
             * switch语句可以一次将测试变量与多个值进行比较,而不是仅测试一个条件。
             * switch语句语法:
             *  switch (<testVar>)
             *  {
             *      case <comparisonVar1>:
             *          <code to execute if <testVar> == <comparisonVar1>>
             *          break;
             *      case <comparisonVar2>:
             *          <code to execute if <testVar> == <comparisonVar2>>
             *          break;
             *      ...
             *      case <comparisonVarN>:
             *          <code to execute if <testVar> == <comparisonVarN>>
             *          break;
             *      default:
             *          <code to execute if <testVar> != <comparisonVars>>
             *          break;
             *  }
             *  
             * 解释说明:
             *  1. <testVar>中的值与每个<comparisonVarX>值(在case语句中指定),如果有一个匹配,
             *     就执行为该匹配提供的语句。如果没有匹配,就执行default部分中的代码。
             *  2. 执行完每个部分中的代码后,还需要另一个语句break。
             *  3. 在执行完一个case块后,再执行第二个case语句是非法的。
             *  4. 每个<comarisonVarX>都必须是一个常数值(字面值或常量)。
             *  5. switch语句对case语句的数量没有限制。
             *  
             * 可以使用三种方式防止程序流程从一个case语句转到下一个case语句:
             * 1. break语句将中断switch语句的执行,而执行该结构后面的语句。
             * 2. return语句将中断当前函数的运行,而不是仅中断switch结构的执行。
             * 3. 可以使用goto语句跳转到其他case语句或跳出switch结构。
             */
             
            // 版本1
            {
                int WeekOfDay;
                
                Console.Write("(Ver1)Enter a week of day: ");
                WeekOfDay = Convert.ToInt32(Console.ReadLine());
                
                switch (WeekOfDay)
                { 
                    case 1:
                        Console.WriteLine("Today is Monday.");
                        break;
                    case 2:
                        Console.WriteLine("Today is Tuesday.");
                        break;
                    case 3:
                        Console.WriteLine("Today is Wednesday.");
                        break;
                    case 4:
                        Console.WriteLine("Today is Thursday.");
                        break;
                    case 5:
                        Console.WriteLine("Today is Friday.");
                        break;
                    case 6:
                    case 7:
                        Console.WriteLine("Today is Weekend.");
                        break;
                    default:
                        return;
                }
            }
            
            // 版本2
            {
                int WeekOfDay;
                
                Console.Write("(Ver2)Enter a week of day: ");
                WeekOfDay = Convert.ToInt32(Console.ReadLine());
                
                switch (WeekOfDay)
                {
                    case 1:
                        Console.WriteLine("Today is Monday.");
                        break;
                    case 2:
                        Console.WriteLine("Today is Tuesday.");
                        break;
                    case 3:
                        Console.WriteLine("Today is Wednesday.");
                        break;
                    case 4:
                        Console.WriteLine("Today is Thursday.");
                        break;
                    case 5:
                        Console.WriteLine("Today is Friday.");
                        break;
                    case 6:
                        goto case 7;
                    case 7:
                        Console.WriteLine("Today is Weekend.");
                        return;
                    default:
                        goto End;
                }
                
            End:
                Console.WriteLine("The week of day is unknown.");
            }
            
            Console.ReadKey();
        }
    }
}


本文出自 “MK IT Life” 博客,请务必保留此出处http://vikxiao.blog.51cto.com/9189404/1586489

十四、流程控制之switch语句