Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking out from nested for loops

I have two for loops want exit from two loops my control is at inner loop see the below code

for(condition)
{
    for (condition)
    {
         if(condition)
                  from here i want to exit from these loops 
     }
}
like image 945
Dasari Vinodh Avatar asked Dec 11 '22 08:12

Dasari Vinodh


1 Answers

JS uses statement labels to give statements identifiers. This is great for use on nested loops, because it can give your inner statement control of what's happening beyond its 'tier' of loop. Statement labels can be used with either the continue or break statements.

So your code can be:

someName: 
for(condition)
{
    for (condition)
    {
         if(condition)
                  if (condition) {
                      break someName; // exit outer loop
                  }
     }
}

For more info, see MDN "label".

like image 162
James Peterson Avatar answered Jan 01 '23 10:01

James Peterson