@(〓〓 iOS-Swift語法)[Swift 語法]
- 作者: Liwx
- 郵箱: 1032282633@qq.com
iOS Swift 語法
底層原理
與內存管理
分析 專題:【iOS Swift5語法】00 - 匯編
01 - 基礎語法
02 - 流程控制
03 - 函數
04 - 枚舉
05 - 可選項
06 - 結構體和類
07 - 閉包
08 - 屬性
09 - 方法
10 - 下標
11 - 繼承
12 - 初始化器init
13 - 可選項
目錄
- 5.循環的介紹
- for循環的寫法
- while和repeat while循環
5.循環的介紹
- 在開發中經常會需要循環
- 常見的循環有:for/while/do while.
- 這里我們只介紹for/while,因為for/while最常見
for循環的寫法
- 最常規寫法
// ----------------------------------------------------------------------------
// 1.for循環
// 1.1 常規寫法
for var i = 0; i < 10; i++ {
print(i)
}
- forin寫法: 區間遍歷
// ------------------------------------------------------------------------
// 1.2 forin寫法: 區間遍歷
for i in 0..<10 {
print(i)
}
- forin循環中如果不需要用到下標i,可以使用_來代替
// ------------------------------------------------------------------------
// 1.3 forin循環中如果不需要用到下標i,可以使用_來代替
for _ in 0..<10 {
print("hello world")
}
while和repeat while循環
- while循環
- while的判斷句
必須有明確的Bool值
,沒有非0即真 - while后面的
()可以省略
.
- while的判斷句
// ----------------------------------------------------------------------------
// 2.while循環
// while后面不需要() 2.判斷句必須有明確的Bool值
var a = 10
while a > 0 {
print(a)
a--
}
-
repeat while
循環- 使用repeat關鍵字來代替了do
// ----------------------------------------------------------------------------
// 3.repeat while循環
// Swift中do while循環是用repeat來代替do
repeat {
print(a)
a++
} while a < 10