증감연산자란? 변수에 1을 더하거나 빼는 연산에 대한 축약
앞뿔뿔 뒤뿔뿔
++Y : 전위연산 (Y=Y+1)
--Y : 전위연산 (Y=Y-1)
Y++ : 후위연산 (Y=Y+1)
Y-- : 후위연산 (Y=Y-1)
전위연산과 후위연산 뭐가 다르지?
전위연산 : 먼저 변수에 1을 증가시킨 후 연산한다.
후위연산 : 연산수행 후 변수에 1을 증가시킨다.
03.html
<script type="text/javascript">
x=1
document.write(x++) //1
document.write(x++) //뿔뿔실행 2
document.write(x++)
document.write(x++)
document.write('<br/>')
x=1
document.write(++x) //먼저연산수행 2
document.write(++x)
document.write(++x)
document.write(++x)
</script>
💻 출력값
1234 :
2345 :
📌 부등호 공부하기
A == B (A와 B가 같다)
A != B (A와 B가 같지 않다)
A < B (A가 B보다 작다)
A <= B (A가 B보다 작거나 같다)
A > B (A가 B보다 크다)
A >= B (A가 B보다 크거나 같다)
A = B 대입
관계연산자란 ?
값이 두개이상 있어야함.
두개의 값을 비교해서 어떤 값을 출력해낸다.
04.html
<script type="text/javascript">
x=10
y=20
document.write(x+">="+y+":") //10>=20:
document.write(x>=y) //10은 20보다 크거나 같지 않으니 false가 출력
document.write('<br/>')
document.write(x+"<="+y+":")
document.write(x<=y) //10이 20보다 작거나 같으니 true
document.write('<br/>')
document.write(x+"!="+y+":")
document.write(x!=y) //같지않으면 true not청개구리 연산자
document.write('<br/>')
document.write(x+"=="+y+":")
document.write(x==y) //같으면 true, 다르면 false
document.write('<br/>')
document.write(x+"="+y+":")
document.write(x=y) //=는 대입
document.write('<br/>')
</script>
💻 출력값
10>=20:false
10<=20:true
10!=20:true
10==20:false
10=20:20
📌 논리연산자의 종류
앤드연산자 : A&&B (A와 B가 모두 True일 경우에만 True)
OR연산자 : A||B (A또는 B가 True일 경우 True)
NOT연산자 : !A (A가 True이면 False, False이면 True)
비트연산자 A^B (A와 B가 모두 True거나 False이면 True)
05.html
<script type="text/javascript">
x=(7>6)&&(1>8) //false 값 두개를 비교해서 둘중 하나가 다르면 false
document.write(x)
document.write('<br/>')
y=(7>6)||(1>8) //or연산자 둘중 하나가 참이니까 true
document.write(y)
document.write('<br/>')
z=!(7>6)
document.write(z) //not연산자 청개구리 값이 참이면 출력은 false
document.write('<br/>')
t=(7>6)^(1>8)// true false = false 숫자로 출력됨 /
document.write(t)
// ^ 비트연산자 true=0 , false=1
</script>
💻 출력값
false : 앤드연산자 둘다 참이아니여서 false
true : or연산자 둘중 하나가 참이여서 true
false : not연산자 값이 참이면 출력은 false
1 : 비트연산자 true=0, false=1
삼항연산자 : (조건)?A:B
<script type="text/javascript">
your="hard"
study=(your=="hard")?"you are very fool" : "you are very smart"
//참이면 A실행 거짓이면 B실행
document.write("If you feel " +your+'<br/>')
document.write(study)
document.write('<br/>')
your="easy"
study=(your=="easy")? "you are very smart":"you are very fool"
//참이면 A실행 거짓이면 B실행
document.write("If you feel " +your+'<br/>')
document.write(study)
</script>
💻 출력값
If you feel hard
you are very fool
If you feel easy
you are very smart
<script type="text/javascript">
x=10;y=20;
document.write(x+y/2)
document.write('<br/>')
document.write((x+y)/2)
</script>
📑
+나-보다 x와 /를 먼저 연산
✨괄호안에 있는 내용 먼저 연산 (20점으로 우선순위 제일 높다.)
💻 출력값
20
15
<script type="text/javascript">
your_sex="남자"
your_sex="여자"
document.write("당신은 "+your_sex+" 이므로")
if(your_sex=="남자") document.write(" 바지를 입으세요.")
if(your_sex=="여자") document.write(" 치마를 입으세요.")
</script>
💻 출력값
당신은 여자 이므로 치마를 입으세요.
✨ your_sex 변수에 두개의 값이 입력됨.
자바스크립트는 위에서부터 읽어내기때문에
여자값이 입력된다.
<script type="text/javascript">
weight=70
height=170
test=(height-100)*0.9
if(weight>test) document.write("당신은 비만입니다.")
else document.write("당신은 정상입니다.")
</script>
💻 출력값
당신은 비만입니다.
<script type="text/javascript">
score=74
document.write("당신이 취득한 점수는 "+score+"점이므로 ")
if(score>=90) document.write(" A 학점 입니다.")
else if(score>=80) document.write(" B 학점 입니다.")
else if(score>=70) document.write(" C 학점 입니다.")
else if(score>=60) document.write(" D 학점 입니다.")
else document.write(" F 학점 입니다.")
</script>
💻 출력값
당신이 취득한 점수는 74점이므로 C 학점 입니다.