0

I have two loops and when I iterating in my inner loop, I want to skip iteration for the outer loop according to my condition; continue; is only skips inner loop. Any idea?

foreach (DataRow row in dt.Rows)
 {
                for (int i = 0; i < 6; i++)
                {
                    if (row[i].Equals(DBNull.Value))
                        //skip iteration to outer loop. Go to next row.
                }
 }
3
  • You cannot jump to the next outer-loop-iteration until your inner loop ended. Is this a problem for you? If not, you can use break. Are there other instructions behind your inner loop which are inside the outer-loop too? Commented Aug 29, 2016 at 11:35
  • 1
    @cihata87 write break; it will break inner loop and continue the outer loop Commented Aug 29, 2016 at 11:35
  • @Akanksha break; breaks from inner loop but it continues under for loop. I want to go to the } of foreach loop so I can check next row. Commented Aug 29, 2016 at 12:09

2 Answers 2

2

I think break will serve the purpos, if you want to get out of the inner loop and continue with the next row of the outer loop

  foreach (DataRow row in dt.Rows)
  {
            for (int i = 0; i < 6; i++)
            {
                if (row[i].Equals(DBNull.Value))
                    break;
            }
     }
Sign up to request clarification or add additional context in comments.

Comments

0

You could do this:

foreach (DataRow row in dt.Rows)
{
    foreach (int i in Enumerable.Range(0, 6).TakeWhile(n => !row[n].Equals(DBNull.Value)))
    {
        // no need for `if` at all now.
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.