help on "continue" - C#

  • Thread starter Thread starter Harman Sahni
  • Start date Start date
H

Harman Sahni

As per this URL
http://msdn.microsoft.com/library/en-us/vjref98/html/14_14.asp?frame=true
conitnue works on for, while, do... I know it works for foreach as well as
I'm using it somewhere.

My question is how do I "continue" (jump an iteration) of an outermost loop
from an inner loop.

So I have something like this:

foreach(foo)
{
foreach(blah blah)
{
for(i= gooo gooo)
{
continue; (I want this continue to jump an iteration of the
foreach(foo foo)
}
}
}

Thank you in anticipation!
Harman Sahni
 
Hi Harman,

C# does not support "continue <label>", instead use Goto (but only if you
really have to ;-)

example:

static void Foo()
{
int[] ArrayOne = new int[] { 1, 2, 3 };
int[] ArrayTwo = new int[] { 10, 20, 30 };
int[] ArrayThree = new int[] { 100, 200, 300 };
foreach(int i in ArrayOne)
{
Console.WriteLine(i);
foreach(int j in ArrayTwo)
{
Console.WriteLine(j);
foreach(int k in ArrayThree)
{
Console.WriteLine(k);
if (k >= 200)
{
goto done;
}
}
}
}
done: Console.WriteLine("We are done");
}

Regards,

Bennie Haelen
 
Gotcha!

I guess I have to use it then coz I can't see how else one can jump
iteration from within nested foreach loops.

Thx!
Harman

Bennie Haelen said:
Hi Harman,

C# does not support "continue <label>", instead use Goto (but only if you
really have to ;-)

example:

static void Foo()
{
int[] ArrayOne = new int[] { 1, 2, 3 };
int[] ArrayTwo = new int[] { 10, 20, 30 };
int[] ArrayThree = new int[] { 100, 200, 300 };
foreach(int i in ArrayOne)
{
Console.WriteLine(i);
foreach(int j in ArrayTwo)
{
Console.WriteLine(j);
foreach(int k in ArrayThree)
{
Console.WriteLine(k);
if (k >= 200)
{
goto done;
}
}
}
}
done: Console.WriteLine("We are done");
}

Regards,

Bennie Haelen
Harman Sahni said:
As per this URL
http://msdn.microsoft.com/library/en-us/vjref98/html/14_14.asp?frame=true
conitnue works on for, while, do... I know it works for foreach as well as
I'm using it somewhere.

My question is how do I "continue" (jump an iteration) of an outermost loop
from an inner loop.

So I have something like this:

foreach(foo)
{
foreach(blah blah)
{
for(i= gooo gooo)
{
continue; (I want this continue to jump an iteration of the
foreach(foo foo)
}
}
}

Thank you in anticipation!
Harman Sahni
 
You can do it by adding flag variable:

bool jumpOut=false;

foreach (foo1)
{
foreach ( foo2)
{
foreach (foo3)
{
if (someCondition)
{
jumpOut=true;
break;
}
}

if (jumpOut) break;
}

if (jumpOut) continue; //ignore below doSomething() and go next loop

//other steps
doSomething()
}
Harman Sahni said:
Gotcha!

I guess I have to use it then coz I can't see how else one can jump
iteration from within nested foreach loops.

Thx!
Harman

Bennie Haelen said:
Hi Harman,

C# does not support "continue <label>", instead use Goto (but only if you
really have to ;-)

example:

static void Foo()
{
int[] ArrayOne = new int[] { 1, 2, 3 };
int[] ArrayTwo = new int[] { 10, 20, 30 };
int[] ArrayThree = new int[] { 100, 200, 300 };
foreach(int i in ArrayOne)
{
Console.WriteLine(i);
foreach(int j in ArrayTwo)
{
Console.WriteLine(j);
foreach(int k in ArrayThree)
{
Console.WriteLine(k);
if (k >= 200)
{
goto done;
}
}
}
}
done: Console.WriteLine("We are done");
}

Regards,

Bennie Haelen
Harman Sahni said:
As per this URL
http://msdn.microsoft.com/library/en-us/vjref98/html/14_14.asp?frame=true
conitnue works on for, while, do... I know it works for foreach as
well
 
Back
Top