5-1. if문
조건문이 참이라면 if문을 실행
let a = 20;
let b = 30;
if (a < b) { // true
document.write(“a 는 b 보다 작다”); // 실행
}
5-2. if ~ else문
조건문이 참이라면 if문 실행, 거짓이라면 else문 실행
let a = 40;
let b = 30;
if (a < b) { // false
document.write(“a 는 b 보다 작다”);
} else {
document.write(“a 는 b 보다 크다.”); // 실행
}
5-3. else if문
여러 개의 조건문을 생성할 때 사용하고, 참이 되는 부분에서 실행을 멈춘다.
let a = 40;
let b = 30;
let c = 20;
if (a < b) { // false
document.write(“a 는 b 보다 작다”);
} else if (a <= b) {
document.write(“참1”);
} else if (a == c) {
document.write(“참2”);
} else {
document.write(“모두 거짓”); // 실행
}
5-4. 중첩 if문
if문 안에 또 다른 if문을 넣을 때 사용하고, 바깥 if문이 참이어야 안의 if문이 실행된다.
let a = 10;
let b = 20;
if (a < b) {
if (a > b) {
document.write(“a는 b보다 크다.”);
} else {
document.write(“a는 b보다 작다.”)
}
} else { document.write(“ 가나다라마바사”); }