04_의사결정
의사결정 문장에 들어가면서
1) 의사결정 문장의 필요성 : 자바스크립트를 실행할 때 결정되는 조건이나 상황에 따라 어떤 작업을 해야 하는 지 프로그래밍을 해둘 필요가 있음.
2) 의사결정 문장의 종류 : if문, switch문
if문
1)
if( true/false 판별식) {
// 판별식이 true를 돌려주는 경우 if(){ } 안의 내용이
// 모두 실행
}else{ // 판별식이 false를 돌려주는 경우 else{ } 안의 내용이
// 모두 실행
}
2)
if( true/false 판별식) {
// 첫 번째 판별식이 true를 돌려주는 경우 if(){ } 안의 내용이
// 모두 실행
}else if( true/false 판별식) {
// 첫 번째 판별식이 false를 돌려주고
// else if( true/false 판별식)이 ture를 돌려 주면
// 이 블락 안의 모든 문장이 실행
}
3)
if( true/false 판별식) {
// else if는 계속 연결해서 쓸 수 있음
}else if( true/false 판별식) {
}else if( true/false 판별식) {
}else if( true/false 판별식) {
}else{
}
4)
if( true/false 판별식) {
// if안에 또 다른 if문을 쓸 수 있습니다.
if( true/false 판별식) {
}
}
switch 문
1)
switch (변수) {
case 일치 검사할 값/변수1 :
//
break ;
case 일치 검사할 값/변수2 :
//
break ;
case 일치 검사할 값/변수3 :
//
break ;
default :
//
}
실습예제
예제 04-1.html
if~else if 문장을 이용하여 다음 상황을 자바스크립트로 프로그래밍 해보겠습니다.
- 첫 번째 조건 : 점수(jumsu)가 90점 이상이면 A를 주고,
- 두 번째 조건 : 그렇지 않고, 점수(jumsu)가 80점 이상이면 B를 주고,
- 세 번째 조건 : 그렇지 않고, 점수(jumsu)가 70점 이상이면 C를 주고,
- 네 번째 조건 : 그렇지 않고, 점수(jumsu)가 60점 이상이면 D를 주고,
- 다섯 번째 조건 : 그 이하는 F를 준다.
.another_category {
border: 1px solid #E5E5E5;
padding: 10px 10px 5px;
margin: 10px 0;
clear: both;
}
.another_category h4 {
font-size: 12px !important;
margin: 0 !important;
border-bottom: 1px solid #E5E5E5 !important;
padding: 2px 0 6px !important;
}
.another_category h4 a {
font-weight: bold !important;
}
.another_category table {
table-layout: fixed;
border-collapse: collapse;
width: 100% !important;
margin-top: 10px !important;
}
* html .another_category table {
width: auto !important;
}
*:first-child + html .another_category table {
width: auto !important;
}
.another_category th, .another_category td {
padding: 0 0 4px !important;
}
.another_category th {
text-align: left;
font-size: 12px !important;
font-weight: normal;
word-break: break-all;
overflow: hidden;
line-height: 1.5;
}
.another_category td {
text-align: right;
width: 80px;
font-size: 11px;
}
.another_category th a {
font-weight: normal;
text-decoration: none;
border: none !important;
}
.another_category th a.current {
font-weight: bold;
text-decoration: none !important;
border-bottom: 1px solid !important;
}
.another_category th span {
font-weight: normal;
text-decoration: none;
font: 10px Tahoma, Sans-serif;
border: none !important;
}
.another_category_color_gray, .another_category_color_gray h4 {
border-color: #E5E5E5 !important;
}
.another_category_color_gray * {
color: #909090 !important;
}
.another_category_color_gray th a.current {
border-color: #909090 !important;
}
.another_category_color_gray h4, .another_category_color_gray h4 a {
color: #737373 !important;
}
.another_category_color_red, .another_category_color_red h4 {
border-color: #F6D4D3 !important;
}
.another_category_color_red * {
color: #E86869 !important;
}
.another_category_color_red th a.current {
border-color: #E86869 !important;
}
.another_category_color_red h4, .another_category_color_red h4 a {
color: #ED0908 !important;
}
.another_category_color_green, .another_category_color_green h4 {
border-color: #CCE7C8 !important;
}
.another_category_color_green * {
color: #64C05B !important;
}
.another_category_color_green th a.current {
border-color: #64C05B !important;
}
.another_category_color_green h4, .another_category_color_green h4 a {
color: #3EA731 !important;
}
.another_category_color_blue, .another_category_color_blue h4 {
border-color: #C8DAF2 !important;
}
.another_category_color_blue * {
color: #477FD6 !important;
}
.another_category_color_blue th a.current {
border-color: #477FD6 !important;
}
.another_category_color_blue h4, .another_category_color_blue h4 a {
color: #1960CA !important;
}
.another_category_color_violet, .another_category_color_violet h4 {
border-color: #E1CEEC !important;
}
.another_category_color_violet * {
color: #9D64C5 !important;
}
.another_category_color_violet th a.current {
border-color: #9D64C5 !important;
}
.another_category_color_violet h4, .another_category_color_violet h4 a {
color: #7E2CB5 !important;
}
\n\n"}}" data-ve-attributes="{"typeof":"mw:Extension/syntaxhighlight","about":"#mwt3"}">
<html>
<head>
<script type="text/javascript">
var jumsu=85;
if(jumsu>=90){
alert("A");
}else if(jumsu>=80){
alert("B");
}else if(jumsu>=70){
alert("C");
}else if(jumsu>=60){
alert("D");
}else{
alert("F");
}
</script>
</head>
</html>

