---
# System prepended metadata

title: For and While
tags: [課程]

---

---
###### tags: `課程`
---
For and While
===

---

## while迴圈

----

**當條件符合(true)**
**就執行括號內的程式，直到條件不符合為止(false)**
```cpp 
while (執行條件){
    do_something;
}
```

----

###  印出1~10的數字

----

```cpp 
#include<iostream>
using namespace std;
int main(){
    int i = 1;
    while (i <= 10){
        cout << i << endl;        
        i++;
    }
return 0;
}
```

----


### 印出5\*5的正方形
![](https://i.imgur.com/SctOp4n.png)



----

```cpp 
#include <iostream>
using namespace std;
int main() {
    int i = 1;
    while (i <= 5) {
        int j = 1;
        while (j <= 5) {
            cout << "*";
            j++;
        }
        cout << endl;
        i++;
    }
    return 0;
}
```

----

![](https://i.imgur.com/gGc5D1T.png)
```cpp 
while (跟罹患阿茲海默症的阿嬤要錢成功) {
    等待二十分鐘
}
```

---

## FOR迴圈

----

### 格式
```cpp=
for(初始化;執行條件;每次要幹麻){
    do_something;
}

for(int i = 0; i < 3; i++)
    cout << "HI" << endl;
//宣告的i作用範圍只有迴圈內喔！
```

----

### 印出1~10

----

 ```cpp
#include<iostream>
using namespace std;
int main (){
    for(int i = 0; i < 10; i++){
        cout << i + 1 << endl; 
    }
    return 0;    
}
 ```

----

### 印出5\*5個星星

```cpp=
for(int i = 0 ; i < 5 ; i ++){
    for(int j = 0 ; j < 5 ; j++){
        cout<<'*'<<endl;
    }
    cout<<endl;
}
```


---

## for v.s. while

----

|  | For | While |
| -------- | -------- | -------- |
| 初始化    | for(<font color="#f00">here</font>; xxx; xxx)    | 自行在外面設定    |
|判斷結束|for(xxx; <font color="#f00">here</font>; xxx)|while(<font color="#f00">here</font>)|
|迴圈執行|for(;;<font color="#f00">here</font>)|自行在裡面設定|
|差異|迴圈前固定次數|迴圈中改變執行次數|
|優點|新手容易debug|使用彈性高|
|注意|**盡量不要在裡面修改結束條件**|**要確保能夠結束**|

---



## 進階用法

----

### while cin

----

如果題目沒有說會給多少次輸入 則可以使用以下語法
```cpp=
//a+b problem
int main(){
    int a, b;
    while(cin >> a >> b)
        cout << a + b <<endl;
    return 0;
}
```

----

### 不斷輸入直到輸入-1
```cpp=
int main(){
    int a;
    while(cin >> a && a != -1)
        cout << a <<endl;
    return 0;
}
```

---

## 練習時間

---

## 數字塔
給定數字n 印出如圖中的數字塔(n < 10)
![](https://i.imgur.com/6nWzHmy.png)

----

### while
 ```cpp=
#include <iostream>
using namespace std;
int main() {
    int c, a = 1, b, d;
    cin >> c;
    while (a <= c) {
        b = c - a;
        while (b > 0) {
            cout << " ";
            b--;
        }
        d = a;
        while (d > 0) {
            cout << a;
            d--;
        }
        cout << " ";
        d = a;
        while (d > 0) {
            cout << a;
            d--;
        }
        cout << endl;
        a++;
    }
} 
 ```

----

### for
```cpp=
#include <iostream>
using namespace std;
int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - i - 1; j++)
            cout << ' ';
        for (int j = 0; j < (i + 1) * 2; j++)
            cout << i + 1;
        cout << endl;
    }
    return 0;
}
```

---

## 給定a,b求 a^b^

example:
input: 5 3
output: 125

----

 ```cpp
#include<iostream>
using namespace std;
int main (){
    int a, b, ans = 1;
    //ans(總數)的初始值要設為1，不然0乘以多少都是0
    cin >> a >> b;
    for(int i = 0; i < b; i++ ){
        ans = ans * a;
    }
    cout << ans << endl;
    return 0;    
}
 ```

----

### 進階算法 fastpow

```cpp=
int main(){
    int a, b, res=1; cin >> a >> b;
    while(b){
        if(b&1) res=res*a;
        a*=a,b>>=1;
    }
    cout << res <<endl;
}
```

---

## 輸入一個數，問是否為質數
## 並找出它的因數
![](https://i.imgur.com/ZqaphQI.png)

----

```cpp
#include <iostream>
using namespace std;
int main() {
    int a, t = 0;
    cin >> a;
    for (int i = 1; i <= a; i++) {
        if (a % i == 0) {
            cout << i << "　";
            t++;
        }
    }
    if (t > 2)
        cout << "no" << endl;
    else
        cout << "yes" << endl;
    return 0;
}
```
