์˜ค๋Š˜์€ break๋ฌธ๊ณผ continue๋ฌธ, ๊ทธ๋ฆฌ๊ณ  label์„ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”์ง€ ํ•œ๋ฒˆ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์—ญ์‹œ๋‚˜ ์˜ˆ์ œ๊ฐ€ ์šฐ๋ฆฌ์—๊ฒ ์ตœ๊ณ ์˜ ์Šค์Šน์ด์ฃ ? ใ…‹

 

<script type="text/javascript">
<!--
var x = 1;
document.write("Entering the loop<br /> ");
while (x < 20)
{
  if (x == 5){ 
     break;  // while๋ฃจํ”„์—์„œ ๋น ์ ธ๋‚˜๊ฐ‘๋‹ˆ๋‹ค
  }
  x = x + 1;
  document.write( x + "<br />");
}
document.write("Exiting the loop!<br /> ");
//-->
</script>

 

x๊ฐ€ 5์ผ ๋•Œ break;๋ฌธ์ด ์‹คํ–‰๋˜๋ฉด์„œ ๋ฃจํ”„์—์„œ ๋น ์ ธ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์˜ค๊ฒŒ ๋˜๊ฒ ์ฃ . 

Entering the loop
2
3
4
5
Exiting the loop!

 

 

 

 continue ๋ฌธ์€ ๊ทธ๋Ÿผ ๋ญ˜๊นŒ์š”? ์™ ์ง€ break๋ฌธ์˜ ๋ฐ˜๋Œ€๊ฐ™์€๋ฐ ๋ง์ด์ฃ . continue๋ฌธ์€ ๋‹ค์Œ ๋ฃจํ”„๋กœ ๋„˜์–ด๊ฐ€๋ผ๋Š” ๋ช…๋ น์ž…๋‹ˆ๋‹ค. ์•„์ง ํ˜„์žฌ ๋ฃจํ”„์—์„œ ์‹คํ–‰๋˜์–ด์•ผ ํ•  ๋ฌธ์žฅ๋“ค์ด ๋‚จ์•„์žˆ๋”๋ผ๋„ ๋ง์ด์ฃ .

์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ๋ณผ๊นŒ์š”?

 

 

<script type="text/javascript">
<!--
var x = 1;
document.write("Entering the loop<br /> ");
while (x < 10)
{
  x = x + 1;
  if (x == 5){ 
     continue;  // x == 5 ์ด๋ฉด x๋ฅผ ์ถœ๋ ฅํ•˜์ง€์•Š๊ณ  ๋‹ค์Œ ๋ฃจํ”„๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค. 
  }
  document.write( x + "<br />");
}
document.write("Exiting the loop!<br /> ");
//-->
</script>

 

๊ฒฐ๊ณผ๋ฅผ ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜ค๊ฒ ์ฃ . x๊ฐ€ 5์ผ๋•Œ๋Š” continue๋ฌธ ๋•Œ๋ฌธ์— document.write( x + "<br />"); ๋ฌธ์ด ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ 2๋ถ€ํ„ฐ 10๊นŒ์ง€ ์ถœ๋ ฅํ•˜๋Š”๋ฐ 5๋งŒ ๋นผ๊ณ  ์ถœ๋ ฅ์„ ํ•˜๊ฒŒ๋˜๊ฒ ์ฃ . 

Entering the loop
2
3
4
6
7
8
9
10
Exiting the loop!

 

 

Labels

JavaScript 1.2 ๋ฒ„์ „๋ถ€ํ„ฐ label์„ beak, continue๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋์Šต๋‹ˆ๋‹ค.  

 label ์€ ๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด ํŠน์ • ์œ„์น˜๋ฅผ ์ง€์ •ํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.   

์˜ˆ์ œ๋ฅผ ํ•œ๋ฒˆ ์‚ดํŽด๋ณด๋„๋ก ํ•ฉ์‹œ๋‹ค. 

Example 1 : break๋ฌธ๊ณผ ํ•จ๊ป˜ label์‚ฌ์šฉํ•˜๊ธฐ

<script type="text/javascript"> <!-- document.write("Entering the loop!<br /> "); outerloop: // 1๋ฒˆ ๋ผ๋ฒจ์ž…๋‹ˆ๋‹ค. for (var i = 0; i < 5; i++) // 1๋ฒˆ ๋ฃจํ”„ { document.write("Outerloop: " + i + "<br />"); innerloop: // 2๋ฒˆ ๋ผ๋ฒจ์ž…๋‹ˆ๋‹ค. for (var j = 0; j < 5; j++) // 2๋ฒˆ ๋ฃจํ”„ { if (j > 3 ) break ; // ์ด ๋ฌธ์žฅ์„ ํฌํ•จํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ๋ฃจํ”„๋ฅผ ๋น ์ ธ๋‚˜์˜ต๋‹ˆ๋‹ค. (์—ฌ๊ธฐ์„œ๋Š” 2๋ฒˆ ๋ฃจํ”„๊ฒ ์ฃ ) if (i == 2) break innerloop; // ์ด ๋ฌธ์žฅ์€ innerloop๋ฅผ ๋น ์ ธ๋‚˜์™€๋Š” ๋ง์ž…๋‹ˆ๋‹ค. ( 2๋ฒˆ ๋ฃจํ”„์—์„œ ๋‚˜์˜ค๋Š”๊ฑฐ์ฃ  ) if (i == 4) break outerloop; // ์ด ๋ฌธ์žฅ์€ outerloop๋ฅผ ๋น ์ ธ๋‚˜์˜ต๋‹ˆ๋‹ค. ( 1๋ฒˆ ๋ฃจํ”„์—์„œ ๋น ์ ธ๋‚˜๊ฐ€๋Š” ๊ฒƒ์ด์ฃ  ) document.write("Innerloop: " + j + " <br />"); } } document.write("Exiting the loop!<br /> "); //--> </script>

 

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

Entering the loop!
Outerloop: 0
Innerloop: 0 
Innerloop: 1 
Innerloop: 2 
Innerloop: 3 
Outerloop: 1
Innerloop: 0 
Innerloop: 1 
Innerloop: 2 
Innerloop: 3 
Outerloop: 2
Outerloop: 3
Innerloop: 0 
Innerloop: 1 
Innerloop: 2 
Innerloop: 3 
Outerloop: 4
Exiting the loop!

 

Example 2 : continue๋ฌธ๊ณผ ํ•จ๊ป˜ label์‚ฌ์šฉํ•˜๊ธฐ

<script type="text/javascript">
<!--
document.write("Entering the loop!<br /> ");
outerloop:   // This is the label name
for (var i = 0; i < 3; i++)
{
   document.write("Outerloop: " + i + "<br />");
   for (var j = 0; j < 5; j++)
   {
      if (j == 3){
         continue outerloop;
      }
      document.write("Innerloop: " + j + "<br />");
   } 
}
document.write("Exiting the loop!<br /> ");
//-->
</script>

 

๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜์˜ต๋‹ˆ๋‹ค. 

Entering the loop!
Outerloop: 0
Innerloop: 0
Innerloop: 1
Innerloop: 2
Outerloop: 1
Innerloop: 0
Innerloop: 1
Innerloop: 2
Outerloop: 2
Innerloop: 0
Innerloop: 1
Innerloop: 2
Exiting the loop!

 

 

 

Reference : http://www.tutorialspoint.com/javascript/javascript_loop_control.htm