Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I skip an iteration of a `foreach` loop?

Tags:

c#

.net

loops

In Perl I can skip a foreach (or any loop) iteration with a next; command.

Is there a way to skip over an iteration and jump to the next loop in C#?

 foreach (int number in numbers)  {      if (number < 0)      {          // What goes here to skip over the loop?      }       // otherwise process number  } 
like image 954
Brian Avatar asked Mar 17 '09 12:03

Brian


People also ask

How do you go to next in foreach?

The reason why using a return statement to continue in a JavaScript forEach loop works is because when you use a forEach loop you have to pass in a callback function. The only way to continue to the next iteration is when your callback function completes and finishes running the code within itself.

Does continue break out of foreach loop?

Continue is not the opposite of break , break stops the loop, continue stops the current iteration.

How do you stop a foreach loop in C#?

Exit Foreach Loop Using break Keyword In C# Let's say you have a list of colors or an array of colors and you are looping through the list and now you have to exit the foreach loop, you will use the break keyword to exit the loop.

Can we use continue in foreach C#?

In C#, the continue statement is used to skip over the execution part of the loop(do, while, for, or foreach) on a certain condition, after that, it transfers the control to the beginning of the loop.


2 Answers

You want:

foreach (int number in numbers) //   <--- go back to here --------+ {                               //                                |     if (number < 0)             //                                |     {                           //                                |         continue;   // Skip the remainder of this iteration. -----+     }      // do work } 

Here's more about the continue keyword.


Update: In response to Brian's follow-up question in the comments:

Could you further clarify what I would do if I had nested for loops, and wanted to skip the iteration of one of the extended ones?

for (int[] numbers in numberarrays) {   for (int number in numbers) { // What to do if I want to                                 // jump the (numbers/numberarrays)?   } } 

A continue always applies to the nearest enclosing scope, so you couldn't use it to break out of the outermost loop. If a condition like that arises, you'd need to do something more complicated depending on exactly what you want, like break from the inner loop, then continue on the outer loop. See here for the documentation on the break keyword. The break C# keyword is similar to the Perl last keyword.

Also, consider taking Dustin's suggestion to just filter out values you don't want to process beforehand:

foreach (var basket in baskets.Where(b => b.IsOpen())) {   foreach (var fruit in basket.Where(f => f.IsTasty())) {     cuteAnimal.Eat(fruit); // Om nom nom. You don't need to break/continue                            // since all the fruits that reach this point are                            // in available baskets and tasty.   } } 
like image 64
John Feminella Avatar answered Sep 28 '22 04:09

John Feminella


Another approach is to filter using LINQ before the loop executes:

foreach ( int number in numbers.Where(n => n >= 0) ) {     // process number } 
like image 31
Dustin Campbell Avatar answered Sep 28 '22 05:09

Dustin Campbell