# Loops迴圈
> [color=#40f1ef][name=LHB阿好伯, 2020/02/09][:earth_africa:](https://www.facebook.com/LHB0222/)
###### tags: `Python_30` `R & python`
[TOC]
許多時候需要執行重複性任務
為了處理重複的任務
R與Python都提供了while和for迴圈
R語言則還有一個repeat迴圈
# while Loops
重複執行語句塊,直到滿足給定條件為止
當條件變為FALSE時,將跳出迴圈

:::danger
```r=
# R 語法
while (迴圈條件) {
若為TRUE時執行程式碼
...
}
```
```python=
# Python 語法
while 迴圈條件:
若為TRUE時執行程式碼
```
:::
```r=
# R code
count <- 0
while (count < 5) {
print(count)
count <- count + 1
}
```
:::success
[1] 0
[1] 1
[1] 2
[1] 3
[1] 4
:::
```python=
# Python code
count = 0
while count < 5:
print(count)
count = count + 1
```
:::success
0
1
2
3
4
:::
若是跳出迴圈後有想要執行的程式碼
Python可以使用else:進行執行
:::danger
```python=
# Python 語法
while 迴圈條件:
若為TRUE時執行程式碼
else:
若為FALSE時執行程式碼
```
:::
```python=+
# Python code
count = 0
while count < 5:
print(count)
count = count + 1
else:
print(count)
```
:::success
0
1
2
3
4
5
:::
## 中斷 while Loops
在R與Python也可以設定條件中斷while迴圈

:::danger
```r=
# R 語法
while (迴圈條件) {
if(中斷條件) break
若為TRUE時執行程式碼
...
}
```
```python=
# Python 語法
while 迴圈條件:
若為TRUE時執行程式碼
if 中斷條件:
break
```
:::
```r=
# R code
count <- 0
while(count < 5){
if( count == 3)break
print(count)
count <- count + 1
}
```
:::success
[1] 0
[1] 1
[1] 2
:::
```python=
# Python code
count = 0
while count < 5:
print(count)
count = count + 1
if count == 3:
break
```
:::success
0
1
2
:::
## 跳過while Loops
前面設定break可以中斷while迴圈
Python可以使用continue跳過特定條件

:::danger
```python=
# Python 語法
while 迴圈條件:
if 中斷條件:
continue
若為TRUE時執行程式碼
```
:::
但在這邊遇到問題
似乎是while並無法使用continue語法並未如範例得到一樣的結果
```python=
# Python code
count = 0
while count < 5:
if count == 3:
continue
count = count + 1
print(count)
```
:::success
1
2
3
:::
# For Loop
for迴圈是最常見的迴圈使用方式
其中iterator疊代器只能使用在list, tuple, dictionary, set, string格式
當然不管是R或是Python語言中都可以使用for迴圈
:::danger
```r=
# R 語法
for(疊代器 in 清單){
執行程式碼(須包含疊代器)
...
}
```
```python=
# Python 語法
for 疊代器 in 清單:
執行程式碼(須包含疊代器)
```
:::
```r=
# R code
language <- c(1, 2, 3, 4, 5)
for(letter in language){
print(letter)
}
```
:::success
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
:::
R語言常用冒號 ==:== 來生成iterator的清單
例如
```r=
# R code
language <- c(1 : 5)
for(letter in language){
print(letter)
}
```
:::success
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
:::
```python=
# Python code
language = list((1, 2, 3, 4, 5))
for letter in language:
print(letter)
```
:::success
1
2
3
4
5
:::
python則可以使用 ==`range(起始, 結束, 間隔)`== 來生成iterator的清單
跟[Lists清單](/vCN7TwuWRKW-QDmoznnbSA)單元說到的Indexing正索引一樣
range是由0開始生成比如要寫0到6的整數列,就是range(7)
如果是1到6的整數列就是range(1, 7)
若6到1則是range(6, 0, -1)
等距間隔例如1, 3, 5, 7, 9則是range(1, 10, 2)
```python=
# Python code
language = list((1, 2, 3, 4, 5))
for letter in language:
print(letter)
```
## 中斷for loops
與While一樣可以設定條件中斷while迴圈
而R語言的寫法也不太一樣須注意
:::danger
```r=
# R 語法
for(疊代器 in 清單){
if(中斷條件) break
執行程式碼(須包含疊代器)
...
}
```
```python=
# Python 語法
for 疊代器 in 清單:
執行程式碼
if 中斷條件:
break
```
:::
```r=
# R code
language <- c(0, 1, 2, 3, 4, 5)
for(letter in language){
print(letter)
if(letter == 3) break
}
```
:::success
[1] 0
[1] 1
[1] 2
[1] 3
:::
```python=
# Python code
numbers = (0,1,2,3,4,5)
for number in numbers:
print(number)
if number == 3:
break
```
:::success
0
1
2
3
:::
## 跳過for Loops
在R語言則可以使用next語法跳過設定條件執行後續迴圈
python則使用continue語法
:::danger
```r=
# R 語法
for(疊代器 in 清單){
if(中斷條件) next
執行程式碼(須包含疊代器)
...
}
```
```python=
# Python 語法
for 疊代器 in 清單:
if 中斷條件:
continue
執行程式碼
```
:::
```r=
# R code
language <- c(0, 1, 2, 3, 4, 5)
for(letter in language){
if(letter == 3) next
print(letter)
}
```
:::success
[1] 0
[1] 1
[1] 2
[1] 4
[1] 5
:::
```python=
# Python code
numbers = (0,1,2,3,4,5)
for number in numbers:
if number == 3:
continue
print(number)
```
與前面說明的一樣Python支援迴圈結束後使用else:執行下一步程式碼
```python=+
# Python code
numbers = (0,1,2,3,4,5)
for number in numbers:
if number == 3:
continue
print(number)
else :
print("End")
```
:::success
0
1
2
4
5
End
:::
# repeat重複循環
在R語言中使用repeat將重複至循環按下Escape或是給予停止指令
也可以使用Sys.sleep()設定暫停執行的時間間隔,以秒為單位
:::danger
```r=
# R 語法
repeat {
執行的程式碼
...
if(退出條件) {break} #結束循環
if(退出條件) {next} #跳至下一個循環
Sys.sleep() #暫停執行
}
```
:::
🌟全文可以至下方連結觀看或是補充
https://hackmd.io/@LHB-0222/Loops
https://www.facebook.com/LHB0222/
https://www.instagram.com/ahb0222/
有疑問想討論的都歡迎於下方留言
喜歡的幫我分享給所有的朋友 \o/
有所錯誤歡迎指教
# [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings)
