欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解JavaScript中循環(huán)控制語句的用法

 更新時(shí)間:2015年06月03日 12:26:27   投稿:goldensun  
這篇文章主要介紹了詳解JavaScript中循環(huán)控制語句的用法,包括break語句和continue語句的使用方法,需要的朋友可以參考下

 JavaScript提供完全控制來處理循環(huán)和switch語句??赡苡幸环N情況,當(dāng)你需要退出一個(gè)循環(huán),但未達(dá)到其底部。也可能有一種情況,當(dāng)要跳過的碼塊的一部分,并直接開始下一個(gè)迭代。

為了處理這些情況下,JavaScript提供了break和continue語句。這些語句是用來馬上退出任何循環(huán)或啟動(dòng)循環(huán)的下一次迭代。
break 語句:

break語句,這是簡單地用switch語句介紹,用于提前退出循環(huán),打破封閉的花括號。
例子:

這個(gè)例子說明了如何使用break語句同while循環(huán)。請注意循環(huán)打破了初期由x到5,document.write(..) 語句的正下方,以右大括號:

<script type="text/javascript">
<!--
var x = 1;
document.write("Entering the loop<br /> ");
while (x < 20)
{
 if (x == 5){ 
   break; // breaks out of loop completely
 }
 x = x + 1;
 document.write( x + "<br />");
}
document.write("Exiting the loop!<br /> ");
//-->
</script>

這將產(chǎn)生以下結(jié)果:

Entering the loop
2
3
4
5
Exiting the loop!

我們已經(jīng)看到break語句在switch語句中使用。
continue 語句:

continue語句告訴解釋器立即啟動(dòng)循環(huán)的下一次迭代,并跳過其余的代碼塊。

當(dāng)遇到continue語句,程序流程將立即轉(zhuǎn)移到循環(huán)檢查表達(dá)式,如果條件保持真,那么就開始下一個(gè)迭代,否則控制退出循環(huán)。
例子:

這個(gè)例子說明使用continue語句同while循環(huán)。請注意continue語句用于跳過打印時(shí)指數(shù)變量x到達(dá)5:

<script type="text/javascript">
<!--
var x = 1;
document.write("Entering the loop<br /> ");
while (x < 10)
{
 x = x + 1;
 if (x == 5){ 
   continue; // skill rest of the loop body
 }
 document.write( x + "<br />");
}
document.write("Exiting the loop!<br /> ");
//-->
</script>

這將產(chǎn)生以下結(jié)果:

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

 
使用標(biāo)簽來控制流程:

從JavaScript1.2開始,標(biāo)簽可以與break及continue使用,繼續(xù)更精確地控制流程。

標(biāo)簽是簡單的標(biāo)識符隨后被施加到一個(gè)語句或代碼塊冒號??吹絻蓚€(gè)不同的例子來了解標(biāo)簽使用突破,并繼續(xù)。

注:換行符是不是繼續(xù)還是分手聲明,其標(biāo)簽名稱之間允許的。此外,不應(yīng)該有一個(gè)標(biāo)簽名稱和相關(guān)聯(lián)的回路之間的任何其它聲明。
實(shí)例1:

<script type="text/javascript">
<!--
document.write("Entering the loop!<br /> ");
outerloop:  // This is the label name
for (var i = 0; i < 5; i++)
{
 document.write("Outerloop: " + i + "<br />");
 innerloop:
 for (var j = 0; j < 5; j++)
 {
   if (j > 3 ) break ;     // Quit the innermost loop
   if (i == 2) break innerloop; // Do the same thing
   if (i == 4) break outerloop; // Quit the outer loop
   document.write("Innerloop: " + j + " <br />");
  }
}
document.write("Exiting the loop!<br /> ");
//-->
</script>

這將產(chǎn)生以下結(jié)果:

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!

 
實(shí)例2:

<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>

這將產(chǎn)生以下結(jié)果:

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!

相關(guān)文章

最新評論