There are two ways to ignore loop in else condition:
- Continue
- Break
Please see this, for explanations of the same.
In simple terms, The Break statement exits out of the loop while the continue statement exits out of the particular iteration.
Let’s understand further with some examples.
for loop with continue statement:
// Defining the variable var i; // For loop for (i = 0; i < 3; i++) { // If i is equal to 1, it // skips the iteration if (i === 1) { continue; } // Printing i console.log(i); } |
Output:
0 2
for loop with Break Statement:
// Defining the variable var i; // For loop for (i = 0; i < 3; i++) { // If i is equal to 1, it comes // out of the for a loop if (i === 1) { break; } // Printing i console.log(i); } |
Output:
0
For eachloop: AngularJS gets pretty messy with break and continue statements when it comes to the forEach loop.
The break and continue statements do not work as expected, the best way to implement continue would be to use return statements, the break cannot be implemented in forEach loop.
// Loop which runs through the array [0, 1, 2] // and ignores when the element is 1 angular.forEach([0, 1, 2], function(count){ if(count == 1) { return true; } // Printing the element console.log(count); }); |
Output:
0 2
However, the action of break can be achieved by including a boolean function, as implemented in the example below:
// A Boolean variable var flag = true; // forEach loop which iterates through // the array [0, 1, 2] angular.forEach([0, 1, 2], function(count){ // If the count equals 1 we // set the flag to false if(count==1) { flag = false; } // If the flag is true we print the count if(flag){ console.log(count); } }); |
Output:
0
Recommended Posts:
- Decision Making in R Programming - if, if-else, if-else-if ladder, nested if-else, and switch
- How to ignore mouse interaction on overlay image using JavaScript ?
- if/else condition in CSS
- if-else Statement in JavaScript
- How to break nested for loop using JavaScript?
- How to trigger setInterval loop immediately using JavaScript ?
- Difference between try-catch and if-else statements in PHP
- SASS | @if and @else
- How to use *ngIf else in AngularJS ?
- R - if-else statement
- Vue.js | v-else directive
- v-else-if Directive in Vue.js
- Why “0” is not equal to false in if condition in JavaScript ?
- JavaScript SyntaxError - Missing ) after condition
- JavaScript | While Loop
- JavaScript | For Loop
- How to use loop through an array in JavaScript?
- How to add a delay in a JavaScript loop?
- JavaScript for-in Loop
- What are the microtask and macrotask within an event loop in JavaScript ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


