JavaScript istirahat untuk loop
//break out of for loop
for (i = 0; i < 10; i++) {
if (i === 3) { break; }
}
Grepper
//break out of for loop
for (i = 0; i < 10; i++) {
if (i === 3) { break; }
}
breakme: if (condition) {
// Do stuff
if (condition2){
// do stuff
} else {
break breakme;
}
// Do more stuff
}
// program to print the value of i
for (let i = 1; i <= 5; i++) {
// break condition
if (i == 3) {
break;
}
console.log(i);
}
// program to find the sum of positive numbers
// if the user enters a negative numbers, break ends the loop
// the negative number entered is not added to sum
let sum = 0, number;
while(true) {
// take input again if the number is positive
number = parseInt(prompt('Enter a number: '));
// break condition
if(number < 0) {
break;
}
// add all positive numbers
sum += number;
}
// display the sum
console.log(`The sum is ${sum}.`);