# 迴圈
---
# while迴圈
----
### 用於不知道要重複多少次
### 用條件判斷是否結束或繼續
----
## 最基本架構
```cpp=
#include<iostream>
using namespace std;
int main{
while(/*條件判斷*/){
//在迴圈中執行的程式碼
}
return 0;
}
```
----
# EXAMPLES
```cpp=
#include<iostream>
using namespace std;
int main(){
int i= 0
while(i<5){
cout<<i<<" ";
i++;
}
return 0;
}
```
### 最後輸出 0 1 2 3 4
----
# MDJUDGE練習題目
* ## L014 階乘
## (先試著用while解解看)
---
# for迴圈
----
**for迴圈為進階且更實用的while迴圈**
**for迴圈基本上都能做到while迴圈能做的事**
----
## 最基本結構
```cpp=
#include<iostream>
using namespace std;
int main(){
for(/*一開始做的事情*/;/*條件判斷*/;/*跑完一次程式後做的事*/){
//在迴圈中執行的程式碼
}
return 0;
}
```
----
# EXAMPLES
```cpp=
#include <iostream>
using namespace std;
int main(){
for(int i=1;i<=5;i++){
cout<<i<<" ";
}
return 0;
}
```
### 輸出1 2 3 4 5
----
### 試著用for迴圈解出剛剛的階乘吧!
## L014 階乘
---
# 陣列
----
## 陣列是什麼?
是許多個相同型態的變數,歸類在一串稱作「陣列」的鏈裡面。
----
## 宣告方式:
```cpp
int number[100];
```
int代表的是這個陣列的變數型態
number為這個陣列的名稱
[100]為陣列的大小
----
```cpp=
#include <iostream>
using namespace std;
int main(){
int a[100];
a[0]=18;
a[1]=19;
//以此類推到a[99]
}
```
### 陣列中的第一個索引值從0開始
----
## 結合for迴圈輸出
```cpp=
#include <iostream>
using namespace std;
int main(){
int a[100];
a[0]=18;
a[1]=19;
for(int i=0;i<2;i++){
cout<<a[i]<<" ";
}
}
```
----
## 利用for迴圈輸入和輸出
```cpp=
#include <iostream>
using namespace std;
int main(){
int a[100];
for(int i=0;i<3;i++){
cin>>a[i];
}
for(int i=0;i<3;i++){
cout<<a[i]<<" ";
}
}
```
----
# MDJUDGE練習題目
## A021 電車載客數LV1
----
### 那如果指定要導入多少個數值呢?
### 試著練習A022 電車載客數LV2
----
## MDJUDGE 進階練習試題
* ## A006 最後倒數
* ## A009 找最大值
{"title":"資訊科技培訓4","contributors":"[{\"id\":\"96cadc34-5f09-4c45-a574-a806328f5462\",\"add\":1977,\"del\":247}]"}