---
title: 'Swift - Loop'
---
while
---
```swift=
var total = 0, index = 1
while index <= 10 {
total += index
index += 1
}
print("1 加到 \(index-1) 的總和為 \(total) ")
```
**output**
> 1 加到 10 的總和為 55
repeat...while
---
```swift=
var total = 0, index = 1
repeat{
total += index
index += 1
}while index <= 10
print("1 加到 \(index-1) 的總和為 \(total) ")
```
**output**
> 1 加到 10 的總和為 55
for-in
---
```swift=
var total = 0
for index in 1...10 {
total += index
}
print("1 加到 10 的總和為 \(total) ")
```
**output**
> 1 加到 10 的總和為 55
```swift=
var string = "Swfit"
//印出字串中的所有字元
for char in string {
print(char)
}
```
**output**
> S
w
f
i
t
```swift=
let end = 3
//不會用到區間值,用“_”代替
for _ in 1...end {
print("Hello World!")
}
```
**output**
> Hello World!
Hello World!
Hello World!
###### tags: `Swift`