반응형
let closedRange = 0...10 //lowerBound 0, upperBound 10
let halfClosedRange = 0..<10 //lowerBound 0, upperBound 9
var sum = 0
for i in closedRange {
print("---> \(i)")
sum += i
}
print("----> total sum: \(sum)")
/*
---> 0
---> 1
---> 2
---> 3
---> 4
---> 5
---> 6
---> 7
---> 8
---> 9
---> 10
----> total sum: 55
*/
let closedRange = 0...10 //lowerBound 0, upperBound 10
let halfClosedRange = 0..<10 //lowerBound 0, upperBound 9
var sum = 0
for i in halfClosedRange {
print("---> \(i)")
sum += i
}
print("----> total sum: \(sum)")
/*
---> 0
---> 1
---> 2
---> 3
---> 4
---> 5
---> 6
---> 7
---> 8
---> 9
----> total sum: 45
*/
// -------> Range + for 문 (1)
let closedRange = 0...10 //lowerBound 0, upperBound 10
let name = "Bang"
for _ in closedRange {
print("i")
}
// -------> Array + for 문 (2)
var shopingList = ["carrot", "potato"]
for i in shopingList {
print(shopingList[0])
}
import UIKit
let closedRange = 0...10 //lowerBound 0, upperBound 10
// CGFloat은 하나의 데이터 타입인데 CG 코어그래픽스 개발하다가 종종 볼수 있음
var sinValue: CGFloat = 0
for i in closedRange {
sinValue = sin(CGFloat.pi/4 * CGFloat(i))
}
// Jason 여러번 프린트하기
let name = "Jason"
for i in closedRange { // Immutable value 'i' was never used; consider replacing with '_' or removing it
print("---> name: \(name)")
}
/*
---> name: Jason
---> name: Jason
---> name: Jason
---> name: Jason
---> name: Jason
---> name: Jason
---> name: Jason
---> name: Jason
---> name: Jason
---> name: Jason
---> name: Jason
*/
쓰지않는 변수가 있다면 경고가 뜬다. "_"로 변경해서 해결할 수 있다고 한다.
Immutable value 'i' was never used; consider replacing with '_' or removing it
for loop 심화
import UIKit
let closedRange = 0...10 //lowerBound 0, upperBound 10
for i in closedRange {
if i % 2 == 0 { // %는 앞을 2로 나눴을 때 나머지
print("---> 짝수: \(i)")
}
}
/*
---> 짝수: 0
---> 짝수: 2
---> 짝수: 4
---> 짝수: 6
---> 짝수: 8
---> 짝수: 10
*/
// 조금더 깔끔한 버전
for i in closedRange where i % 2 == 0 { //closedRange에서 하나씩 뽑아오는데 오른쪽 을 만족하는 것을 가져온다
print("---> 짝수: \(i)")
}
//
import UIKit
let closedRange = 0...10 //lowerBound 0, upperBound 10
for i in closedRange where i % 2 == 0 { //closedRange에서 하나씩 뽑아오는데 오른쪽 을 만족하는 것을 가져온다
print("---> 짝수: \(i)")
}
for i in closedRange {
if i == 3{
continue // i가 3이면 아래를 뛰어넘어라
}
print("---> \(i)")
}
/*
---> 0
---> 1
---> 2
---> 4
---> 5
---> 6
---> 7
---> 8
---> 9
---> 10
*/
// for 루프 중첩사용하기
import UIKit
let halfclosedRange = 1..<10 //lowerBound 1, upperBound 10
//중첩은 너무 많아지면 수행성능도 안좋아지고 가독성도 안좋아서 협업시 싫어할 수 있다.
for i in halfclosedRange {
for j in halfclosedRange{
print("gugu -> \(i) * \(j) = \(i * j)")
}
}
/*
gugu -> 1 * 1 = 1
gugu -> 1 * 2 = 2
gugu -> 1 * 3 = 3
gugu -> 1 * 4 = 4
gugu -> 1 * 5 = 5
gugu -> 1 * 6 = 6
gugu -> 1 * 7 = 7
gugu -> 1 * 8 = 8
gugu -> 1 * 9 = 9
gugu -> 2 * 1 = 2
gugu -> 2 * 2 = 4
gugu -> 2 * 3 = 6
gugu -> 2 * 4 = 8
gugu -> 2 * 5 = 10
gugu -> 2 * 6 = 12
gugu -> 2 * 7 = 14
gugu -> 2 * 8 = 16
gugu -> 2 * 9 = 18
gugu -> 3 * 1 = 3
gugu -> 3 * 2 = 6
gugu -> 3 * 3 = 9
gugu -> 3 * 4 = 12
gugu -> 3 * 5 = 15
gugu -> 3 * 6 = 18
gugu -> 3 * 7 = 21
gugu -> 3 * 8 = 24
gugu -> 3 * 9 = 27
gugu -> 4 * 1 = 4
gugu -> 4 * 2 = 8
gugu -> 4 * 3 = 12
gugu -> 4 * 4 = 16
gugu -> 4 * 5 = 20
gugu -> 4 * 6 = 24
gugu -> 4 * 7 = 28
gugu -> 4 * 8 = 32
gugu -> 4 * 9 = 36
gugu -> 5 * 1 = 5
gugu -> 5 * 2 = 10
gugu -> 5 * 3 = 15
gugu -> 5 * 4 = 20
gugu -> 5 * 5 = 25
gugu -> 5 * 6 = 30
gugu -> 5 * 7 = 35
gugu -> 5 * 8 = 40
gugu -> 5 * 9 = 45
gugu -> 6 * 1 = 6
gugu -> 6 * 2 = 12
gugu -> 6 * 3 = 18
gugu -> 6 * 4 = 24
gugu -> 6 * 5 = 30
gugu -> 6 * 6 = 36
gugu -> 6 * 7 = 42
gugu -> 6 * 8 = 48
gugu -> 6 * 9 = 54
gugu -> 7 * 1 = 7
gugu -> 7 * 2 = 14
gugu -> 7 * 3 = 21
gugu -> 7 * 4 = 28
gugu -> 7 * 5 = 35
gugu -> 7 * 6 = 42
gugu -> 7 * 7 = 49
gugu -> 7 * 8 = 56
gugu -> 7 * 9 = 63
gugu -> 8 * 1 = 8
gugu -> 8 * 2 = 16
gugu -> 8 * 3 = 24
gugu -> 8 * 4 = 32
gugu -> 8 * 5 = 40
gugu -> 8 * 6 = 48
gugu -> 8 * 7 = 56
gugu -> 8 * 8 = 64
gugu -> 8 * 9 = 72
gugu -> 9 * 1 = 9
gugu -> 9 * 2 = 18
gugu -> 9 * 3 = 27
gugu -> 9 * 4 = 36
gugu -> 9 * 5 = 45
gugu -> 9 * 6 = 54
gugu -> 9 * 7 = 63
gugu -> 9 * 8 = 72
gugu -> 9 * 9 = 81
*/
for loop 와 while loop 를 비교해보자
while문은 Boolean 값이 조건이다.
for는 배열이 들어온다??
print("--- while")
var i = 0
while i < 10 {
print(i)
i += 1
}
print("--- Repeat")
i = 0
repeat {
print(i)
i += 1
} while i < 10
print("--- for 문")
let closedRange = 0...10 //lowerBound 1, upperBound 10
for i in closedRange {
반응형
'모바일앱 > Swift' 카테고리의 다른 글
(중간점검) 문법 공부 중 느낀 점 (0) | 2021.10.02 |
---|---|
컬렉션 타입 (0) | 2021.10.02 |
플로우 컨트롤 복습하기 (21.09.5주) (0) | 2021.10.01 |
플로우컨트롤. (조건문) Switch (0) | 2021.09.29 |
플로우컨트롤. (반복문) while, repeat (0) | 2021.09.29 |
Flow control (0) | 2021.09.29 |
Scope (0) | 2021.09.28 |
튜플(Tuple) (0) | 2021.09.28 |