switch/continue

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

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?
 
B

Ben Pfaff

Christopher Benson-Manica said:
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.
 
C

CBFalconer

Ben said:
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.
 
T

Thomas Matthews

Christopher said:
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
 
A

Alan Balmer

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top