예제 04-2.html
중첩 if문장을 이용하여 다음 상황을 자바스크립트로 작성해보겠습니다.
A: 이번주 로또에 당첨된다면..!!
- A-1: 상금이 1억이 넘으면 차를 사고 회사를 그만둔다.
- A-2: 상금이 1억이 넘지 않으면 저금을 한다.
B: 이번주 로또에 당첨되지 않으면 공부를 계속한다.
if (!window.T) { window.T = {} }
window.T.config = {"TOP_SSL_URL":"https://www.tistory.com","PREVIEW":false,"ROLE":"guest","PREV_PAGE":"","NEXT_PAGE":"","BLOG":{"id":2859442,"name":"hihellloitland","title":"27","isDormancy":false,"nickName":"hihelllo","status":"open","profileStatus":"normal"},"NEED_COMMENT_LOGIN":false,"COMMENT_LOGIN_CONFIRM_MESSAGE":"","LOGIN_URL":"https://www.tistory.com/auth/login/?redirectUrl=http://hihellloitland.tistory.com/35","DEFAULT_URL":"https://hihellloitland.tistory.com","USER":{"name":null,"homepage":null,"id":0,"profileImage":null},"SUBSCRIPTION":{"status":"none","isConnected":false,"isPending":false,"isWait":false,"isProcessing":false,"isNone":true},"IS_LOGIN":false,"HAS_BLOG":false,"IS_SUPPORT":false,"IS_SCRAPABLE":false,"TOP_URL":"http://www.tistory.com","JOIN_URL":"https://www.tistory.com/member/join","PHASE":"prod","ROLE_GROUP":"visitor"};
window.T.entryInfo = {"entryId":35,"isAuthor":false,"categoryId":766226,"categoryLabel":"프로그래밍/Javascript"};
window.appInfo = {"domain":"tistory.com","topUrl":"https://www.tistory.com","loginUrl":"https://www.tistory.com/auth/login","logoutUrl":"https://www.tistory.com/auth/logout"};
window.initData = {};
window.TistoryBlog = {
basePath: "",
url: "https://hihellloitland.tistory.com",
tistoryUrl: "https://hihellloitland.tistory.com",
manageUrl: "https://hihellloitland.tistory.com/manage",
token: "kNwqCspEbNjNNH3/UmW/RH7OwB5reuy+o6OLLaARBiq/X/OmlhuueWolHh7CyjIL"
};
var servicePath = "";
var blogURL = "";
\n \n
\n\n"}}" data-ve-attributes="{"typeof":"mw:Extension/syntaxhighlight","about":"#mwt3"}">
<html>
<head>
<script type="text/javascript">
var winlotto = true;
var prizeMoney = 5000000;
if(winlotto){
if(prizeMoney>100000000)
{
alert("차를 사고 회사를 그만둔다");
}else{
alert("저금을 한다.");
}
}else{
alert("공부를 계속한다.");
}
</script>
</head>
</html>

