# **while loop**
### 2020 資訊之芽
#### 2020/3/14
#### 實習生 袁紹奇
---
# **在這之前...**
----
### **Recall:**
| 算術運算子 | 符號 | 例子 |
| ---------- | ---- | ------ |
| 加 | + | 3+2→5 |
| 減 | - | 3-2→1 |
| 乘 | * | 3*2→6 |
| 除 | / | 3/2→1 |
| 模 | % | 3%2→1 |
----
### 好用的語法
| 複合指定運算子 | 例子 | 運算式 |
| -------------- | ------ | --------- |
| 加法指派 | a += b | a = a + b |
| 減法指派 | a -= b | a = a - b |
| 乘法指派 | a *= b | a = a * b |
| 除法指派 | a /= b | a = a / b |
| 模法指派 | a %= b | a = a % b |
----
int a = 1;
| 符號 | 順序 | 舉例 | 結果 |
| ---- | --------- | ------------- | -------- |
| ++ | 遞增→回傳 | int b = ++a; | a:2, b:2 |
| -\- | 遞減→回傳 | int b = --a; | a:0, b:0 |
| ++ | 回傳→遞增 | int b = a++; | a:2, b:1 |
| -\- | 回傳→遞減 | int b = a-\-; | a:0, b:1 |
---
# 印出1~10的數字?
----
### 你可能會這樣做
```cpp
#include <iostream>
int main(){
std::cout << "1" << std::endl;
std::cout << "2" << std::endl;
std::cout << "3" << std::endl;
std::cout << "4" << std::endl;
std::cout << "5" << std::endl;
std::cout << "6" << std::endl;
std::cout << "7" << std::endl;
std::cout << "8" << std::endl;
std::cout << "9" << std::endl;
std::cout << "10" << std::endl;
return 0;
}
```
----
### 如果想要印到10000怎麼辦?
---
```cpp
while(condition){
statement;
statement;
statement;
}
```
### 當 condition 為真時,
### 執行 { } 裡的 statements
#### 注意:condition非零就是true
----
### 流程圖

---
### 語法
印出1~10000的數字!
```cpp
#include <iostream>
int main(){
int i = 1;
while(i <= 10000){
std::cout << i << std::endl;
i++;
}
}
```
----
視覺化效果
http://pythontutor.com/visualize.html#mode=edit\
---
### 關於那個括號...?
```cpp
#include <iostream>
int main(){
int a = 1;
while(a <= 100)
a++;
return 0;
}
```
#### 和 if, else 都一樣
----
### 所以我說那個括號?
```cpp
#include <iostream>
int main(){
int a = 1;
while(a <= 100)
std::cout << a << std::endl;
a++;
return 0;
}
```
----
### 正確做法
```cpp
#include <iostream>
int main(){
int a = 1;
while(a <= 100){
std::cout << a << std::endl;
a++;
}
return 0;
}
```
---
### 如果 condition 一直都是 true?
----
### 無窮迴圈
### infinite loop
----
### 無窮迴圈
```cpp
int i = 1;
while(i <= 100)
std::cout << "I love sprout." << std::endl;
```
### i值永遠<=100,condition恆為true
----
### 無窮迴圈
```cpp
#include <iostream>
int main(){
while(1)
;
return 0;
}
```
---
# while裡的while?
----
### 想印出...
```
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
```
----
### 巢狀迴圈
```cpp
#include <iostream>
int main(){
int i = 1, j;
while(i <= 5){
j = 1;
while(j <= i){
std::cout << j << ' ';
j++;
}
std::cout << std::endl;
i++;
}
}
```
----
視覺化效果
http://pythontutor.com/visualize.html#mode=edit\
----
練習(893)
https://neoj.sprout.tw/problem/893/
----

----
### HINT
```cpp
int n = 10;
while(n--){
statement;
}
// execute the statement for n times
```
----
### solution
```cpp
#include <iostream>
int main(){
int n;
std::cin >> n;
int space = n - 1, time = n, num = 1;
while(time--){
int i = space;
while(i--)
std::cout << ' ';
int j = num;
while(j--)
std::cout << num;
std::cout << ' ';
j = num;
while(j--)
std::cout << num;
std::cout << std::endl;
num++;
space--;
}
}
```
---
### while的小技巧
```cpp
while(std::cin >> n){
std::cout << n << std::endl;
}
```
---
## break
#### 提前結束while迴圈
## continue
#### 直接進入下一個迴圈(跳過本次迴圈的內容)
----
請使用者輸入一個整數,規則如以下:
* 如果輸入為負數或零停止
* \>=1000什麼都不做,繼續下一個輸入
* 偶數輸出 "even"
* 奇數輸出 "odd"
----
### Solution
```cpp
#include <iostream>
int main(){
int n;
while(std::cin >> n){
if(n <= 0)
break;
else if(n >= 1000)
continue;
else if(n % 2 == 1)
std::cout << "odd" << std::endl;
else
std::cout << "even" << std::endl;
}
return 0;
}
```
---
# do-while
#### 與while的差別:至少執行一次
----
#### 範例
```cpp
int x = 9527;
do{
std::cout << x << std::endl;
std::cin >> x;
}
while (x <= 10000);
```
----
### 流程圖

---
## 重點回顧
* \++i v.s i++
* continue? break?
* while & do-while
{"metaMigratedAt":"2023-06-15T05:05:04.029Z","metaMigratedFrom":"Content","title":"**while loop**","breaks":true,"contributors":"[{\"id\":\"c5aa2397-41e9-4519-9c42-c485a8ba1b50\",\"add\":5370,\"del\":913}]"}