回圈樣式
* for( ① ; ② ; ③ ){ * ④ * } * * ①:定義變數 * ②:判斷條件(條件為真,繼續回圈,為假則退出回圈) * ③:變數的變化 * * ④:若干行回圈代碼 * * 1-2-4-3-2-4-3- …… -2-4-3-2(為假,回圈結束) * */ for( let i=0; i<10 ; i++ ){ console.log("這里寫回圈的代碼"); } console.log("回圈結束了");
注:避免死回圈,條件為真時一直回圈,無法跳出回圈
for回圈中的: break, continue
break:回圈體代碼只要執行了break,回圈就會終止,并跳出回圈體代碼
continue: 回圈體代碼只要執行了continue,當前這一次回圈體代碼不再往后執行,直接進入下一次回圈,跳過回圈體代碼
/*for (let i=0;i<10;i++){ //回圈體代碼只要執行了break,回圈就會終止,并跳出回圈體代碼 if (i === 6){ break; } console.log(i,"回圈體代碼"); }*/ for (let i=0;i<10;i++){ //回圈體代碼只要執行了continue,當前這一次回圈體代碼不再往后執行,直接進入下一次回圈,跳過回圈體代碼 if (i === 6){ continue; } console.log(i,"回圈體代碼"); } console.log("結束了");
for回圈的改寫while, do while
while:先判斷在回圈 do while: 先回圈在判斷
//while先判斷在回圈 let i=9; while (i<5){ console.log(i,"回圈體代碼"); i ++; }*/ //do while 先回圈在判斷 let i=9; do{ console.log(i,"回圈體代碼"); i ++; }while(i<5);
雙層回圈
for (let i=0;i<4;i++){ for (let j=0;j<6;j++){ console.log(i,j,"內層回圈"); } // console.log("外層回圈"); }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/244188.html
標籤:其他
上一篇:JavaScript設計模式
