if문
if condition {
statements
}
condition == 조건 == Boolean
let id = “root”
let password = “1234qwer”
if id == “root” {
print(“valid id”)
}
if password == “1234qwer” {
print(“valid password”)
}
if id = “root” && password == “1234qwer” {
print(“go to admin page”)
}
if else문
if condition {
statements
} else {
statements
}
if id = “root” && password == “1234qwer” {
print(“go to admin page”)
} else {
print(“incorrect value”)
}
else if문
if - 항상 처음에. 하나만. 필수
else if - 두 블록 사이에. 하나 이상. 생략 가능
else - 항상 마지막에. 하나만. 생략 가능
if condition {
statements
} else if condition {
statements
} else {
statements
}
let num = 123
if num >= 0 {
print(“Positive number or zero”) // num이 0보다 크거나 같으면 프린트 출력
} else if num % 2 == 0 {
print(“positive even number”) // 위가 거짓이라면 2로 나눠 짝수인값이 나올때 프린트 출력
} else if num % 2 == 1 {
print(“positive odd number”) // 위도 거짓이라면 2로 나눠 홀수인 값이 나올때 프린트 출력
} else {
print(“negative number”) // 다 아니라면 프린트 출력
}
guard문
guard condition(=조건=bool) else {
statements
}
else 생략 블가능. 반드시 스코프 종료해야함(=리턴값 필요)
func validate(id: String) {
guard id.count >= 6 e;se {
}
print(“OK”)
}