if(불 값이 나오는 표현식) {
불 값이 참일 때 실행할 문장
}
/
if(불 값이 나오는 표현식) {
불 값이 참일 때 실행할 문장
} else {
불 값이 거짓일 때 실행할 문장
}
/
if (불 값이 나오는 표현식 1) {
if (불 값이 나오는 표현식 2) {
표현식 2가 참일 때 실행할 문장
} else {
표현식 2가 거짓일 때 실행할 문장
}
} else {
if (불 값이 나오는 표현식 3) {
표현식 3이 참일 때 실행할 문장
} else {
표현식 3이 거짓일 때 실행할 문장
}
}
/
4. if else if 조건문
if(불 표현식) {
문장
} else if (불 표현식) {
문장
} else if (불 표현식) {
문장
} else {
문장
}
/
/
/
switch(자료) {
case 조건 A:
break
case 조건 B:
break
default:
break
}
불 표현식 ? 참일 때의 결과 : 거짓일 때의 결과
3-1. 논리합 연산자를 사용한 짧은 조건문
true|| 000
불 표현식 || 불 표현식이 거짓일 때 실행할 문장
3-2. 논리곱 연산자를 사용한 짧은 조건문
false && 000
결과가 거짓인 불 표현식 && 불 표현식이 참일 때 실행할 문장
기본 과제
p.139 Q3.문제
if (x>10) {
if(x<20) {
console.log('조건에 맞습니다.')
}
}
if(x>10 && x<20) {
console.log('조건에 맞습니다.')
}
정답
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const x = 15;
if(x>10 && x<20) {
console.log('조건에 맞습니다.')
} else {
console.log('조건에 맞지 않습니다.')
}
</script>
</body>
</html>
결과
추가 숙제
p.152의 <태어난 연도를 입력받아 띠 출력하기> 예제 실행
코드
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>외부 스타일 시트</title>
</head>
<body>
<script>
const rawInput = prompt('태어난 해를 입력해주세요', '');
const year = Number(rawInput); // 'Mumber' -> 'Number'로 수정
const e = year % 12;
let result;
if (e === 0) { result = '원숭이'; }
else if (e === 1) { result = '닭'; }
else if (e === 2) { result = '개'; }
else if (e === 3) { result = '돼지'; }
else if (e === 4) { result = '쥐'; }
else if (e === 5) { result = '소'; }
else if (e === 6) { result = '호랑이'; }
else if (e === 7) { result = '토끼'; }
else if (e === 8) { result = '용'; }
else if (e === 9) { result = '뱀'; }
else if (e === 10) { result = '말'; }
else if (e === 11) { result = '양'; }
alert(`${year}년에 태어났다면 ${result} 띠입니다.`);
</script>
</body>
</html>
결과