🧩 一、控制流概述
C# 中的控制流語句用于根據條件或循環執行代碼塊。它們是程序邏輯的核心部分。
? 二、1. if
、else if
、else
int score = 85;if (score >= 90)
{Console.WriteLine("優秀");
}
else if (score >= 60)
{Console.WriteLine("及格");
}
else
{Console.WriteLine("不及格");
}
? 三、2. switch
語句
示例:選擇星期幾
Console.Write("請輸入星期幾(1-7):");
int day = int.Parse(Console.ReadLine());switch (day)
{case 1:Console.WriteLine("星期一");break;case 2:Console.WriteLine("星期二");break;case 3:case 4:Console.WriteLine("工作日");break;case 5:Console.WriteLine("星期五");break;case 6:case 7:Console.WriteLine("周末");break;default:Console.WriteLine("輸入錯誤");break;
}
使用 when
條件匹配(C# 7+)
object value = 100;switch (value)
{case int i when i > 0:Console.WriteLine("正整數");break;case int i when i < 0:Console.WriteLine("負整數");break;default:Console.WriteLine("其他類型或零");break;
}
使用枚舉作為匹配表達式
enum Color
{Red,Green,Blue
}Color selected = Color.Green;switch (selected)
{case Color.Red:Console.WriteLine("紅色");break;case Color.Green:Console.WriteLine("綠色");break;default:Console.WriteLine("未知顏色");break;
}
switch
表達式(C# 8+)
string result = selected switch
{Color.Red => "紅色",Color.Green => "綠色",_ => "未知顏色"
};Console.WriteLine(result);
? 四、3. while
和 do...while
while
簡單循環
int count = 1;
while (count <= 5)
{Console.WriteLine($"計數:{count}");count++;
}
do...while
至少執行一次
int num;
do
{Console.Write("請輸入一個數字(輸入0退出):");num = int.Parse(Console.ReadLine());
} while (num != 0);
? 五、4. for
循環
簡單循環輸出 1~10
for (int i = 1; i <= 10; i++)
{Console.WriteLine(i);
}
for
遍歷數組
int[] numbers = { 10, 20, 30, 40 };for (int i = 0; i < numbers.Length; i++)
{Console.WriteLine($"索引 {i} 的值為:{numbers[i]}");
}
嵌套 for
循環(打印乘法表)
for (int i = 1; i <= 9; i++)
{for (int j = 1; j <= i; j++){Console.Write($"{j}*{i}={i * j}\t");}Console.WriteLine();
}
? 六、5. foreach
遍歷集合
string[] names = { "Alice", "Bob", "Charlie" };foreach (string name in names)
{Console.WriteLine(name);
}
? 七、6. continue
跳過當前迭代
for (int i = 1; i <= 10; i++)
{if (i % 2 == 0)continue;Console.WriteLine(i); // 只輸出奇數
}
🧠 八、總結對比表
控制流結構 | 是否支持條件判斷 | 是否支持多分支 | 是否可重復執行 |
---|---|---|---|
if / else | ? | ? | ? |
switch | ? | ? | ? |
while | ? | ? | ? |
do...while | ? | ? | ? |
for | ? | ? | ? |
foreach | ? | ? | ? |
📦 九、完整練習項目模板(Program.cs)
using System;class Program
{enum WeekDay{Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday}static void Main(){Console.WriteLine("=== C# 控制流綜合練習 ===\n");// if / else if / elseint score = 78;if (score >= 90) Console.WriteLine("優秀");else if (score >= 60) Console.WriteLine("及格");else Console.WriteLine("不及格");// switchWeekDay today = WeekDay.Monday;string dayName = today switch{WeekDay.Saturday or WeekDay.Sunday => "周末",_ => "工作日"};Console.WriteLine($"今天是:{dayName}");// whileint count = 1;while (count <= 5){Console.WriteLine($"計數:{count}");count++;}// do whileint num;do{Console.Write("請輸入一個數字(輸入0退出):");num = int.Parse(Console.ReadLine());} while (num != 0);// for 循環數組int[] nums = { 10, 20, 30 };for (int i = 0; i < nums.Length; i++){Console.WriteLine($"索引 {i} 的值為:{nums[i]}");}// foreachstring[] names = { "Tom", "Jerry" };foreach (string name in names){Console.WriteLine(name);}// continuefor (int i = 1; i <= 10; i++){if (i % 2 == 0) continue;Console.WriteLine(i);}Console.WriteLine("\n按任意鍵退出...");Console.ReadKey();}
}
🎯 十、運行效果(模擬)
=== C# 控制流綜合練習 ===及格
今天是:工作日
計數:1
計數:2
...
請輸入一個數字(輸入0退出):5
請輸入一個數字(輸入0退出):0
索引 0 的值為:10
索引 1 的值為:20
索引 2 的值為:30
Tom
Jerry
1
3
5
7
9按任意鍵退出...