473,396 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 11210
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: QQ | last post by:
I know there are many functions that I can exit the program such as return 0, exit(0), exit(1),_EXIT(0) .... What are the difference between them? Thanks a lot!
1
by: Brendan Miller | last post by:
I am trying to close my application using Application.exit() in the frmMain_Closing event. When the form closes the process does not. My application only has one form (no other classes either). ...
1
by: Ioannis Vranos | last post by:
I am currently reading a chapter involving multithreading, and some sample code calls Environment::Exit() to terminate the application with all threads. What is the difference from...
4
by: Bob Day | last post by:
Using VS 2003, VB.net... I am confused about the Application.Exit method, where the help states "This method does not force the application to exit." Aside from the naming confusion, how do I...
8
by: Zeno Lee | last post by:
What is the best way to return an exit code from a VB.NET windows forms app? My Forms application is dual purpose. It is an interactive windows app. It is also automated and run via a script and...
8
by: Atanas Banov | last post by:
i ran onto this weirdness today: seems like close() on popen-ed (pseudo)file fails miserably with exception instead of returning exit code, when said exit code is -1. here is the simplest...
6
by: Vicky | last post by:
Please tell me at vdkhakhkhar@gmail.com
11
by: yawnmoth | last post by:
To quote from <http://php.net/function.include>, "Because include() is a special language construct, parentheses are not needed around its argument. Take care when comparing return value." ...
39
by: mathieu | last post by:
Hi there, I am trying to reuse a piece of code that was designed as an application. The code is covered with 'exit' calls. I would like to reuse it as a library. For that I renamed the 'main'...
0
by: Gary Robinson | last post by:
In Python 2.5.2, I notice that, in the interpreter or in a script, I can exit with: exit() But I don't see exit() mentioned as a built-in function; rather the Python Library Reference says we...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.