---
###### tags: `課程`
---
# 選擇結構
----
## 如果端火鍋,就會滑倒
[](https://www.youtube.com/watch?v=072tU1tamd0)
![](https://i.imgur.com/BiUoBdt.jpg)
----
## 如果端火鍋,就會滑倒
* 「在不在端火鍋」是一個條件,條件要麼成立,要麼不成立
* 如果條件成立,則導致「滑倒」
----
### 選擇結構會根據條件式判斷的結果
### 選擇不同的應對方式 有以下三種語法
1. if
2. if-else
3. if-else if
---
# C++ 中的 if
----
### if是如果條件式成立,才執行敘述,否則不執行。
```cpp=
if (統神端火鍋) {
會跌倒;
}
```
----
![](https://i.imgur.com/ntDvtGs.jpg)
----
## 範例code
```cpp=
#include<iostream>
using namespace std;
int main(){
int a;
cin>>a;
if(a == 3){
cout<<"A等於3耶";
}
if(a == 4){
cout<<"A等於4耶";
}
}
```
### 也可以寫成↓↓↓
----
```cpp=
#include<iostream>
using namespace std;
int main(){
int a;
cin>>a;
if(a == 3)cout<<"A等於3耶";
if(a == 4)cout<<"A等於4耶";
}
```
---
# if-else
----
### if-else是如果條件式成立,執行敘述1;否則執行敘述2。
![](https://i.imgur.com/rBBLq2F.png)
----
```cpp=
if(a==3)
cout<<"A等於3耶";
else cout<<"A不等於3耶";
```
---
# if-else if
----
### if-else if是依序判斷,條件式成立就執行敘述,若都不成立,就執行 else
![](https://i.imgur.com/PDFCmvj.png)
----
```cpp=
if(a == 1) cout<<"A等於1耶";
else if(a == 2) cout<<"A等於2耶";
else if(a == 3) cout<<"A等於3耶";
else cout<<"A不等於1或2或3耶"
```
---
# 關係判斷
----
* 等號(=)是指派的意思,跟數學上的等號意義不同
* 左邊的變數內容 會變成 右邊的新的儲存值
![](https://i.imgur.com/Szu1xZW.jpg)
----
* && 兩個Ampersand 代表 AND
* shift鍵+7
* || 代表 OR
* shift+\
----
```cpp=
if(是統神&&端火鍋){
會跌倒;
阿阿阿阿阿一代一代一代一代一代;
}
```
----
```cpp=
if(統神端火鍋||火鍋端統神){
也會跌倒;
阿阿阿阿阿一代一代一代一代一代;
}
```
---
# if else if VS. if if
----
## 這兩個差在哪?
----
![](https://i.imgur.com/e9N1vsB.jpg) ![](https://i.imgur.com/tQBtfSr.jpg)
----
## 範例code
----
### 全部都用if if
```cpp=
#include<iostream>
using namespace std;
int main(){
int grade;
cin>>grade;
if(grade == 100)cout<<"100分好棒"<<endl;
if(grade>=90&&grade<100)cout<<"90分以上(含)"<<endl;
if(grade>=80&&grade<90)cout<<"80分以上(含)"<<endl;
if(grade>=70&&grade<80)cout<<"70分以上(含)"<<endl;
if(grade>=60&&grade<70)cout<<"60分以上(含)"<<endl;
if(grade<60)cout<<"不及格!"<<endl;
}
```
----
### 用if else if
```cpp=
#include<iostream>
using namespace std;
int main(){
int grade;
cin>>grade;
if(grade == 100)cout<<"100分好棒"<<endl;
else if(grade>=90)cout<<"90分以上(含)"<<endl;
else if(grade>=80)cout<<"80分以上(含)"<<endl;
else if(grade>=70)cout<<"70分以上(含)"<<endl;
else if(grade>=60)cout<<"60分以上(含)"<<endl;
else cout<<"不及格!"<<endl;
}
```
---
# 例題
---
### 1.寫一程式,輸入 3 次測驗成績後,輸出最高分
![](https://i.imgur.com/qShJ7IT.png)
----
## 解答
```cpp=1
#include <iostream>
using namespace std;
int main(){
int a, b, c, max; //宣告一個變數max為最大數
cin >> a >> b >> c; //輸入a. b. c三數
max = a; //取第一個數a為最大數
if(max < b){ //取第三數c與max比較
max=b;
}
if(max < c){ //取第三數c與max比較
max = c;
}
cout << "Maximum number is " << max;
return 0;
}
```
---
### 2.輸入一個數,並判斷使用者所輸入的數是奇數還是偶數
![](https://i.imgur.com/mQpIlbX.png)
----
## 解答
```cpp=1
#include <iostream>
using namespace std;
int main(){
int input =0;
cout << "Input an integer: ";
cin >> input;
int remain = input % 2;
if(remain == 1){
cout << input << " is odd." << endl;
}else{
cout << input << " is even." << endl;
}
return;
}
```
---
### 3.判斷學生的成績等級
大於等於90分為A
大於等於80分為B
大於等於70分為C
大於等於60分為D
不及格為E
![](https://i.imgur.com/pBT40jB.png)
----
## 解答
```cpp
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
int score;
cout<<"Give :";
cin >> score;
if (score >= 90)
cout << "get A" << endl;
else if (score >= 80)
cout << "get B" << endl;
else if (score >= 70)
cout << "get C" << endl;
else if (score >= 60)
cout << "get D" << endl;
else
cout << "get E" << endl;
return 0;
}
```
---
# 挑戰題
----
## 我愛零分
----
就讀野比高中裡的學生大雄,最喜歡考零分了!一天因緣際會,大雄拿到了成績單,但他對自己許多沒考到零分的考卷並不滿意,希望可以把他們通通改成零分!但是因為大雄太厲害了,甚至可以考出低於零分的成績!所以他不希望改掉他負分的成績。
現在請你幫忙大雄,把所有成績通通改成他喜歡的 0 分,但是如果有負數的成績,不需要更動。
測資:1. -10分、5分、-45分 2. -15分、10分、0分
----
![](https://i.imgur.com/mvTz3Op.jpg)![](https://i.imgur.com/2C57JRb.jpg)
----
## 程式碼
```cpp=
#include<iostream>
using namespace std;
int main(){
int a,b,c;
cin>>a>>b>>c;
if(a>=0)cout<<"0"<<endl;
else if(a<0) cout<<a<<endl;
if(b>=0) cout<<"0"<<endl;
else if(b<0) cout<<b<<endl;
if(c>=0) cout<<"0"<<endl;
else if(c<0) cout<<c<<endl;
return 0;
}
```
---
## 一元二次方程式的解
輸入a b c 分別代表x^2項, x項, 常數項
判斷 這方程式的解是 相異根,重根,無解
----
``` cpp=1
#include<iostream>
using namespace std;
int main(){
int a,b,c,d;
cin>>a>>b>>c;
d=b*b-4*a*c;
if(d>0){
cout<<"Two different real roots"<<endl;
}else if(d==0){
cout<<"Two same roots(repeated roots)"<<endl;
}else{
cout<<"No root."<<endl;
}
return 0;
}
```
---
## 記得多多練code喔
{"metaMigratedAt":"2023-06-16T03:21:36.569Z","metaMigratedFrom":"Content","title":"選擇結構","breaks":true,"contributors":"[{\"id\":\"5b23b090-3e7f-4d31-957a-41665bdc6388\",\"add\":260,\"del\":0},{\"id\":\"71bdf46d-72d3-43b0-9f90-8e2b261abc6b\",\"add\":2818,\"del\":1367},{\"id\":\"4c515f38-ba10-4d5b-afb7-e9167f5e9034\",\"add\":708,\"del\":12},{\"id\":\"7d4f22ac-9934-417b-aa5e-c76934d4fc98\",\"add\":10,\"del\":0},{\"id\":\"82f46fc6-f9dd-4e98-8fe8-19fda0dc8ba3\",\"add\":2414,\"del\":0}]"}