Home [C# 筆記] switch 陳述句
Post
Cancel

[C# 筆記] switch 陳述句

「選擇結構(Selection Structure)」會根據程式的「判斷條件」是否成立來決定程式最後要往哪一流程(程序)去跑。

選擇結構包含:

  • if陳述句
  • switch陳述句
  • ?:運算子

switch 多向選擇結構

switch是「透過案例(case)的判斷來切換(switch)至符合案例(case)的區塊中」。

當判斷條件「超過三個以上」時,採用「switch 陳述句」會是較佳的選擇。

語法

  • 值常見的資料型態:char(字元)、string(字串)、int(數值)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch(運算式) 
{
    case [1]:
        //statement
        break;
    case [2]:
        //statement
        break;
    case [3]:
        //statement
        break;
    default:
        //statement
        break;
}

範例

透過switch實作「成績好壞」的功能:

  • 成績A:高手
  • 成績B:低空飛過
  • 成績C:明年再來
  • 以上皆非:非判級資料
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char score = char.Parse(Console.ReadLine()!);
switch (score)
{
    case 'A':
        Console.WriteLine("高手");
        break;
    case 'B':
        Console.WriteLine("低空飛過");
        break;
    case 'C':
        Console.WriteLine("明年再來");
        break;
    default:
        Console.WriteLine("非判級資料");
        break;
}

Book: Visual C# 2005 建構資訊系統實戰經典教本

This post is licensed under CC BY 4.0 by the author.