## 流程控制
by Q3goldlegeng
---
## if else
----
### 問題
先來個複習題目
題目:一瓶回復藥水要x元,小明身上有一點錢,請問小明買完剩多少錢
輸入:小明的錢n元,要買m罐 n m x
輸出:小明剩下的錢
----
```cpp
#include<iostream>
using namespace std;
int main(){
int n, m, x;
cin >> n >> m >> x;
int cost = m * x; // 總花費
int remaining = n - cost; // 剩餘金錢
cout << "小明剩下 " << remaining << " 元" << endl;
}
```
想想看如果錢是負數的呢?
----
### 解決方法
if 就像一個閘門,當符合條件才會開門
基本語法
```cpp
#include<iostream>
using namespace std;
int main(){
if( /*判斷條件*/ ){
//符合條件,執行大括號的程式碼
}
}
```
----
```cpp
#include<iostream>
using namespace std;
int main(){
if( /*條件1*/ ){
//符合條件,執行
}else{
//不符合條件1,執行
}
}
```
----
```cpp
#include<iostream>
using namespace std;
int main(){
if( /*條件1*/ ){
//符合條件1,執行
}else if( /*條件2*/ ){
//不符合條件1,但符合條件2,執行
}else{
//不符合條件1,也不符合條件2,執行
}
}
```
----
那判斷條件要塞甚麼?
---
## 比較運算子
----
還記得我說過程式世界裡'=',是賦值符號,而判斷用的等於要用'=='
等於 ==
不等於 !=
大於 >
大於等於 >=
小於 <
小於等於 <=
----
範例程式碼
```cpp
#include<iostream>
using namespace std;
int main(){
int a = 10, b = 20;
if(a == b){
cout << "a等於b" << endl;
}else{
cout << "a不等於b" << endl;
}
if(a > b){
cout << "a大於b" << endl;
}else{
cout << "a小於等於b" << endl;
}
}
```
----
回到藥水問題的解決方案
```cpp
#include <iostream>
using namespace std;
int main(){
int n = 0, m , x;
cin >> n >> m >> x;
int remaining = n - (m * x)
if(remaining >= 0){
cout << "小明剩下 " << remaining << " 元" << endl;
}else{
cout << "小明的錢不夠,還缺 " << -remaining << " 元" << endl;
}
}
```
---
## 邏輯運算子
----
有時候我們需要同時判斷多個條件,這時候就需要邏輯運算子
* 且 (AND): &&
* 或 (OR): ||
* 非 (NOT): !
----
範例
```cpp
#include<iostream>
using namespace std;
int main(){
int score = 85;
int age = 18;
// 且 (&&) - 兩個條件都要成立
if(score >= 80 && age >= 18){
cout << "可以申請獎學金" << endl;
}
// 或 (||) - 至少一個條件成立
if(score >= 90 || age <= 16){
cout << "符合特殊條件" << endl;
}
// 非 (!) - 條件相反
if(!(score < 60)){
cout << "沒有不及格" << endl;
}
}
```
---
## 巢狀 if
----
向~~巢狀一樣的if~~
if裡面再塞if

