一、JS退出for循環(huán)語(yǔ)句
在JS中,退出for循環(huán)語(yǔ)句有兩種方式:
1、使用break語(yǔ)句
for(let i = 0; i < 10; i++){
if(i === 5){
break;
}
console.log(i);
}
//輸出結(jié)果:0 1 2 3 4
在上面的代碼中,當(dāng)i的值等于5時(shí),使用break語(yǔ)句跳出循環(huán)。
2、使用return語(yǔ)句
function test(){
for(let i = 0; i < 10; i++){
console.log(i);
if(i === 5){
return;
}
}
}
test();
//輸出結(jié)果:0 1 2 3 4 5
在上面的代碼中,當(dāng)i的值等于5時(shí),使用return語(yǔ)句跳出循環(huán)并退出函數(shù)。
二、JS如何退出for循環(huán)
JS使用break和return語(yǔ)句可以退出for循環(huán)。
break語(yǔ)句可以結(jié)束當(dāng)前循環(huán)語(yǔ)句,轉(zhuǎn)而執(zhí)行當(dāng)前循環(huán)語(yǔ)句后的語(yǔ)句。
return語(yǔ)句可以退出當(dāng)前函數(shù),結(jié)束函數(shù)的執(zhí)行,并返回一個(gè)值給函數(shù)調(diào)用者。
三、退出for循環(huán)
在循環(huán)體內(nèi)判斷條件,符合條件使用break語(yǔ)句退出循環(huán)。
let arr = [1, 2, 3, 4, 5];
for(let i = 0; i < arr.length; i++){
if(arr[i] === 3){
console.log('找到了3');
break;
}
}
//輸出結(jié)果:找到了3
在上面的代碼中,當(dāng)數(shù)組中出現(xiàn)3時(shí),使用break語(yǔ)句跳出循環(huán),輸出找到了3。
四、VB退出for循環(huán)
在VB中,使用Exit For語(yǔ)句退出for循環(huán)。
Dim arr(4)
arr = Array(1, 2, 3, 4, 5)
For i = 0 To 4
If arr(i) = 3 Then
MsgBox "找到了3"
Exit For
End If
Next
在上面的代碼中,當(dāng)數(shù)組中出現(xiàn)3時(shí),使用Exit For語(yǔ)句跳出循環(huán),彈出消息框找到了3。
五、JS跳出for循環(huán)
JS使用continue語(yǔ)句可以跳過(guò)本次循環(huán),直接進(jìn)入下一次循環(huán),不會(huì)退出循環(huán)。
let arr = [1, 2, 3, 4, 5];
for(let i = 0; i < arr.length; i++){
if(arr[i] === 3){
continue;
}
console.log(arr[i]);
}
//輸出結(jié)果:1 2 4 5
在上面的代碼中,當(dāng)數(shù)組中出現(xiàn)3時(shí),使用continue語(yǔ)句跳過(guò)本次循環(huán),直接進(jìn)入下一次循環(huán)。
六、JS退出while循環(huán)
在JS中,退出while循環(huán)有兩種方式,一種是使用break語(yǔ)句,一種是使用return語(yǔ)句。
1、使用break語(yǔ)句
let i = 0;
while(i < 10){
console.log(i);
if(i === 5){
break;
}
i++;
}
//輸出結(jié)果:0 1 2 3 4 5
在上面的代碼中,當(dāng)i的值等于5時(shí),使用break語(yǔ)句跳出循環(huán)。
2、使用return語(yǔ)句
function test(){
let i = 0;
while(i < 10){
console.log(i);
if(i === 5){
return;
}
i++;
}
}
test();
//輸出結(jié)果:0 1 2 3 4 5
在上面的代碼中,當(dāng)i的值等于5時(shí),使用return語(yǔ)句跳出循環(huán)并退出函數(shù)。
七、JS forEach退出循環(huán)
在JS中,可以使用forEach方法遍歷數(shù)組,但是無(wú)法使用break語(yǔ)句跳出循環(huán),需要使用return語(yǔ)句跳出循環(huán)。
let arr = [1, 2, 3, 4, 5];
arr.forEach(function(item){
console.log(item);
if(item === 3){
return;
}
});
//輸出結(jié)果:1 2 3
在上面的代碼中,當(dāng)數(shù)組中出現(xiàn)3時(shí),使用return語(yǔ)句跳出循環(huán),只輸出前三個(gè)元素。
八、JS退出循環(huán)并退出函數(shù)
在JS中,使用return語(yǔ)句可以退出循環(huán)并退出函數(shù)。
function test(){
for(let i = 0; i < 10; i++){
console.log(i);
if(i === 5){
return;
}
}
console.log('不會(huì)執(zhí)行');
}
test();
//輸出結(jié)果:0 1 2 3 4 5
在上面的代碼中,當(dāng)i的值等于5時(shí),使用return語(yǔ)句跳出循環(huán)并退出函數(shù),不會(huì)執(zhí)行后面的代碼。
九、JS退出循環(huán)
JS使用break和return語(yǔ)句可以退出循環(huán)。
break語(yǔ)句可以結(jié)束當(dāng)前循環(huán)語(yǔ)句,轉(zhuǎn)而執(zhí)行當(dāng)前循環(huán)語(yǔ)句后的語(yǔ)句。
return語(yǔ)句可以退出當(dāng)前函數(shù),結(jié)束函數(shù)的執(zhí)行,并返回一個(gè)值給函數(shù)調(diào)用者。
完整示例代碼:
//使用break語(yǔ)句退出for循環(huán)
for(let i = 0; i < 10; i++){
if(i === 5){
break;
}
console.log(i);
}
//使用return語(yǔ)句退出for循環(huán)
function test(){
for(let i = 0; i < 10; i++){
console.log(i);
if(i === 5){
return;
}
}
}
test();
//在循環(huán)體內(nèi)判斷條件,符合條件使用break語(yǔ)句退出循環(huán)
let arr = [1, 2, 3, 4, 5];
for(let i = 0; i < arr.length; i++){
if(arr[i] === 3){
console.log('找到了3');
break;
}
}
//使用Exit For語(yǔ)句退出for循環(huán)
Dim arr(4)
arr = Array(1, 2, 3, 4, 5)
For i = 0 To 4
If arr(i) = 3 Then
MsgBox "找到了3"
Exit For
End If
Next
//使用continue語(yǔ)句跳出for循環(huán)
let arr = [1, 2, 3, 4, 5];
for(let i = 0; i < arr.length; i++){
if(arr[i] === 3){
continue;
}
console.log(arr[i]);
}
//使用break語(yǔ)句退出while循環(huán)
let i = 0;
while(i < 10){
console.log(i);
if(i === 5){
break;
}
i++;
}
//使用return語(yǔ)句退出while循環(huán)
function test(){
let i = 0;
while(i < 10){
console.log(i);
if(i === 5){
return;
}
i++;
}
}
test();
//使用return語(yǔ)句跳出forEach循環(huán)
let arr = [1, 2, 3, 4, 5];
arr.forEach(function(item){
console.log(item);
if(item === 3){
return;
}
});
//使用return語(yǔ)句退出循環(huán)并退出函數(shù)
function test(){
for(let i = 0; i < 10; i++){
console.log(i);
if(i === 5){
return;
}
}
console.log('不會(huì)執(zhí)行');
}
test();