---
# System prepended metadata

title: 流程控制
tags: [else, if, Python, 流程控制, elif]

---

{%hackmd  @dzif24x25/IBXIWNXgTju0h2AKtSaLkA %}

###### tags: `Python` `流程控制` `if` `elif` `else`

# 流程控制

## 比較運算子

### `>`  大於：

```python
x = 3
y = 4
print(x > y)
```
輸出結果
```bash
False
```
---
### `<`  小於：

```python
x = 3
y = 4
print(x < y)
```
輸出結果
```bash
True
```
---
### `>=` 大於或等於：

```python
x = 4
y = 4
print(x >= y)
```
輸出結果
```bash
True
```
---
### `<=` 小於或等於：

```python
x = 4
y = 4
print(x <= y)
```
輸出結果
```bash
True
```
---
### `==` 等於：

```python
x = 4
y = 4
print(x == y)
```
輸出結果
```bash
True
```
---
### `!=` 不等於：

```python
x = 4
y = 4
print(x != y)
```
輸出結果
```bash
False
```
---
## 邏輯運算子

### `and` 而且：

| `and`運算 | A: <font color=#448aff>True</font>  | B: <font color=#ff5252>False</font> |
| -------- | -------- | -------- |
| **B: <font color=#448aff>True</font>**  | <font color=#448aff>True</font>     | <font color=#ff5252>False</font>     |
| **B: <font color=#ff5252>False</font>** | <font color=#ff5252>False</font>     | <font color=#ff5252>False</font>    |

```python
print('T and T =', True and True)
print('T and F =', True and False)
print('F and T =', False and True)
print('F and F =', False and False)
```
輸出結果
```bash
T and T = True
T and F = False
F and T = False
F and F = False
```
---

### `or` 或者：

| `or`運算 | A: <font color=#448aff>True</font>  | B: <font color=#ff5252>False</font> |
| -------- | -------- | -------- |
| **B: <font color=#448aff>True</font>**  | <font color=#448aff>True</font>     | <font color=#448aff>True</font>     |
| **B: <font color=#ff5252>False</font>** | <font color=#448aff>True</font>     | <font color=#ff5252>False</font>    |

```python
print('T and T =', True or True)
print('T and F =', True or False)
print('F and T =', False or True)
print('F and F =', False or False)
```
輸出結果
```bash
T and T = True
T and F = True
F and T = True
F and F = False
```
---

### `not` 非：

| `not`運算 | A: <font color=#448aff>True</font>  | A: <font color=#ff5252>False</font> |
| -------- | -------- | -------- |
|   | <font color=#ff5252>False</font>     | <font color=#448aff>True</font>     |

```python
print('not True =', not True)
print('not False =', not False)
```
輸出結果
```bash
not True = False
not False = True
```
---
## if敘述

程式碼：
```python
if 條件敘述:
    程式區塊
```

流程圖：
```flow
st=>start: if 開始
cond=>condition: 條件敘述
chunk=>operation: 程式區塊
en=>end: if 結束

st->cond
cond(yes)->chunk->en
cond(no)->en

```

:::info
註：若有程式區塊有多行，每一行程式的空格必須相同
```python
if 條件敘述:
****程式區塊
****程式區塊
****程式區塊
```
若有程式區塊只有單行，可以這樣寫：
```python
if 條件敘述: 程式區塊
```
:::

## if...else敘述

程式碼：
```python
if 條件敘述:
    程式區塊A
else:
    程式區塊B
```

流程圖：
```flow
st=>start: if...else 開始
cond=>condition: 條件敘述
chunkA=>operation: 程式區塊A
chunkB=>operation: 程式區塊B
en=>end: if...else 結束

st->cond
cond(yes)->chunkA->en
cond(no)->chunkB->en
```

## if...elif...else敘述

程式碼：
```python
if 條件敘述1:
    程式區塊1
elif 條件敘述2:
    程式區塊2
elif 條件敘述3:
    程式區塊3
else:
    程式區塊n
```

流程圖：
```flow
st=>start: if...elif...else 開始
cond1=>condition: 條件敘述1
cond2=>condition: 條件敘述2
cond3=>condition: 條件敘述3
chunk1=>operation: 程式區塊1
chunk2=>operation: 程式區塊2
chunk3=>operation: 程式區塊3
chunkn=>operation: 程式區塊n
en=>end: if...elif...else 結束

st->cond1
cond1(yes)->chunk1->en
cond1(no)->cond2
cond2(yes)->chunk2->en
cond2(no)->cond3
cond3(yes)->chunk3->en
cond3(no)->chunkn->en
```

### 範例 - 成績等級計算：
```python=
score = int(input('請輸入您的分數：'))
if score >= 90:
    level = '甲等'
elif score >= 80:
    level = '乙等'
elif score >= 70:
    level = '丙等'
elif score >= 60:
    level = '丁等'
else:
    level = '不及格'
print('您的成績為', level)
```

輸出結果：
![](https://i.imgur.com/Z20Fgdh.gif)
![](https://i.imgur.com/iCvi5KN.gif)


:::info
點此前往簡報版： [https://hackmd.io/@dzif24x25/BJ4dPCSRo](https://hackmd.io/@dzif24x25/S1sgNYCRs)
:::
