469,356 Members | 2,172 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

How to exit a For-Next in a nested Switch statement?

Hi,

Back to basics!

My understanding is that the only way to exit a For-Next loop prematurely is
with the 'break' keyword. How are you supposed to do that if you're inside
a Switch statement? The break keyword will only come out of the switch, not
the for-next loop.
for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
exit the for loop;

case 1 :
... do somehting
exit the for loop;

case 2 :
... do somehting
exit the for loop;
}
}
Cheers,
PeterZ
Nov 17 '05 #1
13 10863
I think there is no built-in language support for doing that. Again, you have
a work-around - use a Label and goto statement. :-)

"PeterZ" wrote:
Hi,

Back to basics!

My understanding is that the only way to exit a For-Next loop prematurely is
with the 'break' keyword. How are you supposed to do that if you're inside
a Switch statement? The break keyword will only come out of the switch, not
the for-next loop.
for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
exit the for loop;

case 1 :
... do somehting
exit the for loop;

case 2 :
... do somehting
exit the for loop;
}
}
Cheers,
PeterZ

Nov 17 '05 #2
> My understanding is that the only way to exit a For-Next loop prematurely is
with the 'break' keyword. How are you supposed to do that if you're inside
a Switch statement? The break keyword will only come out of the switch, not
the for-next loop.
for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
exit the for loop;

case 1 :
... do somehting
exit the for loop;

case 2 :
... do somehting
exit the for loop;
}
}


Peter,
In your case as presented I believe the following (caveat - untested code!) will
work (note the added braces around the "if" clause):

for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
{
switch (i)
{
case 0 :
... do somehting
break; // exit the switch

case 1 :
... do somehting
break; // exit the switch
}
break; // exit the for loop
}
}

HTH,
-rick-
Nov 17 '05 #3
PeterZ wrote:
My understanding is that the only way to exit a For-Next loop prematurely
is
with the 'break' keyword. How are you supposed to do that if you're inside
a Switch statement? The break keyword will only come out of the switch,
not
the for-next loop.


Well, this is really one good example why break isn't much better that
goto. Why don't you just set a flag:

bool endLoop = false;
for (int i = 0; i <= myArray.Length && !endLoop; i++) {
...
switch(i) {
case 0:
// whatever
endLoop = true;
break;
}
}

This way you have a clearly defined loop, you can find out after the loop
why exactly you left it, and you don't go jumping around the program flow
like break does.

I'm not saying never use it - but I do think it should be used only in the
simplest situations where it's absolutely clear what will happen.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #4
Hi PeterZ,
I would put the body of the for loop into another function to aid
readability. The function will return a boolean indicating if the for loop
should continue executing. So the following code:

for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
exit the for loop;

case 1 :
... do somehting
exit the for loop;

case 2 :
... do somehting
exit the for loop;
}
}
becomes:
public void FunctionLoop()
{
for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)

if(!DoLoopWork(i))
{
//break out of the loop
break;
}
}
}

public bool DoLoopWork(int i)
{
bool blnContinueLooping = true;

switch (i)
{
case 0 :
... do somehting
break;

case 1 :
... do somehting

//exit the for loop;
blnContinueLooping = false;

break;
}

return blnContinueLooping;
}
Hope that helps
Mark R Dawson
http://www.markdawson.org


"PeterZ" wrote:
Hi,

Back to basics!

My understanding is that the only way to exit a For-Next loop prematurely is
with the 'break' keyword. How are you supposed to do that if you're inside
a Switch statement? The break keyword will only come out of the switch, not
the for-next loop.
for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
exit the for loop;

case 1 :
... do somehting
exit the for loop;

case 2 :
... do somehting
exit the for loop;
}
}
Cheers,
PeterZ

Nov 17 '05 #5
I think Oliver hit the mark.
This is how Iwould do it.

And I would never let a goto go live if there was another solution. And each
time I see a goto I start to think. And always I solve them. A goto is the
same as poor design.

Happy Spaghetti
- Michael S

"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
PeterZ wrote:
My understanding is that the only way to exit a For-Next loop prematurely
is
with the 'break' keyword. How are you supposed to do that if you're
inside
a Switch statement? The break keyword will only come out of the switch,
not
the for-next loop.


Well, this is really one good example why break isn't much better that
goto. Why don't you just set a flag:

bool endLoop = false;
for (int i = 0; i <= myArray.Length && !endLoop; i++) {
...
switch(i) {
case 0:
// whatever
endLoop = true;
break;
}
}

This way you have a clearly defined loop, you can find out after the loop
why exactly you left it, and you don't go jumping around the program flow
like break does.

