continue with switch

Discussion in 'C Programming' started by v4vijayakumar, May 12, 2006.

  1. 'continue' within switch actually associated with the outer 'while'
    loop. Is this behavior protable?

    int ch = '\n';
    while (true) {
    switch(ch) {
    case '\n': cout << "test"; continue;
    }
    }

    the above loop executed endlessly in "gcc version 2.96 20000731 (Red
    Hat Linux 7.3 2.96-110)".
    v4vijayakumar, May 12, 2006
    #1
    1. Advertising

  2. v4vijayakumar

    Richard Bos Guest

    "v4vijayakumar" <> wrote:

    > 'continue' within switch actually associated with the outer 'while'
    > loop. Is this behavior protable?
    >
    > int ch = '\n';
    > while (true) {
    > switch(ch) {
    > case '\n': cout << "test"; continue;
    > }
    > }


    I have no idea if it's portable under C++, which is what your code
    actually is. In this newsgroup we discuss ISO C, and yes, in that
    language any continue statements do not apply to a switch statement, but
    to the continue's closest surrounding loop statement, if there is any.

    Richard
    Richard Bos, May 12, 2006
    #2
    1. Advertising

  3. v4vijayakumar

    Bart Rider Guest

    v4vijayakumar wrote:
    > 'continue' within switch actually associated with the outer 'while'
    > loop. Is this behavior protable?
    >
    > int ch = '\n';
    > while (true) {
    > switch(ch) {
    > case '\n': cout << "test"; continue;
    > }
    > }
    >
    > the above loop executed endlessly in "gcc version 2.96 20000731 (Red
    > Hat Linux 7.3 2.96-110)".
    >


    try this peace of code:
    int main() {


    int ch = 'a';
    int i=0;
    while (1) {
    switch(ch) {
    case 'a': printf("i=%d, a\n",i); ch='b'; continue; break;
    case 'b': printf("i=%d, b\n",i); ch='c'; continue;
    }
    i++;

    }
    return 0;
    }

    you will see, that both printf-statements are executed. The continue
    in switch is therefore to jump to the next case block, which is
    executed *with* renewed comparison. The latter means, that if you
    replace ch='b' by ch='d' or something, then only the first printf
    is executed.
    Bart Rider, May 12, 2006
    #3
  4. v4vijayakumar

    Bart Rider Guest

    Richard Bos wrote:
    > "v4vijayakumar" <> wrote:
    >
    >
    >>'continue' within switch actually associated with the outer 'while'
    >>loop. Is this behavior protable?
    >>
    >> int ch = '\n';
    >> while (true) {
    >> switch(ch) {
    >> case '\n': cout << "test"; continue;
    >> }
    >> }

    >
    >
    > I have no idea if it's portable under C++, which is what your code
    > actually is. In this newsgroup we discuss ISO C, and yes, in that
    > language any continue statements do not apply to a switch statement, but
    > to the continue's closest surrounding loop statement, if there is any.
    >
    > Richard


    really?
    Any break inside a switch statement is actually assigned to the
    switch statement (it leaves the choice statement switch and continues
    with the first statement after switch) and not for any surrounding loop.
    The same applies to continue IMHO.
    Bart Rider, May 12, 2006
    #4
  5. Bart Rider said:

    >> I have no idea if it's portable under C++, which is what your code
    >> actually is. In this newsgroup we discuss ISO C, and yes, in that
    >> language any continue statements do not apply to a switch statement, but
    >> to the continue's closest surrounding loop statement, if there is any.
    >>
    >> Richard

    >
    > really?


    Really.

    > Any break inside a switch statement is actually assigned to the
    > switch statement (it leaves the choice statement switch and continues
    > with the first statement after switch) and not for any surrounding loop.
    > The same applies to continue IMHO.


    Really? Perhaps you'd better tell the ISO C Committee. I don't think they
    took your opinion into account when signing off the C Standard. If you run,
    you might still catch them.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, May 12, 2006
    #5
  6. v4vijayakumar

    pete Guest

    Bart Rider wrote:
    >
    > v4vijayakumar wrote:
    > > 'continue' within switch actually associated with the outer 'while'
    > > loop. Is this behavior protable?
    > >


    > > while (true) {
    > > switch(ch) {


    > > continue;


    > > }
    > > }


    You've got a continue statement inside a while loop.
    It's just as simple as that.

    >
    > try this peace of code:
    > int main() {
    >
    > int ch = 'a';
    > int i=0;
    > while (1) {
    > switch(ch) {
    > case 'a': printf("i=%d, a\n",i); ch='b'; continue; break;
    > case 'b': printf("i=%d, b\n",i); ch='c'; continue;
    > }
    > i++;
    >
    > }
    > return 0;
    > }
    >
    > you will see, that both printf-statements are executed. The continue
    > in switch is therefore to jump to the next case block, which is
    > executed *with* renewed comparison.


    That's not what happens at all.
    You put the ++i loop counter in the wrong place
    and bypassed it with the continue statement.

    i=1, a
    i=2, b




    /* BEGIN new.c */

    #include <stdio.h>

    int main(void)
    {
    int ch = 'a';
    int i = 0;

    while (1) {
    ++i;
    switch(ch) {
    case 'a': printf("i=%d, a\n",i); ch='b'; continue; break;
    case 'b': printf("i=%d, b\n",i); ch='c'; continue;
    default: return 0;
    }
    }
    return 0;
    }

    /* END new.c */


    --
    pete
    pete, May 12, 2006
    #6
  7. v4vijayakumar

    Flash Gordon Guest

    Bart Rider wrote:
    > v4vijayakumar wrote:
    >> 'continue' within switch actually associated with the outer 'while'
    >> loop. Is this behavior protable?
    >>
    >> int ch = '\n';
    >> while (true) {
    >> switch(ch) {
    >> case '\n': cout << "test"; continue;
    >> }
    >> }
    >>
    >> the above loop executed endlessly in "gcc version 2.96 20000731 (Red
    >> Hat Linux 7.3 2.96-110)".
    >>

    >
    > try this peace of code:
    > int main() {


    Better to be explicit about the lack of parameters:
    int main(void)

    > int ch = 'a';
    > int i=0;
    > while (1) {
    > switch(ch) {
    > case 'a': printf("i=%d, a\n",i); ch='b'; continue; break;


    The break above is misleading and/or useless because it will never be
    reached.

    > case 'b': printf("i=%d, b\n",i); ch='c'; continue;
    > }
    > i++;
    >
    > }
    > return 0;
    > }
    >
    > you will see, that both printf-statements are executed. The continue
    > in switch is therefore to jump to the next case block, which is
    > executed *with* renewed comparison.


    This is misleading at best. The continue is a jump to the end of the
    loop (not the beginning, important to not for do ... while loops).
    Obviously it then goes back to the start of the loop in the above code
    and executes the switch again. Where the continue goes to has absolutely
    *nothing* to do with case blocks.

    > The latter means, that if you
    > replace ch='b' by ch='d' or something, then only the first printf
    > is executed.


    Your conclusion about your code is true. Add a printf before the switch
    to see the rest is at best badly worded and in my opinion just plain wrong.

    The description of continue in section 6.8.6.2 of the latest version of
    the standard is very readable. You can find the latest draft at
    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
    Flash Gordon, May 12, 2006
    #7
  8. break/continue inside loop means
    break-the-loop/continue-next-iteration, but inside switch (as it is not
    associated with any looping) both could mean same thing, that is
    execute-the-statement-next-to-switch.

    am directly posting from google groups. can you please let me know,
    what tool people generally use to read/post to groups. TIA.
    v4vijayakumar, May 12, 2006
    #8
  9. v4vijayakumar

    pete Guest

    v4vijayakumar wrote:
    >
    > break/continue inside loop means
    > break-the-loop/continue-next-iteration,
    > but inside switch (as it is not associated with any looping)
    > both could mean same thing, that is
    > execute-the-statement-next-to-switch.


    No.

    "continue" has no meaning related to "switch"

    --
    pete
    pete, May 12, 2006
    #9
  10. v4vijayakumar said:

    > break/continue inside loop means
    > break-the-loop/continue-next-iteration, but inside switch (as it is not
    > associated with any looping) both could mean same thing, that is
    > execute-the-statement-next-to-switch.


    No, that is incorrect. A break will break out of the immediately enclosing
    switch or loop, it's true - but continue has nothing to do with switch. It
    is purely a loop construct.

    > am directly posting from google groups.


    Actually, you are /indirectly/ posting from Google Groups. What happens is
    that you compose your message, and post it to a Google Web server using
    HTTP on port 80. Google then automatically transmits that article to a news
    server using NNTP on port 119. So it's actually slightly indirect.


    > can you please let me know,
    > what tool people generally use to read/post to groups. TIA.


    I use KNode. Some people use tin, trn, Free Agent, Netscape Messenger - in
    fact, any newsreader you like.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, May 12, 2006
    #10
  11. v4vijayakumar

    Flash Gordon Guest

    v4vijayakumar wrote:
    > break/continue inside loop means
    > break-the-loop/continue-next-iteration, but inside switch (as it is not
    > associated with any looping) both could mean same thing, that is
    > execute-the-statement-next-to-switch.


    No, a continue has no meaning in a switch.

    > am directly posting from google groups. can you please let me know,
    > what tool people generally use to read/post to groups. TIA.


    Almost and news client is better than Google, even Outlook Express,
    although personally I use Thundirbird, I've also used Agent and
    Sylpheed. Ask you ISP for details of your news server or search for a
    free or pay for server.

    However, when posting using Google is *is* possible to provide context,
    so please do so. See the information about Google at and linked from
    http://clc-wiki.net/wiki/Intro_to_clc
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
    Flash Gordon, May 12, 2006
    #11
  12. v4vijayakumar

    Bart Rider Guest

    pete wrote:
    > Bart Rider wrote:
    >
    >>v4vijayakumar wrote:
    >>
    >>>'continue' within switch actually associated with the outer 'while'
    >>>loop. Is this behavior protable?
    >>>

    >

    [...skip...]
    >
    > /* BEGIN new.c */
    >
    > #include <stdio.h>
    >
    > int main(void)
    > {
    > int ch = 'a';
    > int i = 0;
    >
    > while (1) {
    > ++i;
    > switch(ch) {
    > case 'a': printf("i=%d, a\n",i); ch='b'; continue; break;
    > case 'b': printf("i=%d, b\n",i); ch='c'; continue;
    > default: return 0;
    > }
    > }
    > return 0;
    > }
    >
    > /* END new.c */
    >
    >


    Was a little bit misleaded by my code.
    No I see clear. Thanks for opening my eyes.

    Bart
    Bart Rider, May 12, 2006
    #12
  13. v4vijayakumar

    CBFalconer Guest

    v4vijayakumar wrote:
    >

    .... snip ...
    >
    > am directly posting from google groups. can you please let me know,
    > what tool people generally use to read/post to groups. TIA.


    Google groups is a very poor interface to Usenet. You can use the
    methods described below in my sig. However you would be well
    advised to install a real newsreader (such as Thunderbird from
    mozilla.org) and access news directly through your ISP and a
    newsserver. Most ISPs supply a newserver.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
    CBFalconer, May 12, 2006
    #13
  14. Bart Rider wrote:
    > Richard Bos wrote:
    >> "v4vijayakumar" <> wrote:
    >>
    >>
    >>> 'continue' within switch actually associated with the outer 'while'
    >>> loop. Is this behavior protable?
    >>>
    >>> int ch = '\n';
    >>> while (true) {
    >>> switch(ch) {
    >>> case '\n': cout << "test"; continue;
    >>> }
    >>> }

    >>
    >>
    >> I have no idea if it's portable under C++, which is what your code
    >> actually is. In this newsgroup we discuss ISO C, and yes, in that
    >> language any continue statements do not apply to a switch statement, but
    >> to the continue's closest surrounding loop statement, if there is any.
    >>
    >> Richard

    >
    > really?
    > Any break inside a switch statement is actually assigned to the
    > switch statement (it leaves the choice statement switch and continues
    > with the first statement after switch) and not for any surrounding loop.
    > The same applies to continue IMHO.


    Nope. continue only applies to loops.
    void * clvrmnky(), May 12, 2006
    #14
  15. <OT> Re: continue with switch

    On 2006-05-12, CBFalconer <> wrote:
    > v4vijayakumar wrote:
    >>

    > ... snip ...
    >>
    >> am directly posting from google groups. can you please let me know,
    >> what tool people generally use to read/post to groups. TIA.

    >
    > Google groups is a very poor interface to Usenet. You can use the
    > methods described below in my sig. However you would be well
    > advised to install a real newsreader (such as Thunderbird from
    > mozilla.org) and access news directly through your ISP and a
    > newsserver. Most ISPs supply a newserver.
    >


    Just out of curiousity, have you ever had problems with Thunderbird
    locking up? Whenever a large batch of messages came in, the
    program would stop working permanently and require me to delete all
    my mail files. I was using FC5.

    Now I just use slrn, and I highly recommend it. The only issue I
    have encountered is that I need to do my own line wrapping.
    Andrew Poelstra, May 12, 2006
    #15
  16. v4vijayakumar

    CBFalconer Guest

    Re: <OT> Re: continue with switch

    Andrew Poelstra wrote:
    > On 2006-05-12, CBFalconer <> wrote:
    >> v4vijayakumar wrote:
    >>>

    >> ... snip ...
    >>>
    >>> am directly posting from google groups. can you please let me know,
    >>> what tool people generally use to read/post to groups. TIA.

    >>
    >> Google groups is a very poor interface to Usenet. You can use the
    >> methods described below in my sig. However you would be well
    >> advised to install a real newsreader (such as Thunderbird from
    >> mozilla.org) and access news directly through your ISP and a
    >> newsserver. Most ISPs supply a newserver.

    >
    > Just out of curiousity, have you ever had problems with Thunderbird
    > locking up? Whenever a large batch of messages came in, the
    > program would stop working permanently and require me to delete all
    > my mail files. I was using FC5.


    Well, I actually use Netscape 4.75 for mail and news, because I
    consider it better than T'bird for my purposes.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
    CBFalconer, May 12, 2006
    #16
  17. v4vijayakumar

    Default User Guest

    CBFalconer wrote:

    > v4vijayakumar wrote:
    > >

    > ... snip ...
    > >
    > > am directly posting from google groups. can you please let me know,
    > > what tool people generally use to read/post to groups. TIA.

    >
    > Google groups is a very poor interface to Usenet. You can use the
    > methods described below in my sig. However you would be well
    > advised to install a real newsreader (such as Thunderbird from
    > mozilla.org) and access news directly through your ISP and a
    > newsserver. Most ISPs supply a newserver.


    Many ISPs these days have dropped usenet access, notably AOL. Mostly
    dial-ups (in the US) have gotten rid of it, but some broadband
    providers. If the OP is unable to get it from the ISP, then either a
    free news service (I don't personally know of any) or a for-pay one
    will be needed. I use news.individual.net, it costs 10 euro per year,
    about $13 US.



    Brian
    Default User, May 12, 2006
    #17
  18. On 2006-05-12, Default User <> wrote:
    > CBFalconer wrote:
    >
    >> v4vijayakumar wrote:
    >> >

    >> ... snip ...
    >> >
    >> > am directly posting from google groups. can you please let me know,
    >> > what tool people generally use to read/post to groups. TIA.

    >>
    >> Google groups is a very poor interface to Usenet. You can use the
    >> methods described below in my sig. However you would be well
    >> advised to install a real newsreader (such as Thunderbird from
    >> mozilla.org) and access news directly through your ISP and a
    >> newsserver. Most ISPs supply a newserver.

    >
    > Many ISPs these days have dropped usenet access, notably AOL. Mostly
    > dial-ups (in the US) have gotten rid of it, but some broadband
    > providers. If the OP is unable to get it from the ISP, then either a
    > free news service (I don't personally know of any) or a for-pay one
    > will be needed. I use news.individual.net, it costs 10 euro per year,
    > about $13 US.
    >


    I can't imagine anyone on c.l.c who uses AOL, but I've been
    surprised before.
    Andrew Poelstra, May 12, 2006
    #18
  19. v4vijayakumar

    Flash Gordon Guest

    Default User wrote:
    > CBFalconer wrote:
    >
    >> v4vijayakumar wrote:
    >> ... snip ...
    >>> am directly posting from google groups. can you please let me know,
    >>> what tool people generally use to read/post to groups. TIA.

    >> Google groups is a very poor interface to Usenet. You can use the
    >> methods described below in my sig. However you would be well
    >> advised to install a real newsreader (such as Thunderbird from
    >> mozilla.org) and access news directly through your ISP and a
    >> newsserver. Most ISPs supply a newserver.

    >
    > Many ISPs these days have dropped usenet access, notably AOL. Mostly
    > dial-ups (in the US) have gotten rid of it, but some broadband
    > providers. If the OP is unable to get it from the ISP, then either a
    > free news service (I don't personally know of any) or a for-pay one
    > will be needed. I use news.individual.net, it costs 10 euro per year,
    > about $13 US.


    There is a news group about free news servers...

    Also I know of a couple of free servers.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc

    Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
    Flash Gordon, May 12, 2006
    #19
  20. v4vijayakumar

    tedu Guest

    Bart Rider wrote:

    > Any break inside a switch statement is actually assigned to the
    > switch statement (it leaves the choice statement switch and continues
    > with the first statement after switch) and not for any surrounding loop.
    > The same applies to continue IMHO.


    did you try it?

    void foo(int x) {
    switch (x) {
    case 0: continue;
    case 1: break;
    }
    }

    t.c: In function 'foo':
    t.c:3: error: continue statement not within a loop
    tedu, May 12, 2006
    #20
    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,842
    Jonathan Allen
    Sep 25, 2004
  2. Christopher Benson-Manica

    switch/continue

    Christopher Benson-Manica, Jun 7, 2004, in forum: C Programming
    Replies:
    4
    Views:
    13,266
    Alan Balmer
    Jun 8, 2004
  3. xz
    Replies:
    13
    Views:
    546
  4. Francois Grieu
    Replies:
    55
    Views:
    1,358
    James Kuyper
    Jul 30, 2011
  5. Switch Within A Switch

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

Share This Page