By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,784 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,784 IT Pros & Developers. It's quick & easy.

help on "continue" - C#

P: n/a
As per this URL
http://msdn.microsoft.com/library/en...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
Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <Ha*********@Hotmail.com> wrote in message
news:uY****************@TK2MSFTNGP09.phx.gbl...
As per this URL
http://msdn.microsoft.com/library/en...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

Nov 22 '05 #2

P: n/a
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" <Be***********@jda.com> wrote in message
news:OQ*************@TK2MSFTNGP11.phx.gbl...
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" <Ha*********@Hotmail.com> wrote in message
news:uY****************@TK2MSFTNGP09.phx.gbl...
As per this URL
http://msdn.microsoft.com/library/en...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


Nov 22 '05 #3

P: n/a
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" <Ha*********@Hotmail.com> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...
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" <Be***********@jda.com> wrote in message
news:OQ*************@TK2MSFTNGP11.phx.gbl...
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" <Ha*********@Hotmail.com> wrote in message
news:uY****************@TK2MSFTNGP09.phx.gbl...
As per this URL
http://msdn.microsoft.com/library/en...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



Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.