I'm not saying never use it - but I do think it should be used only in the
simplest situations where it's absolutely clear what will happen.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog

Nov 17 '05 #6
Thanks guys!

I my particular case, I think Rick's is the most simple, but I can see where
on other occasions the other suggestions would be more appropriate - eg.
where you only wanted to exit the loop in a specific case statement rather
than all of them.

Thanks again!
PeterZ
"PeterZ" <_r**********************@hotmail.com> wrote in message
news:eB*************@tk2msftngp13.phx.gbl...
Hi,

Back to basics!

My understanding is that the only way to exit a For-Next loop prematurely is
with the 'break' keyword. How are you supposed to do that if you're inside
a Switch statement? The break keyword will only come out of the switch, not
the for-next loop.
for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
exit the for loop;

case 1 :
... do somehting
exit the for loop;

case 2 :
... do somehting
exit the for loop;
}
}
Cheers,
PeterZ

Nov 17 '05 #7
I agree that goto is not ideal, that it even is poor design.

The fact that there continues to be a case when goto is often used means
that it is poor language design. In this specific case, C# does not provide
a way to exit the for (break) without confusing this with the exit case (also
break).

I would suggest that a nested function ala Pascal would provide the ability
to break from the for within the switch. The nested function is the ideal
solution as the function is only intended to be called from within this
function.

While C# doesn't provide nested functions, use a function and figure a way
to not pollute the namespace with this function yourself.

"Michael S" wrote:
I think Oliver hit the mark.
This is how Iwould do it.

And I would never let a goto go live if there was another solution. And each
time I see a goto I start to think. And always I solve them. A goto is the
same as poor design.

Happy Spaghetti
- Michael S

"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
PeterZ wrote:
My understanding is that the only way to exit a For-Next loop prematurely
is
with the 'break' keyword. How are you supposed to do that if you're
inside
a Switch statement? The break keyword will only come out of the switch,
not
the for-next loop.


Well, this is really one good example why break isn't much better that
goto. Why don't you just set a flag:

bool endLoop = false;
for (int i = 0; i <= myArray.Length && !endLoop; i++) {
...
switch(i) {
case 0:
// whatever
endLoop = true;
break;
}
}

This way you have a clearly defined loop, you can find out after the loop
why exactly you left it, and you don't go jumping around the program flow
like break does.

I'm not saying never use it - but I do think it should be used only in the
simplest situations where it's absolutely clear what will happen.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog


Nov 17 '05 #8
I agree that goto is not ideal, that it even is poor design.

The fact that there continues to be a case when goto is often used means
that it is poor language design. In this specific case, C# does not provide
a way to exit the for (break) without confusing this with the exit case (also
break).

I would suggest that a nested function ala Pascal would provide the ability
to break from the for within the switch. The nested function is the ideal
solution as the function is only intended to be called from within this
function.

While C# doesn't provide nested functions, use a function and figure a way
to not pollute the namespace with this function yourself.

"Michael S" wrote:
I think Oliver hit the mark.
This is how Iwould do it.

And I would never let a goto go live if there was another solution. And each
time I see a goto I start to think. And always I solve them. A goto is the
same as poor design.

Happy Spaghetti
- Michael S

"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
PeterZ wrote:
My understanding is that the only way to exit a For-Next loop prematurely
is
with the 'break' keyword. How are you supposed to do that if you're
inside
a Switch statement? The break keyword will only come out of the switch,
not
the for-next loop.


Well, this is really one good example why break isn't much better that
goto. Why don't you just set a flag:

bool endLoop = false;
for (int i = 0; i <= myArray.Length && !endLoop; i++) {
...
switch(i) {
case 0:
// whatever
endLoop = true;
break;
}
}

This way you have a clearly defined loop, you can find out after the loop
why exactly you left it, and you don't go jumping around the program flow
like break does.

I'm not saying never use it - but I do think it should be used only in the
simplest situations where it's absolutely clear what will happen.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog


Nov 17 '05 #9
I agree, in your case Rick's solution is really the best.

However, if you want to exit the for loop in some cases but not in some
other, you could put the loop and switch inside a method and use the return
statement:

private void myFunction(bool[] myArray)
{
for (int i=0; i<myArray.Length; i++)
{
Console.WriteLine(i);
if (myArray[i] == true)
switch (i)
{
case 0:
Console.WriteLine("Case 0");
return;
case 1:
Console.WriteLine("Case 1");
break;
default:
Console.WriteLine("Default");
break;
}
}
}

