switch/continue

Discussion in 'C Programming' started by Christopher Benson-Manica, Jun 7, 2004.

  1. 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.
    Christopher Benson-Manica, Jun 7, 2004
    #1
    1. Advertising

  2. Christopher Benson-Manica

    Ben Pfaff Guest

    Christopher Benson-Manica <> 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[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \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\
    );}return 0;}
    Ben Pfaff, Jun 8, 2004
    #2
    1. Advertising

  3. Christopher Benson-Manica

    CBFalconer Guest

    Ben Pfaff wrote:
    > Christopher Benson-Manica <> 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 () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Jun 8, 2004
    #3
  4. 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
    Thomas Matthews, Jun 8, 2004
    #4
  5. Christopher Benson-Manica

    Alan Balmer Guest

    On Tue, 08 Jun 2004 07:46:59 GMT, CBFalconer <>
    wrote:

    >Ben Pfaff wrote:
    >> Christopher Benson-Manica <> 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
    Alan Balmer, Jun 8, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam

    ASP to continue processing.

    Sam, Sep 22, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    1,844
    Jonathan Allen
    Sep 25, 2004
  2. v4vijayakumar

    continue with switch

    v4vijayakumar, May 12, 2006, in forum: C Programming
    Replies:
    25
    Views:
    962
    Herbert Rosenau
    May 13, 2006
  3. xz
    Replies:
    13
    Views:
    547
  4. Francois Grieu
    Replies:
    55
    Views:
    1,364
    James Kuyper
    Jul 30, 2011
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    97
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page