자, 오늘은 컨트롤 플로우 문법에 대해서 알아보도록 하겠습니다.
컨트롤 플로우 문법이라함은 if와 같은 조건문과 for, while과 같은 루프문을 말합니다.
우선 if...else 문에 대해서 보도록 하죠.
<%! int day = 3; %>
<html>
<head><title>IF...ELSE Example</title></head>
<body>
<% if (day == 1 | day == 7) { %>
<p> Today is weekend</p>
<% } else { %>
<p> Today is not weekend</p>
<% } %>
</body>
</html>
결과는 아래과 같습니다.
Today is not weekend
왜 이렇게 나오는건지 한줄 한줄 살펴보겠습니다.
<%! int day = 3; %>
day라는 int 타입 변수에 3이라는 값을 할당했습니다.
그리고 body 태그 내에서 if-else 문을 이용하였는데 day 값이 1 (일요일) 이거나 7 (토요일)이면 "Today is weekend"를 출력했겠지만, 우리는 day 값에 3을 할당해주었으니 "Today is not weekend"가 출력되는 것입니다. 이때 주의할 점은 if-else 문 내에서 출력하고자 하는 태그 부분은 <% %> 안에 감싸지 않았다는 점입니다.
이제 switch - case 문에 대해서 알아보죠.
<%! int day = 3; %>
<html>
<head><title>SWITCH...CASE Example</title></head>
<body>
<%
switch(day) {
case 0:
out.println("It\'s Sunday.");
break;
case 1:
out.println("It\'s Monday.");
break;
case 2:
out.println("It\'s Tuesday.");
break;
case 3:
out.println("It\'s Wednesday.");
break;
case 4:
out.println("It\'s Thursday.");
break;
case 5:
out.println("It\'s Friday.");
break;
default:
out.println("It's Saturday.");
}
%>
</body>
</html>
역시 결과는 아래와 같습니다.
It's Wednesday.
이번에는 Loop 문법에 대해서 알아보도록 하겠습니다.
<%! int fontSize; %>
<html>
<head><title>FOR LOOP Example</title></head>
<body>
<%for ( fontSize = 1; fontSize <= 3; fontSize++){ %>
<p style="font-size:<%= fontSize %>rem"> JSP Tutorial </p> <%}%>
</body>
</html>
결과는 아래와 같습니다.
JSP Tutorial
JSP Tutorial
JSP Tutorial
3번에서 for 루프로 구현한 것을 이번에는 while루프를 이용해서 구현해보도록 하겠습니다.
<%! int fontSize; %>
<html>
<head><title>WHILE LOOP Example</title></head>
<body>
<%while ( fontSize <= 3){ %>
<p style="font-size:<%= fontSize %>rem"> JSP Tutorial </p>
<%fontSize++;%>
<%}%>
</body>
</html>
결과는 아래와 같습니다.
JSP Tutorial
JSP Tutorial
JSP Tutorial
JSP Tutorial
자 이제 어느정도 감이 잡히시죠?
그냥 자바 문법을 그대로 옮겨놓기만 하는 겁니다.
JSP 정말 별거 아니네요~ ㅎㅎ
while 루프를 이용한 것을 이번에는 do - while을 이용해서 한번 해보세요~ 숙제입니다~ ㅋㅋ
Reference : http://www.tutorialspoint.com/jsp/jsp_quick_guide.htm
[JSP] Custom Tags ( 커스텀, 사용자 정의 태그 ) (0) | 2019.02.15 |
---|---|
[JSP] Directives ( 디렉티브 ) (0) | 2019.02.15 |
[JSP] JSP 생명 주기 ( life cycle ) (0) | 2019.02.15 |
[JSP] JSP 시작하기 (0) | 2019.02.15 |
[JSP] JSP란? (0) | 2019.02.15 |
자~ 오늘은 for 루프에 대해서 알아보도록 하겠습니다.
for 루프와 while루프는 100% 바꿔치기가 가능한 녀석들입니다. 어떤 상황에서 어떤 반복문을 사용하느냐는 개발자인 여러분들에게 달려있습니다!!
우선 문법부터 확인해야겠죠? 어떻게 생긴놈인지 알아야 뭘 써먹더라도 써먹죠 ㅋㅋ
for ( 초기화 ; 조건문 ; 반복실행문 ){ 조건문이 참인 동안 실행되어야 할 문장 } |
<script type="text/javascript"> <!-- var count; document.write("Starting Loop" + "<br />"); for(count = 0; count < 10; count++){ document.write("Current Count : " + count ); document.write("<br />"); } document.write("Loop stopped!"); //--> </script> |
자 위 코드를 보면 어떤 결과가 나올거라고 예상이 되시나요??
아래 해답을 보시죠.
Starting Loop Current Count : 0 Current Count : 1 Current Count : 2 Current Count : 3 Current Count : 4 Current Count : 5 Current Count : 6 Current Count : 7 Current Count : 8 Current Count : 9 Loop stopped! |
어때요? 쉽죠? 어려우시다구요? 그럼 설명을 좀 해드릴게요.
for ( count = 0 ; count < 10 ; count++ ) : for루프를 시작하는 부분입니다. 변수 count를 0으로 초기화 합니다. count변수의 값이 10보다 작을 동안 실행하라는 조건을 줬습니다. 그리고나서 매 루프가 끝나면 count를 1씩 증가시키도록 합니다.
여기서 세미콜론 ( ; ) 잊어먹지 않도록 조심하세요.
문법이 어렵다면 연습만이 당신을 살릴 수 있으니 연습하세요. ㅋㅋ
자, 또 다른 for 루프가 있습니다. 객체를 이용하는 것인데요~ 형식을 한번 볼까요?
for (변수명 in 객체){ 실행할 문장 } |
아래 예제는 웹브라우저의 Navigator객체에 있는 속성을 출력하는 기능을 for루프로 구현해놓은 것입니다.
<script type="text/javascript"> <!-- var aProperty; document.write("Navigator Object Properties<br /> "); for (aProperty in navigator) { document.write(aProperty); document.write("<br />"); } document.write("Exiting from the loop!"); //--> </script> |
결과는 아래처럼 나오겠죠? 아닌가요? 맞나? 확인해보세요~~ ^___^
Navigator Object Properties appCodeName appName appMinorVersion cpuClass platform plugins opsProfile userProfile systemLanguage userLanguage appVersion userAgent onLine cookieEnabled mimeTypes Exiting from the loop! |
Reference : http://www.tutorialspoint.com/javascript/javascript_for_loop.htm
[Javascript / 자바스크립트] 강좌 #10 - Functions ( 함수 ) (0) | 2015.08.05 |
---|---|
[Javascript / 자바스크립트] 강좌 #9 - break, continue, label 사용하기 (0) | 2015.08.05 |
[Javascript / 자바스크립트] 강좌 #7 - while loop ( while 루프 ) (0) | 2015.08.05 |
[Javascript / 자바스크립트] 강좌 #6 - Switch - Case 문 (0) | 2015.08.05 |
[Javascript / 자바스크립트] 강좌 #5 - IF, IF-ELSE, IF-ELSE IF-ELSE 문 (0) | 2015.08.05 |
이번 포스팅에서는 자바스크립트의 while 루프문에 대해서 알아보도록 하겠습니다.
우선 문법부터 확인해볼까요??
while (조건문){ 조건문이 참인동안 실행해야할 문장 } |
이제 예제를 한번 볼까요??
<script type="text/javascript"> <!-- var count = 0; document.write("Starting Loop" + "<br />"); while (count < 10){ document.write("Current Count : " + count + "<br />"); count++; } document.write("Loop stopped!"); //--> </script> |
결과가 어떻게 나올것 같나요??
아래처럼 나와야겠죠??
Starting Loop Current Count : 0 Current Count : 1 Current Count : 2 Current Count : 3 Current Count : 4 Current Count : 5 Current Count : 6 Current Count : 7 Current Count : 8 Current Count : 9 Loop stopped! |
자 while문과 비슷한 do-while문을 아시나요? do-while문은 일단 먼저 한번 실행시킨 다음에 while문을 타는겁니다.
역시 말보다는 실습이죠?
문법부터 확인해볼까요??
do{ 최소 한번은 실행되어야 하는 문장 } while (조건문); |
<script type="text/javascript"> <!-- var count = 0; document.write("Starting Loop" + "<br />"); do{ document.write("Current Count : " + count + "<br />"); count++; }while (count < 0); document.write("Loop stopped!"); //--> </script> |
결과가 예상이 되나요?
아래처럼 결과를 예상하셨다면 당은은 자바스크립트의 중수가 되신겁니다~ 축하드려용~~ㅋㅋ
Starting Loop Current Count : 0 Loop stopped! |
간단하죠??
별거 없습니다 ㅎㅎ
Reference : http://www.tutorialspoint.com/javascript/javascript_while_loop.htm
[Javascript / 자바스크립트] 강좌 #9 - break, continue, label 사용하기 (0) | 2015.08.05 |
---|---|
[Javascript / 자바스크립트] 강좌 #8 - for loop ( for 루프 ) (0) | 2015.08.05 |
[Javascript / 자바스크립트] 강좌 #6 - Switch - Case 문 (0) | 2015.08.05 |
[Javascript / 자바스크립트] 강좌 #5 - IF, IF-ELSE, IF-ELSE IF-ELSE 문 (0) | 2015.08.05 |
[Javascript / 자바스크립트] 강좌 #4 - DataType ( 데이타 타입, 변수 ) (0) | 2015.08.05 |