Javascript流程式控制制 1.條件語句 (1)if(exp)執行一句代碼 (2)if(exp){執行代碼段;} (3)if(exp){exp為true執行代碼段}else{exp為false執行的代碼段} (4)if...else if... (5)if嵌套 2.迴圈語句 (1)for 迴圈 ( ...
Javascript流程式控制制
1.條件語句
(1)if(exp)執行一句代碼
(2)if(exp){執行代碼段;}
(3)if(exp){exp為true執行代碼段}else{exp為false執行的代碼段}
(4)if...else if...
(5)if嵌套
2.迴圈語句
(1)for 迴圈
(2)while 迴圈
(3)do/while迴圈
3.特殊迴圈控制
(1)break 終止迴圈
(2)continue 跳過迴圈
下麵就是具體詳情:
for迴圈
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <h3>for迴圈</h3> 9 <p>for(exp1;exp2;exp3;){迴圈體;}</p> 10 <p>exp1:無條件執行第一個語句</p> 11 <p>exp2:判斷是歐服可以執行迴圈體的條件</p> 12 <p>exp3:做增量的操作</p> 13 <script> 14 for(var i=0;i<4;i++){ 15 document.write(i+'hello world <br />') 16 } 17 // 迴圈i++,i迴圈三次,每次輸出第幾次+hello world 18 </script> 19 <p>通過break結束迴圈</p> 20 <script> 21 for(var i=0;i<=6;i++){ 22 if(i>5){ 23 break; 24 } 25 document.write(i+'<br/>'); 26 }//迴圈for語句每次輸出1個i值,當i>5時停止輸出 27 </script> 28 <p>通過continue跳過檔次迴圈</p> 29 <script> 30 for(var i=1;i<=6;i++){ 31 if(i==5){ 32 continue; 33 } 34 document.write(i+'<br />') 35 }//迴圈for語句,每次輸出1個i值,的那個i=5時跳過本次迴圈進入下次迴圈。 36 </script> 37 </body> 38 </html>
for迴圈嵌套
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <h3>for迴圈嵌套</h3> 9 <script> 10 for(var i=1;i<=3;i++){ 11 for (var k=1;k<=2;k++){ 12 document.write(k); 13 } 14 document.write(i+'<br>'); 15 } 16 </script> 17 </body> 18 </html>
條件語句
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <h3>條件語句</h3> 9 <p>假如3>=11 輸出a;否則輸出b</p> 10 <script> 11 if(3>=11){ 12 document.write('a'); 13 }else{ 14 document.write('b'); 15 } 16 </script> 17 <p>定義a=1,判斷a是否全等於2,若全等則輸出2;若不全等則判斷a是否全等於3,若全等則輸出3;若不全等則判斷a是否全等於1,若全等則輸出1</p> 18 <script> 19 var a=1; 20 if(a==2){ 21 document.write(2); 22 }else if(a==3){ 23 document.write(3); 24 }else if(a==1){ 25 document.write(1); 26 } 27 </script> 28 </body> 29 </html>
switch迴圈
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <h3>switch迴圈</h3> 9 <p>switch迴圈若是沒有找到指定值則一直比較,當找到指定值是執行後邊所有代碼,所以在swith迴圈中每個值的代碼段最後要寫break</p> 10 <p>沒加break前</p> 11 <script> 12 var i=3; 13 switch(i){ 14 case 1:document.write('a<br />'); 15 case 2:document.write('b<br />'); 16 case 3:document.write('c<br />'); 17 case 4:document.write('d<br />'); 18 }//因為我們在這裡沒有加入break所以說當找到i=3時後執行後邊所有代碼 19 </script> 20 <p>加break後</p> 21 <script> 22 switch(i){ 23 case 1:document.write('a<br />');break; 24 case 2:document.write('b<br />');break; 25 case 3:document.write('c<br />');break; 26 case 4:document.write('d<br />');break; 27 } 28 </script> 29 </body> 30 </html>
while迴圈
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <h3>while迴圈</h3> 9 <script> 10 var i=0; 11 while(i<=5){ 12 i++; 13 document.write(i+'<br />'); 14 }//若i<5則執行i++並輸出i值 15 </script> 16 </body> 17 </html>
do/while迴圈
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <body> 8 <h3>do/while迴圈</h3> 9 <script> 10 var x=0; 11 do{ 12 x++; 13 document.write('第'+x+'次X的值為:'+x+'<br>') 14 } 15 while (x<4); 16 document.write('最終X的值為:'+x+'<br />') 17 //當x<4時執行每次輸出第幾次的x的值, 18 </script> 19 </body> 20 </html>