예제 04-3.html
switch 문장에 대한 예제입니다.
다음 상황을 자바스크립트로 프로그래밍해 보겠습니다.
- 해미 선물은 뿡뿡이
- 순돌이 선물은 과자
- 준희 선물은 트랜스포머
- 철수 선물은 곰 인형
if (!window.T) { window.T = {} }
window.T.config = {"TOP_SSL_URL":"https://www.tistory.com","PREVIEW":false,"ROLE":"guest","PREV_PAGE":"","NEXT_PAGE":"","BLOG":{"id":2859442,"name":"hihellloitland","title":"27","isDormancy":false,"nickName":"hihelllo","status":"open","profileStatus":"normal"},"NEED_COMMENT_LOGIN":false,"COMMENT_LOGIN_CONFIRM_MESSAGE":"","LOGIN_URL":"https://www.tistory.com/auth/login/?redirectUrl=http://hihellloitland.tistory.com/35","DEFAULT_URL":"https://hihellloitland.tistory.com","USER":{"name":null,"homepage":null,"id":0,"profileImage":null},"SUBSCRIPTION":{"status":"none","isConnected":false,"isPending":false,"isWait":false,"isProcessing":false,"isNone":true},"IS_LOGIN":false,"HAS_BLOG":false,"IS_SUPPORT":false,"IS_SCRAPABLE":false,"TOP_URL":"http://www.tistory.com","JOIN_URL":"https://www.tistory.com/member/join","PHASE":"prod","ROLE_GROUP":"visitor"};
window.T.entryInfo = {"entryId":35,"isAuthor":false,"categoryId":766226,"categoryLabel":"프로그래밍/Javascript"};
window.appInfo = {"domain":"tistory.com","topUrl":"https://www.tistory.com","loginUrl":"https://www.tistory.com/auth/login","logoutUrl":"https://www.tistory.com/auth/logout"};
window.initData = {};
window.TistoryBlog = {
basePath: "",
url: "https://hihellloitland.tistory.com",
tistoryUrl: "https://hihellloitland.tistory.com",
manageUrl: "https://hihellloitland.tistory.com/manage",
token: "kNwqCspEbNjNNH3/UmW/RH7OwB5reuy+o6OLLaARBiq/X/OmlhuueWolHh7CyjIL"
};
var servicePath = "";
var blogURL = "";
\n \n
\n\n"}}" data-ve-attributes="{"typeof":"mw:Extension/syntaxhighlight","about":"#mwt3"}">
<html>
<head>
<script type="text/javascript">
var name="순돌";
var present;
switch(name){
case "준희" :
present="트랜스포머";
break;
case "순돌" :
present="과자"
break;
case "순돌" :
present="곰 인형"
break;
case "순돌" :
present="뿡뿡이"
break;
}
alert(present);
</script>
</head>
</html>

'프로그래밍 > Javascript' 카테고리의 다른 글
06_배열 (0) | 2018.02.27 |
---|---|
05_반복 (0) | 2018.02.27 |
03_연산자 (0) | 2018.02.27 |
02_데이터타입 (0) | 2018.02.27 |
01_자바스크립트 개요 (0) | 2018.02.27 |