로봇화/맥
[문법] 연산자 /와 % + if문과 swift의 차이
깨끗한눈빛
2019. 7. 8. 08:42
< /와 %의 차이 >
1) /: 몫
2) %: 나머지
- 언제: 배수 또는 짝/홀수 판별 시
- 사용: if문과 사용
< if문과 switch의 차이 >
1. 조건문 1개
var grade = "A"
/* if문 */
if grade == "A" {
print("A학점")
} else if grade == "B" {
print("B학점")
} else if grade == "C" {
print("C학점")
} else if grade == "D" {
print("D학점")
} else {
prind("FFFF")
}
/* switch문 */
switch grade {
case "A" :
print("A학점")
case "B" :
print("B학점")
case "C" :
print("B학점")
case "D" :
print("B학점")
default :
print("F학점")
}
2. 조건문 2개
차이점: || vs ,
/* if문 */
if grade == "A" || grade == "B" {
"PASS"
} else if grade == "C" || grade == "D" {
"재수강"
} else {
"FAIL"
}
/* switch문 */
switch grade {
case "A", "B" :
"PASS"
case "C", "D" :
"재수강"
default :
"FAIL"
}
3. 범위 조건문
차이점: &&, ..., ..<, ..>사용
var score = 88
/* if문 */
if score >= 90 && score <= 100 {
print("A학점")
} else if score >= 80 && score < 90 {
print("B학점")
} else if score >= 70 && score < 80 {
print("C학점")
} else if score >= 60 && score < 70 {
print("D학점")
} else {
print("F학점")
}
/* switch문 */
switch score {
case 90...100 :
print("A학점")
case 80..<90 :
print("B학점")
case 70..<80 :
print("C학점")
case 60..<70 :
print("D학점")
default :
print("F학점")
}