一、while語句
While循環(huán),先進(jìn)行條件判斷,再執(zhí)行循環(huán)體的代碼
while (條件表達(dá)式){
循環(huán)體
}
如果條件不滿足,則不會執(zhí)行循環(huán)體,一次都不會
案例:
var i= 1;
while(i<=10){
console.log(i);
i++;
}
二、do.....while循環(huán)
do while循環(huán),先執(zhí)行循環(huán)體代碼,再進(jìn)行條件判斷。至少執(zhí)行一次循環(huán)體的代碼。
代碼塊會至少先執(zhí)行一次
do {
代碼段
} while (條件表達(dá)式)
案例:
var i=1;
do{
i++; // 2
console.log(i);
}while(i<=10)
三、for .....in?
for (變量 in 對象) {
代碼段
}
在無法預(yù)知對象的任何信息,和循環(huán)次數(shù)的時(shí)候使用
案例
var arr = [
{
"name":"Java編程思想",
"price":78.9
},
{
"name":"python入門寶典",
"price":59
},
{
"name":"JavaScript程序設(shè)計(jì)",
"price":69
}
];
for(var i=0;i<arr.length;i++){
// console.log(arr[i]);
// json - {"name":"Java編程思想","price":78.9}
for(key in arr[i]){
console.log(arr[i][key]);
}
}
四、跳轉(zhuǎn)語句
1、continue
用于中止本次循環(huán),根據(jù)控制表達(dá)式還允許繼續(xù)進(jìn)行下一次循環(huán)。
continue終止后再繼續(xù)
var iNum = 0;
for (var i=1; i<10; i++) {
if (i % 5 == 0) {
document.write(i);
continue;
}
iNum++;
}
alert(iNum);
2、break
break語句用于退出循環(huán),阻止再次反復(fù)執(zhí)行任何代碼或者退出一個(gè)switch語句文章來源:http://www.zghlxwxcb.cn/news/detail-741794.html
for (var i=1; i<10; i++) {
if (i % 5 == 0) {
break;
}
iNum++;
}
alert(iNum);
?文章來源地址http://www.zghlxwxcb.cn/news/detail-741794.html
到了這里,關(guān)于JavaScript 其他循環(huán)語句和跳轉(zhuǎn)語句的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!