본문 바로가기
Mobile/Swift

Loops

by 꼰대코더 2024. 2. 14.

for in

let platforms = ["iOS", "macOS", "tvOS", "watchOS"]

for os in platforms {
    print("Swift works great on \(os).")
}
Swift works great on iOS.
Swift works great on macOS.
Swift works great on tvOS.
Swift works great on watchOS.
// 5를 포함
for
i in 1...5 {

    print("Counting from 1 through 5: \(i)")
}

// 5를 미포함
for i in 1..<5 {
    print("Counting 1 up to 5: \(i)")
}
Counting from 1 through 5: 1
Counting from 1 through 5: 2
Counting from 1 through 5: 3
Counting from 1 through 5: 4
Counting from 1 through 5: 5

Counting from 1 through 5: 1
Counting from 1 through 5: 2
Counting from 1 through 5: 3
Counting from 1 through 5: 4
for _ in 1...5 {
    
}
숫자를 포함하지 않고 5번 루프만...

 

'Mobile > Swift' 카테고리의 다른 글

예외처리  (0) 2024.02.14
function  (0) 2024.02.14
switch  (0) 2024.02.14
String  (0) 2024.02.08
enum  (0) 2024.02.07