05_반복
반복 문장에 들어가면서
1) 반복 문장의 필요성 : 같은 작업을 여러 번 반복해야 하는 경우에 반복을 표현하는 문장으로 간편하게 프로그래밍할 수 있음.
2) 반복 문장의 종류 : for문, while문
for문
for( 초기식 ; true/false 판별식 ; 증감식 )
{
//반복해야 하는 문장들
}
for( 초기식 ; true/false 판별식 ; 증감식 )
{
//반복해야 하는 문장들
}
while문
1) while문
while(true/false 판별식)
{
//반복해야 하는 문장들
}
2) do while문
do
{
//반복해야 하는 문장들
} while(true/false 판별식)
break/ continue문
1) break : 반복문을 탈출하는 역할
2) continue문 : 현재 반복문을 중지하고 새로운 반복 문을 시작하는 역할
실습예제
예제 05-1.html
for문장과 if문장을 이용하여 10이 소수인지 아닌지를 구하는 자바스크립트를 작성해보겠습니다.
.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 num = 10;
var cnt = 0;
for(var i =2; i<=num; i++){
if((num%i)==0)cnt++
}
if(cnt==1) alert("소수입니다.");
else if("소수가 아닙니다.");
</script>
</head>
</html>
예제 05-2.html
중첩 for문장과 if문장을 이용하여 2부터 10까지 수 중 소수를 모두 찾아 출력하는 자바스크립트를 작성해보겠습니다.
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=https://hihellloitland.tistory.com/36","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":36,"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: "IM9mnuRP2Q6G1Dehrk071DecnRyoT7tHBalwh8F+MS7JjVGV2oHhZMG3IQ9qx56l"
};
var servicePath = "";
var blogURL = "";
\n \n
\n\n"}}" data-ve-attributes="{"typeof":"mw:Extension/syntaxhighlight","about":"#mwt3"}">
<html>
<head>
<script type="text/javascript">
var cnt = 0;
for(var num=2; num<=10; num++)
{
for(var i=2; i<=num; i++)
{
if((num%i)==0) cnt++;
}
if(cnt==1) document.write(num+"은 소수입니다.<br>");
else document.write(num + "은 소수가 아닙니다.<br>");
cnt=0;
}
</script>
</head>
</html>