Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > break inside of case- statement inside of loop

Reply
Thread Tools

break inside of case- statement inside of loop

 
 
Alexander Korsunsky
Guest
Posts: n/a
 
      02-25-2007
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?
 
Reply With Quote
 
 
 
 
Beej Jorgensen
Guest
Posts: n/a
 
      02-25-2007
In article <(E-Mail Removed)>,
Alexander Korsunsky <(E-Mail Removed)> wrote:
>for (int i = 0; i < 10; i++)
>{
> switch (array[i])
> {
>[code snipped all up]
> 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?


break only gets you out of the smallest enclosing switch or loop.

You could put another condition in the for:

for (i = done = 0; i < 10 && !done; i++) {
switch(array[i]) {
case 'a':
done = 1;
break; // or "continue" to get right out
case 'b':
break;
}
}

Or you could use goto:

for (i = 0; i < 10; i++) {
switch(array[i]) {
case 'a':
goto done;
case 'b':
break;
}
}
done:

-Beej--4am...hopefully I didn't screw that answer up too bad.

 
Reply With Quote
 
 
 
 
Bill Pursell
Guest
Posts: n/a
 
      02-25-2007
On Feb 25, 11:16 am, Alexander Korsunsky <(E-Mail Removed)>
wrote:

>
> char array[10] = "abcdefghij";


It's unfortunate that this doesn't generate a
compiler warning in the same way that
int array[2] = { 1, 2, 3}; does.


>
> Is it possible to break out of the for loop from inside of one case-
> statement, or do I have to use a workaround?


There are at least 3 obvious ways:
1) increment the index to cause your condition on the
for loop to terminate the loop (a workaround.)
2) goto
3) fix the loop condition to more accurately reflect
whatever it is you are testing for. (Probably
the correct solution.)

There is no "break 3" type syntax in C, however.

--
Bill Pursell


 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      02-25-2007
On Sun, 25 Feb 2007 12:16:06 +0100, Alexander Korsunsky
<(E-Mail Removed)> 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;
> }


// After the switch executes, control passes here.
// You could retest and break here to get out of the for loop.
// For example, breaking if case 'b'.

if(array[i] == 'b')
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?


I'm not aware of a double break command. A break statement stops
execution of the smallest enclosing switch statement. I would break
out of each as above. You might rethink the switch statement and
recast it as a if-else series.

Best wishes,

Bob
 
Reply With Quote
 
Alexander Korsunsky
Guest
Posts: n/a
 
      02-25-2007
Bill Pursell wrote:
> On Feb 25, 11:16 am, Alexander Korsunsky <(E-Mail Removed)>
> wrote:
>
>> char array[10] = "abcdefghij";

>
> It's unfortunate that this doesn't generate a
> compiler warning in the same way that
> int array[2] = { 1, 2, 3}; does.


I agree. I will use array[] = "abcde"; in future.

> There are at least 3 obvious ways:
> 1) increment the index to cause your condition on the
> for loop to terminate the loop (a workaround.)


Doesn't seem to me, that this is good style... Correct me if I'm mistaken.

> 2) goto


I've heard that goto should not be used (anymore) because there are
better ways.
But in my situation it looks like that would be the best possibility,
because I don't have to set up a variable and test it to be false, to
continue the loop.

> 3) fix the loop condition to more accurately reflect
> whatever it is you are testing for. (Probably
> the correct solution.)


In my case, it isn't because I am checking a letter, and then executing
different code for each different letter. This code calls functions
which return error codes if they fail. To prevent the program to fail,
the loop has to be stopped.

Thanks for your help,
Alexander Korsunsky
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      02-25-2007
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
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      02-25-2007
In article <(E-Mail Removed)>,
Alexander Korsunsky <(E-Mail Removed)> wrote:
....
>> There are at least 3 obvious ways:
>> 1) increment the index to cause your condition on the
>> for loop to terminate the loop (a workaround.)

>
>Doesn't seem to me, that this is good style... Correct me if I'm mistaken.


The point is that the "short answer" to your question is "No, you can't
do it (you have to do a workaround)". The slightly longer answer is
"Yes, it is a mis-design in the language, but we have to live with it".
The problem isn't so much the *lack* of "break 2" (as exists in shell),
but rather the use of "break" in the syntax of "switch". "break" should
be (i.e., have been) reserved for the looping constructs and "switch"
clearly isn't a looping construct.

