본 수업은 폐지 예정입니다. 자바스크립트 언어 수업과 웹브라우저 자바스크립트로 수업이 리뉴얼 되었기 때문에 이것을 이용해주세요.
비교연산자(Comparison Operators)
- 조건문의 핵심
- 비교의 결과는 true나 false 둘 중의 하나
x = 5 일 때
연산자 | 설명 | 예 |
---|---|---|
== | 같다 | x==8 은 false |
=== | 정확하게 같다 (value and type) | x===5 은 true x==="5" 은 false |
!= | 다르다 | x!=8 은 true |
> | 크다 | x>8 은 false |
< | 작다 | x<8 은 true |
>= | 크거나 같다 | x>=8 은 false |
<= | 작거나 같다 | x<=8 은 true |
논리연산자(Logical Operators)
x = 6; y = 3;
연산자 | 설명 | 예 |
---|---|---|
&& (AND) |
둘다 참이어야 참 | (x<10 && y>1) -> true (x<10 && y<1) -> false (x>10 && y<1) -> false |
|| (OR) |
둘중의 하나 이상이 참이면 참 | (x == 6 || y == 3) -> true (x == 6 || y == 6) --> true (x == 5 || y ==5) -> false |
! (NOT) |
부정 | (x == y) -> false !(x == y) -> true |
조건문은
- 프로그래밍의 핵심!
- 프로그램을 지적으로 만들어줌
- if, if else, switch 문이 있음
- 비교연산자를 사용
if문
example1.html - (jsfiddle, github)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <!DOCTYPE html> < html > < head > < meta http-equiv = "Content-Type" content = "text/html;charset=utf-8" > < script > // 참 if (1 == 1) { alert('참'); } else { alert('거짓'); } // 거짓 if (1 == 2) { alert('참'); } else { alert('거짓'); } </ script > </ head > < body ></ body > </ html > |
switch문
example2.html - (jsfiddle, github)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <!DOCTYPE html> < html > < head > < meta http-equiv = "Content-Type" content = "text/html;charset=utf-8" > < script > var n = 5; switch(n) { case 1: alert('case 1'); break; case 2: alert('case 2'); break; case 5: alert('case 5'); break; default: alert('case default'); } </ script > </ head > < body ></ body > </ html > |
example3.html - 조건문의 실제적인 활용 (jsfiddle, github)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!DOCTYPE html> < html > < head > < meta http-equiv = "Content-Type" content = "text/html;charset=utf-8" > < script > var id = prompt('아이디를 입력해주세요. (coding)'); var password = prompt('비밀번호를 입력해주세요. (everybody)'); if(id == 'coding' && password == 'everybody'){ alert('딩동댕') } else { alert('다시 해주세요.') } </ script > </ head > < body > </ body > </ html > |