์กฐ๊ฑด๋ฌธ : ํ๋ก๊ทธ๋๋ฐ์ ํน์ ์กฐ๊ฑด์์ ์ฝ๋๋ฅผ ์คํํ๊ฑฐ๋, ํ์ง ๋ง์์ผ ํ ์ํฉ์ด ์๊ธธ ๋ ์ฌ์ฉ
๋ฐ๋ณต๋ฌธ : ํน์ ๋ช ๋ น์ด๋ฅผ ๋ฐ๋ณตํด์ ์คํํด์ผ ํ ๋ ์ฌ์ฉ
์ค์ํํธ์ ํ๋ฆ ์ ์ด ๊ตฌ๋ฌธ์์๋ ์๊ดํธ(())๋ฅผ ๋๋ถ๋ถ ์๋ตํ ์ ์์.
์ค๊ดํธ๋ ์๋ตํ ์ ์์
ํจ์ ๋ด๋ถ์์ ์คํ๋๋ ์ ํ๋ฌธ
ํน์ ์กฐ๊ฑด์ ๋ฐ๋ผ ์ ํ์ ์ผ๋ก ์ฝ๋๋ฅผ ์คํ์ํด
๋ํ์ ์ธ ์กฐ๊ฑด๋ฌธ : if-else ๋ฌธ๊ณผ switch ๋ฌธ
๋ณดํธ์ ์ผ๋ก if-else๋ฅผ ๋ง์ด ์ฌ์ฉํ๋, swift์์๋ switch-case๊ฐ ๊ฐํ๋์์
์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์ฐ if๋ฌธ ๋๊ดํธ ์์ ์ฝ๋๊ฐ ์คํ๋จ
๋ง์ฝ ์กฐ๊ฑด์ด ๊ฑฐ์ง์ธ ๊ฒฝ์ฐ else ๋ฌธ ๋๊ดํธ ์์ ์ฝ๋๊ฐ ์คํ๋จ
if ์กฐ๊ฑด {
//์กฐ๊ฑด์ด ๋ง์กฑ๋๋ฉด ์คํ
} else {
//์กฐ๊ฑด์ด ๋ง์กฑ๋์ง ์์ ๋ ์คํ
}์กฐ๊ฑด๊ฐ์ ์ฐธ, ๊ฑฐ์ง์ ๋ํ๋ด๋ Bool๊ฐ์ผ๋ก ํํ
์ถ๊ฐ ์กฐ๊ฑด ๋ฐฉ๋ฒ์ผ๋ก ๋ฐ๋ณตํด์ ์ถ๊ฐ ํ ์ ์์
if ์กฐ๊ฑด1 {
//์กฐ๊ฑด1์ด ๋ง์กฑ๋๋ฉด ์คํ
} else if ์กฐ๊ฑด2 {
//์กฐ๊ฑด1์ด ๋ง์กฑ๋์ง ์๊ณ , ์กฐ๊ฑด2๊ฐ ๋ง์กฑ ๋ ๋ ์คํ
} else {
// ์กฐ๊ฑด๋ค ๋ชจ๋ ๋ง์กฑ๋์ง ์์ ๋ ์คํ
}์กฐ๊ฑด2๋ ์กฐ๊ฑด1์ด ๊ฑฐ์ง์ผ ๋ ์คํ
๋ง์ง๋ง else๋ ์๋ตํ ์ ์์
๋น๊ต ์ฐ์ฐ์๋ฅผ ํตํด ์กฐ๊ฑด์ ๊ฒฐ๊ณผ๊ฐ bool๊ฐ์ผ๋ก ๋์์ผ ํจ.
๋ ผ๋ฆฌ ์ฐ์ฐ์๋ก ๋ค์ํ ์กฐ๊ฑด์ ์กฐํฉ์ด ๊ฐ๋ฅํจ.
- ์ํ์์ ์ฌ์ฉํ๋ ์ฐ์ฐ์์ ๊ฐ์
+ : ๋ํ๊ธฐ
- : ๋นผ๊ธฐ
* : ๊ณฑํ๊ธฐ
/ : ๋๋๊ธฐ
% : ๋๋จธ์ง
== : ๊ฐ๋ค A == B
>= : ํฌ๊ฑฐ๋ ๊ฐ๋ค A >= B
<= : ์๊ฑฐ๋ ๊ฐ๋ค A <= B
> : ํฌ๋ค A > B
< : ์๋ค A < B
!= : ๊ฐ์ง ์๋ค A != B
&& : AND -> A && B --> A์กฐ๊ฑด์ด ์ฐธ์ด๊ณ , B์กฐ๊ฑด์ด ์ฐธ์ด๋ฉด ์ฐธ์ด๋ค.
|| : OR -> A || B ==> A์กฐ๊ฑด์ด๋,B์กฐ๊ฑด ๋์ค์ ํ๋๊ฐ ์ฐธ์ด๋ฉด ์ฐธ์ด๋ค.
! : NOT -> !A ==> A ์กฐ๊ฑด์ ๋ฐ๋
์ฐ์ฐ์ ์์ ์ค๋ช
-------------------------------------
+= a += 1 a์ ๊ฐ์ ๋ํ๊ธฐ
-= b -= 2 b์ ๊ฐ์ ๋นผ๊ธฐ
*, /, % ๋ ๊ฐ๋ฅ
์ฐ์ฐ์ ์์ ์ค๋ช
-------------------------------------------
a...b 3...10 a~b๊น์ง์ ์ซ์
a..<b 0..<10 a~b๊น์ง ์ซ์์ค b๋ ํฌํจ์ํจ
1... : 1์ด์์ ๋ชจ๋ ์
1.2 ..< 1.5 --> 1.2, 1,3, 1,4 (o), 1.12 (x)
์ฐ์ฐ์ ์์ ์ค๋ช
--------------------------------------------------------------------------
=== person2 === person1 person1๊ณผ person2๋ ๊ฐ์ ์ธ์คํด์ค๋ฅผ ์ฐธ์กฐํ๊ณ ์๋ค.
!== person2 !== person1 person1๊ณผ person2๋ ๋ค๋ฅธ ์ธ์คํด์ค๋ฅผ ์ฐธ์กฐํ๊ณ ์๋ค.
๊ฐ์ฒด๊ฐ ๊ฐ์์ง ํ์ธ, reference๊ฐ ๊ฐ์์ง ํ์ธ
question? answer1 : answer2
-> question์ด ์ฐธ์ด๋ฉด answer1 ์คํ, ๊ฑฐ์ง์ด๋ฉด answer2 ์คํ
let age = 20
var result: String = age > 19 ? "์ฑ๋
" : "๋ฏธ์ฑ๋
"func printGeneration(age:Int)
{
if (age >= 30) {
print(โ30๋ ์ด์")
} else if(age >= 20) {
print("20๋")
} else {
print("๋ฏธ์ฑ๋
์")
}
}
// ์คํ๊ฒฐ๊ณผ
printGeneration(10);
>> ๋ฏธ์ฑ๋
์
printGeneration(40);
>> 30๋ ์ด์
printGeneration(20);
>> 20๋
//์์ 1
func check(name compareName: String) {
if compareName == "์ฃผ์๋ฏผ" {
print(โ์ฃผ์๋ฏผ ์
๋๋ค.โ)
} else {
print("์ฃผ์๋ฏผ์ด ์๋๋๋ค")
}
}
// ์์ 2
func isEven(number:Int) -> Bool
{
if(number%2 == 0)
{
//์ง์
return true
} else {
//ํ์
return false
}
}- or์กฐํฉ(||): ์กฐ๊ฑด๋ค ์ค ํ๊ฐ๋ง ์ฐธ์ด์ด๋ ์ ์ฒด ์กฐ๊ฑด์ด ์ฐธ์
- and์กฐํฉ(&&) : ์กฐ๊ฑด๋ค ๋ชจ๋ ์ฐธ์ด์ฌ๋ง ์ ์ฒด ์กฐ๊ฑด์ด ์ฐธ์
- not(!) : ์กฐ๊ฑด ๊ฒฐ๊ณผ์ ๋ฐ๋๊ฐ
- ํจํด ๋น๊ต๋ฌธ
- ๊ฐ์ฅ ์ฒซ๋ฒ์งธ ๋งค์นญ๋๋ ํจํด์ ๊ตฌ๋ฌธ์ด ์คํ๋จ
- Swift์์ switch๋ฌธ์ break๋ฌธ์ด ํ์ ์์ (C์ ๋ค๋ฅธ์ )
switch some value to consider
{
case value 1:
respond to value 1
case value 2,
value 3:
respond to value 2 or 3
default:
otherwise, do something else
}"default: " - ํญ์ ์์ด์ผ ๋๋ค! ์์ case๊ฐ ์คํ๋์ง ์์ ๋ ์ฌ์ฉํจ
- ๊ฐ ์กฐ๊ฑด์ ์ํ๋ case ํค์๋๋ฅผ ํตํด ๋ํ๋ผ์ ์์.
- ๊ฐ case ์ํ ๋์๋ ์ฝ๋ก ( : )์ ์์ฑํด์ ๋งค์นญ ํจํด์ ์ข ๋ฃ.
- ์ฌ๋ฌ๊ฐ์ case๊ฐ ํ์ํ๋ฉด ์ฝค๋ง( , )๋ฅผ ํตํด ์ํ๋ฅผ ์ถ๊ฐ ํ ์ ์์.
- ๊ฐ case๋ ๋ถ๊ธฐ๋ก ์คํ๋๋ฉฐ ๋งค์นญ๋ ํด๋น case๋ฌธ์ด ์ข ๋ฃ๋๋ฉด switch ๋ฌธ์ ์ข ๋ฃ์ํด.
- ๊ฐ case์ ์ํ๋ switch ๋ฌธ์ value๊ฐ์ ๋งค์นญ๋ ์ง์ ์ ๊ฒฐ์ ํจ, ๋ชจ๋ value์ ๋ํด ๋งค์นญ ๋์ด์ผ ํจ. ๋ง์ฝ ๋งค์นญ๋๋ ๊ฐ์ด ์๋ค๋ฉด default ํค์๋๋ฅผ ํตํด ๊ธฐ๋ณธ ๋งค์นญ๊ฐ์ ์ค์ ํจ, defaultํค์๋๋ ๋ง์ง๋ง์ ๋ฐฐ์น๋จ.
func sampleSwitch(someCharacter:Character)
{
switch someCharacter {
case "a":
print("The first letter of the alphabet")
case "z":
print("The last letter of the alphabet")
default:
print("Some other character")
}
}- switch๋ฌธ์ ์ํ๋ ๋จ์ value ๋งค์นญ์ ๋์ด ์ข๋ ๋ค์ํ ํจํด์ ํตํด ๋งค์นญ์ด ๊ฐ๋ฅ
- interval matching์ ๋ฒ์ ์ฐ์ฐ์๋ฅผ ํตํด ํด๋น ๋ฒ์์ ํด๋นํ๋ value๋ฅผ ๋งค์นญ ์ํฌ ์ ์์
func interSwitch(count:Int)
{
let countedThings = "moons orbiting Saturn"
let naturalCount: String
switch count {
case 0:
naturalCount = "no"
case 1..<5:
naturalCount = "a few"
case 5..<12:
naturalCount = "several"
case 12..<100:
naturalCount = "dozens of"
case 100..<1000:
naturalCount = "hundreds of"
default:
naturalCount = "many"
}
print("There are \(naturalCount) \(countedThings).")
}
- ํํ์ ์ฌ์ฉํด์ ์ฌ๋ฌ๊ฐ์ value๋ฅผ ๋์์ ํ์ธ ํ ์ ์์
- ์ฌ์ฉ ๊ฐ๋ฅํ ๋ชจ๋ ๊ฐ์ ๋ํ ๋งค์นญ์ ์์ผ๋ ์นด๋(_)๋ฅผ ํตํด ๋งค์นญ ๊ฐ๋ฅ
func getPoint(somePoint:(Int,Int))
{
switch somePoint {
case (0, 0):
print("\(somePoint) is at the origin")
case (_, 0):
print("\(somePoint) is on the x-axis")
case (0, _):
print("\(somePoint) is on the y-axis")
case (-2...2, -2...2):
print("\(somePoint) is inside the box")
default:
print("\(somePoint) is outside of the box")
}
}_ : wildcard ๋ชจ๋ ์กฐ๊ฑด์ด ๋ค ๋จ
- case ๋ด๋ถ์์ ์ฌ์ฉ๋๋ ์์ ๊ฐ์ผ๋ก ๋งค์นญ ์ํฌ ์ ์์
func getPoint(somePoint:(Int,Int))
{
switch somePoint {
case (0, 0):
print("\(somePoint) is at the origin")
case (let x, 0):
print("on the x-axis with an x value of \(x)")
case (0, let y):
print("on the y-axis with an y value of \(y)")
case (-2...2, -2...2):
print("\(somePoint) is inside the box")
default:
print("\(somePoint) is outside of the box")
}
}- where ๋ฌธ์ ์ถ๊ฐ๋ก ์ถ๊ฐ ์กฐ๊ฑด์ ๋ฃ์ ์ ์์
func wherePoint(point:(int,Int))
{
switch point {
case let (x, y) where x == y:
print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == -y:
print("(\(x), \(y)) is on the line x == -y")
case let (x, y):
print("(\(x), \(y)) is just some arbitrary point")
}
}