panthro
panthro

Reputation: 24061

How do I prevent execution of code in a "for" loop?

I have a loop:

for (var language in locales) {
    if(language == 'en') //go on to next language and do not do code below

I wish to prevent code inside the for from executing in certain conditions.

I've tried break; but this stops the entire loop from continuing on to the next language.

How can I prevent code from executing below the if statement but still maintain a loop?

Upvotes: 2

Views: 225

Answers (3)

Oriol
Oriol

Reputation: 288000

Alternatively to continue, you can just use an if statement:

for (var language in locales) if(language !== 'en') {
  // ...
}

Upvotes: 2

setu
setu

Reputation: 181

Use-continue

for (var language in locales) {
    if(language == 'en')
       continue;
    statement;

}

Upvotes: 2

Mohit Bhardwaj
Mohit Bhardwaj

Reputation: 10083

You need to use continue for skipping current iteration of a loop and continue with the next iteration.

Upvotes: 7

Related Questions