# 重複結構
## 2021/10/01 電算社第三堂社課
----
打疫苗還要上社課
.__________________.
---
## 迴圈
----
電算社的猴子社長要學數數,幫忙他輸出1~5吧
----
```cpp=
#include<iostream>
using namespacce std;
int main(){
cout << 1 << ' ';
cout << 2 << ' ';
cout << 3 << ' ';
cout << 4 << ' ';
cout << 5 << ' ';
return 0;
}
```
----
是不是覺得很麻煩呢
那如果要數1 -> 50 呢
----
```cpp=
#include<iostream>
using namespacce std;
int main(){
cout << 1 << ' ';
cout << 2 << ' ';
cout << 3 << ' ';
cout << 4 << ' ';
cout << 5 << ' ';
cout << 6 << ' ';
cout << 7 << ' ';
cout << 8 << ' ';
cout << 9 << ' ';
cout << 0 << ' ';
cout << 11 << ' ';
cout << 12 << ' ';
cout << 13 << ' ';
cout << 14 << ' ';
cout << 15 << ' ';
cout << 16 << ' ';
cout << 17 << ' ';
cout << 18 << ' ';
cout << 19 << ' ';
cout << 20 << ' ';
cout << 21 << ' ';
cout << 22 << ' ';
cout << 23 << ' ';
cout << 24 << ' ';
cout << 25 << ' ';
cout << 26 << ' ';
cout << 27 << ' ';
cout << 28 << ' ';
cout << 29 << ' ';
cout << 30 << ' ';
cout << 31 << ' ';
cout << 32 << ' ';
cout << 33 << ' ';
cout << 34 << ' ';
cout << 35 << ' ';
cout << 36 << ' ';
cout << 37 << ' ';
cout << 38 << ' ';
cout << 39 << ' ';
cout << 40 << ' ';
cout << 41 << ' ';
cout << 42 << ' ';
cout << 43 << ' ';
cout << 44 << ' ';
cout << 45 << ' ';
cout << 46 << ' ';
cout << 47 << ' ';
cout << 48 << ' ';
cout << 49 << ' ';
cout << 50 << ' ';
return 0;
// 講師是太閒喔
}
```
----
可以看到,一條一條輸出非常沒有效率
那我們怎麼簡化這一坨程式呢
我們要輸出1 -> 50
已知**起始值**是 1
**結束的條件**是當數字到50
每一次的**間隔**是 1 (1,2,3,4,5......)
那我們就可以寫出下列的程式
----
```cpp=
#include<iostream>
using namespace std;
int main(){
int i = 1;
while(i <= 50) // 當 i < = 50 時繼續執行
{
cout << i << ' ';
i++; // i+=1
}
return 0;
}
```
----
### 迴圈的功能
迴圈就是讓同一件事情可以重複運作
當你發現需要做的事情有**規律**的時候,
或是同一件事要做**很多遍**的時候,
就可以用迴圈
---
## while迴圈
----
```cpp=
a//先做的事
while(b){//能夠繼續執行的條件 , b為 bool
c//要執行的事
}
```
<br>
當碰到while迴圈時,程式會先判斷b是否成立,
若成立就執行c,否則就跳出迴圈
----
Examples
```cpp=
#include<iostream>
using namespace std;
int main(){
int i = 0;
while(i <= 20){
cout << i << ' ';
i += 5;
}
return 0;
}
//輸出:0 5 10 15 20
```
----
### if() vs while()
```cpp=
int a = 5 , b = 9;
if(a <= b) cout << "yes ";
// yes
while(a <= b) cout << "yes ";
// yes yes yes yes yes
```
---
## for迴圈
----
```cpp=
for(a; b; d){
c//迴圈內要做的事;
}
```
<br>
當碰到for迴圈的時候,
程式會先執行a,接著判斷b是否成立,
若成立就執行c和d,否則就跳出迴圈
a只會執行一次
----
Examples
```cpp=
#include<iostream>
using namespace std;
int main(){
for(int i = 2; i <= 18; i += 4){
cout << i << ' ';
}
return 0;
}
//輸出:2 6 10 14 18
```
----
for 跟 while 的轉換
```cpp=
for(int i = 0 ; i < 10 ; i++){
cout << i << " \n";
}
//上面的 for 迴圈其實等於下面的 while 迴圈
int i = 0;
while(i < 10){
cout << i << " \n";
i++;
}
```
---
## do_while
----
```cpp=
do{
...//迴圈內要做的事
}while(能夠繼續執行的條件); //注意分號!!!!!!!
```
與while不同的是,do_while是<font color="#f00">先執行命令再判斷</font>,
而while是<font color="#f00">先判斷再執行命令</font>
因此do_while至少會執行一次命令
----
Examples
```cpp=
#include<iostream>
using namespace std;
int main(){
int N = 100;
do{
cout << N << '\n';
}while(N < 10);
return 0;
}
//輸出 : 100
```
你會發現,即便N不小於10,他還是會輸出一次N
---
## break 、 continue
----
break : 強制退出迴圈
```cpp=
for(int i = 0 ; i < 10 ; i++){
if(i == 5){
break;
}
cout << i << ' ';
}
//輸出 : 0 1 2 3 4
```
----
continue : 終止這層迴圈,執行下一次
```cpp=
for(int i = 0 ; i < 10 ; i++){
if(i == 5){
continue;
}
cout << i << ' ';
}
//輸出 : 0 1 2 3 4 6 7 8 9
```
---
## 巢狀迴圈
----
和巢狀if一樣的意思,就是迴圈包著迴圈
----
```cpp=
while(a){
while(b){
...//a成立且b成立時執行的事
}
...//a成立且b不成立時執行的事
}
for(a; b; c){
for(d; e; f){
...//b成立且e成立時執行的事
}
...//b成立且e不成立執行的事
}
```
----
Examples
```cpp=
#include<iostream>
using namespace std;
int main(){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 4; j++){
cout << i << ' ' << j << endl;
}
cout << endl;
}
return 0;
}
/*
輸出:
0 0
0 1
0 2
0 3
1 0
1 1
1 2
1 3
2 0
2 1
2 2
2 3
*/
```
---
## 連續輸入
----
有時候會需要一次輸入很多項,這時候就可以利用迴圈達成目的
----
Examples
```cpp=
#include<iostream>
using namespace std;
int main(){
int n;//要連續輸入n項
cin >> n;
int a, b;
for(int i = 0; i < n; i++){
cin >> a >> b;
cout << a + b << ' ';
}
}
/*
輸入:
2
1 5
6 3
*/
//輸出:6 9
```
---
### 小練習
電算社的猴子社長要來學九九乘法表了,你可以幫忙他輸出九九乘法表嗎
----
輸入說明:沒有輸入
輸出說明:輸出九九乘法表,每輸出一項即換行
範例輸入:沒有輸入
範例輸出:
2 * 1 = 2
2 * 2 = 4
...
9 * 8 = 72
9 * 9 = 81
----
我是防雷頁:D
----
解答
```cpp=
#include<iostream>
using namespace std;
int main()
{
for(int i = 2; i <= 9; i++){
for(int j = 1; j <= 9; j++){
cout << i << " * " << j << " = " << i * j << '\n';
}
}
return 0;
}
```
---
### OJ練習
(依照難度)
1. [GreenJudge a021 我討厭偶數](http://www,tcgs.tc.edu.tw:1218/ShowProblem?problemid=a021)
2. [GreenJudge a037 公平的戰役(N行版)](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a037)
3. [GreenJudge a027 倒數計時](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a027)
4. [GreenJudge a023 3N+1](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a023)
5. [GreenJudge a024 所有位數和](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a024)
5. [GreenJudge a025 數字倒轉](http://www.tcgs.tc.edu.tw:1218/ShowProblem?problemid=a025)
6. [ZeroJudge a005 Eva的回家作業](https://zerojudge.tw/ShowProblem?problemid=a005)
{"metaMigratedAt":"2023-06-16T11:13:31.690Z","metaMigratedFrom":"YAML","title":"重複結構","breaks":true,"slideOptions":"{\"transition\":\"slide\",\"theme\":null}","contributors":"[{\"id\":\"9e7d687a-83f2-4e8a-8ee6-8846394e69a5\",\"add\":2527,\"del\":491},{\"id\":\"68c94489-3c2e-4879-b847-e982f360b03c\",\"add\":2227,\"del\":67},{\"id\":\"4f731eff-9d88-41f4-af56-2e3e02f20cfc\",\"add\":1512,\"del\":20}]"}