I've asked this same question here in the past and have been told that
"goto" is the best workaround (one of the few situations where "goto" is
"permissible").

 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      02-25-2007
On Sun, 25 Feb 2007 13:54:51 +0100, Alexander Korsunsky
<(E-Mail Removed)> wrote:

>Bill Pursell wrote:

<snip>
>> There are at least 3 obvious ways:
>> 1) increment the index to cause your condition on the
>> for loop to terminate the loop (a workaround.)

>
>Doesn't seem to me, that this is good style... Correct me if I'm mistaken.


Sounds like a hard to maintain work around to me.

>> 2) goto

>
>I've heard that goto should not be used (anymore) because there are
>better ways.


goto, continue, and break are all jump statements. IMHO, they all
share some of the same issues although goto is the worst.

>But in my situation it looks like that would be the best possibility,
>because I don't have to set up a variable and test it to be false, to
>continue the loop.
>
>> 3) fix the loop condition to more accurately reflect
>> whatever it is you are testing for. (Probably
>> the correct solution.)

>
>In my case, it isn't because I am checking a letter, and then executing
>different code for each different letter. This code calls functions
>which return error codes if they fail. To prevent the program to fail,
>the loop has to be stopped.


Then I would not use a switch statement at all. I'm not sure how many
cases you are worrying about, but perhaps something like this:

for (int i = 0; i < 10; i++)
{
if(function(array[i]))
break;
}

function returns an error code and may contain the switch as well, if
needed.

Best wishes,

Bob
 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      02-25-2007
Bob wrote, On 25/02/07 13:21:
> On Sun, 25 Feb 2007 13:54:51 +0100, Alexander Korsunsky
> <(E-Mail Removed)> wrote:
>
>> Bill Pursell wrote:

> <snip>
>>> There are at least 3 obvious ways:
>>> 1) increment the index to cause your condition on the
>>> for loop to terminate the loop (a workaround.)

>> Doesn't seem to me, that this is good style... Correct me if I'm mistaken.

>
> Sounds like a hard to maintain work around to me.


Not something I would like.

>>> 2) goto

>> I've heard that goto should not be used (anymore) because there are
>> better ways.

>
> goto, continue, and break are all jump statements. IMHO, they all
> share some of the same issues although goto is the worst.


setjmp/longjmp is worse IMHO. Which does not mean to say it isn't needed
sometimes.

>> But in my situation it looks like that would be the best possibility,
>> because I don't have to set up a variable and test it to be false, to
>> continue the loop.
>>
>>> 3) fix the loop condition to more accurately reflect
>>> whatever it is you are testing for. (Probably
>>> the correct solution.)

>> In my case, it isn't because I am checking a letter, and then executing
>> different code for each different letter. This code calls functions
>> which return error codes if they fail. To prevent the program to fail,
>> the loop has to be stopped.

>
> Then I would not use a switch statement at all. I'm not sure how many
> cases you are worrying about, but perhaps something like this:
>
> for (int i = 0; i < 10; i++)
> {
> if(function(array[i]))
> break;
> }
>
> function returns an error code and may contain the switch as well, if
> needed.


Or include the for in the function as well and use an early return in
the switch.
--
Flash Gordon
 
Reply With Quote
 
Bill Pursell
Guest
Posts: n/a
 
      02-25-2007
On Feb 25, 12:54 pm, Alexander Korsunsky <(E-Mail Removed)>
wrote:
> Bill Pursell wrote:
> > On Feb 25, 11:16 am, Alexander Korsunsky <(E-Mail Removed)>
> > wrote:

>
>
> > 3) fix the loop condition to more accurately reflect
> > whatever it is you are testing for. (Probably
> > the correct solution.)

>
> In my case, it isn't because I am checking a letter, and then executing
> different code for each different letter. This code calls functions
> which return error codes if they fail. To prevent the program to fail,
> the loop has to be stopped.


You might consider something like:
for( ; !err_flag; ) {
switch(value) {
case 0:
err_flag = do_something();
break;
....

--
Bill Pursell

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
`if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ? lovecreatesbea...@gmail.com C Programming 12 04-14-2008 07:59 AM
break statement in a for loop a.mil@chello.nl C Programming 25 05-15-2007 10:29 PM
How to break a while loop inside a switch statement? howachen@gmail.com Java 16 07-11-2006 10:23 AM
Nested if statement inside a while loop. gstewart@gmail.com Perl Misc 5 09-29-2005 12:09 PM



Advertisments