----
範例
```cpp
#include<iostream>
using namespace std;
int main(){
int score;
cin >> score;
if(score >= 0 && score <= 100){
if(score >= 90){
cout << "優秀" << endl;
}else if(score >= 80){
cout << "良好" << endl;
}else if(score >= 60){
cout << "及格" << endl;
}else{
cout << "不及格" << endl;
}
}else{
cout << "分數輸入錯誤" << endl;
}
}
```
----
不過我們一般盡量避免這樣
因為可讀性差 維護困難 除錯噩夢☠️☠️☠️
```
if(a > 0){
if(b > 0){
if(c > 0){
if(d > 0){
// 哪一層出問題了?
result = a * b * c * d;
cout << "找到我了嗎?" << endl;
}
}
}
}
```
----
改善!
#### 1早期返回 (Early Return)
```cpp
#include<iostream>
using namespace std;
int main(){
int score;
cin >> score;
// 先檢查錯誤情況,直接處理
if(score < 0 || score > 100){
cout << "分數輸入錯誤" << endl;
return 0;
}
// 正常情況的處理
if(score >= 90){
cout << "優秀" << endl;
}else if(score >= 80){
cout << "良好" << endl;
}else if(score >= 60){
cout << "及格" << endl;
}else{
cout << "不及格" << endl;
}
}
```
----
#### 2邏輯運算子:請善用我🤬
```cpp
if(age >= 18){
if(hasLicense){
if(hasInsurance){
cout << "可以開車" << endl;
}
}
}
//改善版本
if(age >= 18 && hasLicense && hasInsurance){
cout << "可以開車" << endl;
}else{
cout << "不能開車" << endl;
}
```
----
#### 3.為何不反向思考 (Guard Clauses)
```
if(money >= 100){
if(age >= 18){
if(hasID){
cout << "可以購買" << endl;
}else{
cout << "沒有身分證" << endl;
}
}else{
cout << "年齡不足" << endl;
}
}else{
cout << "金錢不足" << endl;
}
//改善
if(money < 100){
cout << "金錢不足" << endl;
return 0;
}
if(age < 18){
cout << "年齡不足" << endl;
return 0;
}
if(!hasID){
cout << "沒有身分證" << endl;
return 0;
}
cout << "可以購買" << endl;
```
---
練習題目
----
[zerojudge a001. 哈囉](https://zerojudge.tw/ShowProblem?problemid=a001)
----
題目1: 判斷奇偶數
輸入一個整數,判斷是奇數還是偶數
```cpp
#include<iostream>
using namespace std;
int main(){
int num;
cin >> num;
if(num % 2 == 0){
cout << "偶數" << endl;
}else{
cout << "奇數" << endl;
}
}
```
----
題目2: 三個數字比較大小
輸入三個數字,找出最大值
```cpp
#include<iostream>
using namespace std;
int main(){
int a, b, c;
cin >> a >> b >> c;
int max_num;
if(a >= b && a >= c){
max_num = a;
}else if(b >= a && b >= c){
max_num = b;
}else{
max_num = c;
}
cout << "最大值是: " << max_num << endl;
}
```
----
題目3: 成績等級判定
輸入一個分數根據分數給出等級 (A: 90-100, B: 80-89, C: 70-79, D: 60-69, F: 0-59)
----
題目4: 學生成績系統
改寫以下巢狀程式碼,讓它更清楚易讀
```cpp
if(studentID > 0){
if(score >= 0 && score <= 100){
if(attendance >= 80){
if(homeworkDone){
cout << "學生通過課程" << endl;
}else{
cout << "作業未完成" << endl;
}
}else{
cout << "出席率不足" << endl;
}
}else{
cout << "成績無效" << endl;
}
}else{
cout << "學號無效" << endl;
}
```
---
題庫
https://zerojudge.tw/ShowProblem?problemid=a003
https://zerojudge.tw/ShowProblem?problemid=a006
https://zerojudge.tw/ShowProblem?problemid=a053
https://zerojudge.tw/ShowProblem?problemid=a012
https://zerojudge.tw/ShowProblem?problemid=e835
----
https://zerojudge.tw/ShowProblem?problemid=a004
https://zerojudge.tw/ShowProblem?problemid=a005
https://zerojudge.tw/ShowProblem?problemid=d074
https://zerojudge.tw/ShowProblem?problemid=c013
https://zerojudge.tw/ShowProblem?problemid=c005
{"description":"by Q3goldlegeng","title":"2.流程控制","contributors":"[{\"id\":\"a8c05b3e-3d5f-46fb-979d-2545bb145f97\",\"add\":6887,\"del\":1475,\"latestUpdatedAt\":1761488006738},{\"id\":\"c5992744-4068-4fb1-9494-4eee3d2d74df\",\"add\":504,\"del\":3,\"latestUpdatedAt\":1761698157203}]"}