However, my opinion is that using break in a loop really is a jump statement
and should be used carefully.... you can always implement the desired
behaviour using flags and a while loop... most languages do not have a break
statement for terminating loops and this is how you implement that behaviour
in them. By naming the flag with a meaningful name your code will be more
readable, using break may not always be so clear, for instance if you have
nested loops. In other words, I prefer Oliver's solution over mine above.
"PeterZ" wrote:
Thanks guys!

I my particular case, I think Rick's is the most simple, but I can see where
on other occasions the other suggestions would be more appropriate - eg.
where you only wanted to exit the loop in a specific case statement rather
than all of them.

Thanks again!
PeterZ
"PeterZ" <_r**********************@hotmail.com> wrote in message
news:eB*************@tk2msftngp13.phx.gbl...
Hi,

Back to basics!

My understanding is that the only way to exit a For-Next loop prematurely is
with the 'break' keyword. How are you supposed to do that if you're inside
a Switch statement? The break keyword will only come out of the switch, not
the for-next loop.
for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
exit the for loop;

case 1 :
... do somehting
exit the for loop;

case 2 :
... do somehting
exit the for loop;
}
}
Cheers,
PeterZ

Nov 17 '05 #10
Just for saving your time: The following code compiles well, but it is a
U-turn:

for(int i = 0; i<10; i++) {
switch( i ) {
case 0: Console.WriteLine("Start"); break;
case 4: {
Console.WriteLine("Haf");
break;
};
break;
default: Console.WriteLine(i);break;
}
}
Console.WriteLine("End");

Produces funny:
Start
1
2
3
Haf
5
6
7
8
9
End
Nov 17 '05 #11
Perhaps there should be some way of naming blocks of code, so one might
specify to which the break statment applies.

"jgorlick" wrote:
I agree that goto is not ideal, that it even is poor design.

The fact that there continues to be a case when goto is often used means
that it is poor language design. In this specific case, C# does not provide
a way to exit the for (break) without confusing this with the exit case (also
break).

I would suggest that a nested function ala Pascal would provide the ability
to break from the for within the switch. The nested function is the ideal
solution as the function is only intended to be called from within this
function.

While C# doesn't provide nested functions, use a function and figure a way
to not pollute the namespace with this function yourself.

"Michael S" wrote:
I think Oliver hit the mark.
This is how Iwould do it.

And I would never let a goto go live if there was another solution. And each
time I see a goto I start to think. And always I solve them. A goto is the
same as poor design.

Happy Spaghetti
- Michael S

"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:xn****************@msnews.microsoft.com...
PeterZ wrote:

>My understanding is that the only way to exit a For-Next loop prematurely
>is
>with the 'break' keyword. How are you supposed to do that if you're
>inside
>a Switch statement? The break keyword will only come out of the switch,
>not
>the for-next loop.

Well, this is really one good example why break isn't much better that
goto. Why don't you just set a flag:

bool endLoop = false;
for (int i = 0; i <= myArray.Length && !endLoop; i++) {
...
switch(i) {
case 0:
// whatever
endLoop = true;
break;
}
}

This way you have a clearly defined loop, you can find out after the loop
why exactly you left it, and you don't go jumping around the program flow
like break does.

I'm not saying never use it - but I do think it should be used only in the
simplest situations where it's absolutely clear what will happen.
Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog


Nov 17 '05 #12
hi,

the way i've exited for loops in the past is to set the loop variable
to the max so the loop drops out naturally on it's next itteration.

i.e.
for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
i = myArray.Length + 1;
break;
case 1 :
... do somehting
i = myArray.Length + 1;
break;
case 2 :
... do somehting
i = myArray.Length + 1;
break;
}
}

i've never had any problems with this method.

hope this helps
:)

Nov 17 '05 #13
i do this too, there is convention that states that code should manipulate
the counter. if you don't care about convention, that's fine

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------

"raican" <go**********@raican.co.uk> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
hi,

the way i've exited for loops in the past is to set the loop variable
to the max so the loop drops out naturally on it's next itteration.

i.e.
for (int i=0; i<=myArray.Length; i++)
{
if (myArray[i] == true)
switch (i)
{
case 0 :
... do somehting
i = myArray.Length + 1;
break;
case 1 :
... do somehting
i = myArray.Length + 1;
break;
case 2 :
... do somehting
i = myArray.Length + 1;
break;
}
}

i've never had any problems with this method.

hope this helps
:)

Nov 17 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Bob Day | last post: by
6 posts views Thread by Vicky | last post: by
11 posts views Thread by yawnmoth | last post: by
39 posts views Thread by mathieu | last post: by
reply views Thread by Gary Robinson | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.