Reputation: 887
What command I must use, to get out of the for loop, also from //code inside
jump direct to //code after
//code before
for(var a in b)
{
switch(something)
{
case something:
{
//code inside
break;
}
}
}
//code after
Upvotes: 37
Views: 39036
Reputation: 41
const array = [1, 2, 3, 4, 5];
outerLoop: for (let i = 0; i < array.length; i++) {
switch (array[i]) {
case 1:
console.log('Case 1');
break; // This breaks out of the switch statement
case 2:
console.log('Case 2');
break; // This breaks out of the switch statement
case 3:
console.log('Case 3');
break outerLoop; // This breaks out of both the switch and the for loop
default:
console.log('Default case');
}
}
console.log('Loop exited');
Upvotes: 0
Reputation: 31062
You can use label
. Have a labeled statement and break to that label. outerLoop
is the label I have used here.
//code before
outerLoop:
for (var a in b) {
switch (something) {
case 'case1':
//code inside
break outerLoop;
}
}
//code after
Upvotes: 42
Reputation:
Replace your switch
with a series of if
statements.
for (const a of b) {
if (something === someValue) {
// code inside
break; // break out of the for loop
} else if (something === someOtherValue) {
// etc.
}
}
Upvotes: 2
Reputation: 237905
Unfortunately, Javascript doesn't have allow break
ing through multiple levels. The easiest way to do this is to leverage the power of the return
statement by creating an anonymous function:
//code before
(function () {
for (var a in b) {
switch (something) {
case something:
{
//code inside
return;
}
}
}
}());
//code after
This works because return
leaves the function and therefore implicitly leaves the loop, moving you straight to code after
As pointed out in the comments, my above answer is incorrect and it is possible to multi-level break
ing, as in Chubby Boy's answer, which I have upvoted.
Whether this is wise is, from a seven-year-later perspective, somewhat questionable.
Upvotes: 18
Reputation: 101614
use another variable to flag when you need to exit:
var b = { firstName: 'Peter', lastName: 'Smith' };
var keepGoing = true;
for (var a in b) {
switch (true) {
case 1:
keepGoing = false;
break;
}
if (!keepGoing) break;
console.log('switch end');
}
console.log('for end');
Upvotes: 28
Reputation: 49
for(var i=0; i<b.length; i++) {
switch (something) {
case 'something1':
i=b.length;
}
}
When the iteration variable i
reaches the end of the loop, it breaks. But one can force the iteration variable to reach the end of the loop.
Upvotes: 1
Reputation: 111
it depends on what you want to accomplish... one thing I commonly do is something like this:
//code before
for(var a in b)
{
var breakFor = false;
switch(something)
{
case something:
{
//code inside
breakFor = true;
break;
}
}
if (breakFor)
break;
}
//code after
Upvotes: 9
Reputation: 2479
You can tell which loop / switch to break.
function foo ()
{
dance:
for(var k = 0; k < 4; k++){
for(var m = 0; m < 4; m++){
if(m == 2){
break dance;
}
}
}
}
See this answer.
Upvotes: 4
Reputation: 2562
I always find using conditional statements one of the easiest ways to control the code flow, at least conceptually.
var done = false;
//code before for loop
for(var a in b){
switch(switchParameter){
case firstCase:
//code inside
done = true;
break;
}
}
if(done)
break;
}
//code outside of for loop
Upvotes: 0