4-2 switch

switch陳述式透過檢查單一變數的數值,來決定程式執行路線。

int a = new Random().Next(1, 4);
switch ( a ) {
case 1:
Console.WriteLine(“Come to first block”);
break;
case 2:
case 3:
Console.WriteLine(“Come to second block”);
break;
default:
Console.WriteLine(“Come to default block”);
break;
}