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

  • Thread starter Thread starter PeterZ
  • Start date Start date
P

PeterZ

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 == 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
 
I think there is no built-in language support for doing that. Again, you have
a work-around - use a Label and goto statement. :-)
 
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 == 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.
 
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
 
Perhaps there should be some way of naming blocks of code, so one might
specify to which the break statment applies.
 
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 == 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
:)
 
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
 
Back
Top