Using foreach loop.

Posted by Harikrishna on Stack Overflow See other posts from Stack Overflow or by Harikrishna
Published on 2010-03-17T11:49:34Z Indexed on 2010/03/17 11:51 UTC
Read the original article Hit count: 496

Filed under:
|
|
|
|

I break the code of the for loop without using break like I have for loop given below.And when i is 1 or 2 or 3 or any else but if condition is true then loop will be terminated because i will be 5 if the condition is true.And so NO need of break is needed there.Beacause I do not want to use break.I have done like this here.It works.

bool myCondition=false;
for(int i=0;i<5;i++)
{
   if(myCondition)
   {
       i=5;
   }
}

But now I want to use foreach loop and in this loop when some condition is true then I want to break the foreach loop code.So what should I do here for breaking the foreach loop code without using break ? Like in the above for loop I have initialize i to 5 when condition is true.In the foreach loop anything like that to do to avoid break.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET