Velocity Reviews > switch/continue

# switch/continue

Christopher Benson-Manica
Guest
Posts: n/a

 06-07-2004
Given the following snippet,

int i;

for( i=0 ; i < 10 ; i++ ) {
switch( i ) {
case 0: case 1:
continue; /* NB */
default:
/* do something */
}
}

The indicated continue statement will apply to the for loop, correct?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Ben Pfaff
Guest
Posts: n/a

 06-08-2004
Christopher Benson-Manica <(E-Mail Removed)> writes:

> for( i=0 ; i < 10 ; i++ ) {
> switch( i ) {
> case 0: case 1:
> continue; /* NB */
> default:
> /* do something */
> }
> }
>
> The indicated continue statement will apply to the for loop, correct?

Yes. As C99 says, "A continue statement causes a jump to the
loop-continuation portion of the smallest enclosing
iteration statement; that is, to the end of the loop body." Note
that a switch statement is not an iteration statement.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}

CBFalconer
Guest
Posts: n/a

 06-08-2004
Ben Pfaff wrote:
> Christopher Benson-Manica <(E-Mail Removed)> writes:
>
>> for( i=0 ; i < 10 ; i++ ) {
>> switch( i ) {
>> case 0: case 1:
>> continue; /* NB */
>> default:
>> /* do something */
>> }
>> }
>>
>> The indicated continue statement will apply to the for loop,
>> correct?

>
> Yes. As C99 says, "A continue statement causes a jump to the
> loop-continuation portion of the smallest enclosing
> iteration statement; that is, to the end of the loop body."
> Note that a switch statement is not an iteration statement.

However it might be well to avoid the construct, both because it
may be obscure to a future reader, and it is the sort of unusual
construct that can harbor compiler bugs.

--
Chuck F ((E-Mail Removed)) ((E-Mail Removed))
Available for consulting/temporary embedded and systems.

Thomas Matthews
Guest
Posts: n/a

 06-08-2004
Christopher Benson-Manica wrote:

> Given the following snippet,
>
> int i;
>
> for( i=0 ; i < 10 ; i++ ) {
> switch( i ) {
> case 0: case 1:
> continue; /* NB */
> default:
> /* do something */
> }
> }
>
> The indicated continue statement will apply to the for loop, correct?
>

Search the web for "Duff's Device".

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Alan Balmer
Guest
Posts: n/a

 06-08-2004
On Tue, 08 Jun 2004 07:46:59 GMT, CBFalconer <(E-Mail Removed)>
wrote:

>Ben Pfaff wrote:
>> Christopher Benson-Manica <(E-Mail Removed)> writes:
>>
>>> for( i=0 ; i < 10 ; i++ ) {
>>> switch( i ) {
>>> case 0: case 1:
>>> continue; /* NB */
>>> default:
>>> /* do something */
>>> }
>>> }
>>>
>>> The indicated continue statement will apply to the for loop,
>>> correct?

>>
>> Yes. As C99 says, "A continue statement causes a jump to the
>> loop-continuation portion of the smallest enclosing
>> iteration statement; that is, to the end of the loop body."
>> Note that a switch statement is not an iteration statement.

>
>However it might be well to avoid the construct, both because it
>may be obscure to a future reader, and it is the sort of unusual
>construct that can harbor compiler bugs.

I don't consider the general case either unusual or obscure, and
certainly wouldn't expect a compiler to mistreat it.

--
Al Balmer
Balmer Consulting
http://www.velocityreviews.com/forums/(E-Mail Removed)