Alexander Korsunsky wrote:
> Hi!
>
> I have some code that looks similar to this:
>
> --------------------------------------------
>
> char array[10] = "abcdefghij";
>
> for (int i = 0; i < 10; i++)
> {
> switch (array[i])
> {
> case 'a':
> /* code */
> break;
> case 'b':
> /* code */
> break;
> case 'c':
> /* code */
> break;
>
> default:
> /* code */
> break;
> }
>
> }
>
> --------------------------------------------
>
> Is it possible to break out of the for loop from inside of one case-
> statement, or do I have to use a workaround?
You cannot `break' from the inside of the `switch' to
the outside of the `for'. Here is one alternative:
for (int i = 0; i < 10; ++i) {
switch(array[i]) {
case 'a':
/* code */
continue;
case 'b':
/* code */
if (want_to_break_out)
break;
continue;
default:
/* code */
continue;
}
break;
}
I cannot recommend this dodge for all circumstances.
Code is read by compilers and by people; the former are the
less important audience.
--
Eric Sosman
lid