1.
for
/while
是最常見的兩種循環-
2.
for
循環的寫法-
1.最常規寫法
傳統寫法 for var i = 0; i < 10; i++ { print(i) } 區間for循環 for i in 0..<10 { print(i) } for i in 0...10 { print(i) }
-
區間for循環
特殊寫法
如果在`for`循環中不需要用到下標 `i`
for _ in 0..<10 {
print("hello")
}
-
3.
while
和do while
循環-
while
循環
while的判斷句必須有正確的真假,沒有非0即真
while后面的()可以省略var a = 0 while a < 10 { a += 1 } print(a)
-
do while
循環 使用repeat
關鍵字來代替了do
使用repeat關鍵字來代替了dovar b = 0 repeat { print(b) b += 1 } while b < 20
-