Question

In my "native" programming language (RPG), I can write a loop and just leave the loop or force an iteration. It is kind of like a GOTO.

dow (x < 999);
  read file;
  if (%eof);
    leave; // Leave the loop
  endif;
  if (field <> fileField);
    iter; // Iterate to the next record
  endif;
enddo;

My question is if there is a similar option is C#. In my case, I am working with a foreach loop.

Was it helpful?

Solution

continue; // Goto the next iteration
break; // Exit the loop

OTHER TIPS

Break will exit the loop. Continue will jump to the next iteration.

Use the continue keyword

for (int i = 1; i <= 10; i++) 
  {
     if (i < 9) 
        continue;
     Console.WriteLine(i);
  }

the output of this is :

9
10
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top