Switch statement without break

Discussion in 'C Programming' started by Colin King, Oct 9, 2005.

  1. Colin King

    Colin King Guest

    Amusingly, one can use a while(0) statement to allow one to
    perform a switch statement without breaks. The while (0)
    enables the continue statements to break out of the switch.
    Ugly and beautiful!

    void sw(int s)
    {
    switch (s) while (0) {
    case 0:
    printf("zero\n");
    continue;
    case 1:
    printf("one\n");
    continue;
    case 2:
    printf("two\n");
    continue;
    default:
    printf("something else\n");
    continue;
    }
    }
     
    Colin King, Oct 9, 2005
    #1
    1. Advertising

  2. Colin King

    Skarmander Guest

    Colin King wrote:
    > Amusingly, one can use a while(0) statement to allow one to
    > perform a switch statement without breaks. The while (0)
    > enables the continue statements to break out of the switch.
    > Ugly and beautiful!
    >

    <snip>

    The best-known instance of mixing up a switch statement with a loop is
    Duff's device. See e.g.
    http://catb.org/~esr/jargon/html/D/Duffs-device.html.

    Needless to say, constructions like these are not something you want to
    see in production code. :)

    S.
     
    Skarmander, Oct 9, 2005
    #2
    1. Advertising

  3. Colin King

    Colin King Guest

    On Sun, 09 Oct 2005 16:30:08 +0200, Skarmander wrote:

    > Colin King wrote:
    >> Amusingly, one can use a while(0) statement to allow one to
    >> perform a switch statement without breaks. The while (0)
    >> enables the continue statements to break out of the switch.
    >> Ugly and beautiful!
    >>

    > <snip>
    >
    > The best-known instance of mixing up a switch statement with a loop is
    > Duff's device. See e.g.
    > http://catb.org/~esr/jargon/html/D/Duffs-device.html.


    Indeed. It was inspired by Duff's device, however, the beauty of
    using a while loop instead of a block { } statement is an amusing
    feature which allows me to substitute breaks with continues. :)

    >
    > Needless to say, constructions like these are not something you want to
    > see in production code. :)
    >
    > S.
     
    Colin King, Oct 9, 2005
    #3
  4. Colin King

    Eric Sosman Guest

    Colin King wrote:

    > On Sun, 09 Oct 2005 16:30:08 +0200, Skarmander wrote:
    >
    >
    >>Colin King wrote:
    >>
    >>>Amusingly, one can use a while(0) statement to allow one to
    >>>perform a switch statement without breaks. The while (0)
    >>>enables the continue statements to break out of the switch.
    >>>Ugly and beautiful!

    >>
    >><snip>
    >>
    >>The best-known instance of mixing up a switch statement with a loop is
    >>Duff's device. See e.g.
    >>http://catb.org/~esr/jargon/html/D/Duffs-device.html.

    >
    > Indeed. It was inspired by Duff's device, however, the beauty of
    > using a while loop instead of a block { } statement is an amusing
    > feature which allows me to substitute breaks with continues. :)


    The same substitution is possible if you invert the
    nesting of the loop and the switch. I've actually used
    the latter pattern a few times in functions to scan the
    program's argv[] command-line arguments:

    while (*argv != NULL) {
    switch (index_in_table_of(*argv++)) {
    case 0: /* -q */
    optionQ = 1;
    continue;
    case 1: /* -n value */
    if (*argv != NULL) {
    valueN = convert_to_value(*argv++);
    if (legitimate_n_value(valueN))
    continue;
    }
    break;
    ...
    }
    /* No `continue' was executed: bad argument */
    fprintf(stderr, "You blockhead!\n");
    return FAILURE;
    }
    return SUCCESS;

    --
     
    Eric Sosman, Oct 10, 2005
    #4
  5. In article <>,
    Colin King <> wrote:

    >Indeed. It was inspired by Duff's device, however, the beauty of
    >using a while loop instead of a block { } statement is an amusing
    >feature which allows me to substitute breaks with continues. :)


    It also allows you to break out of either of two nested switch
    statements without a goto:

    #include <stdio.h>

    int main(int argc, char **argv)
    {
    int a = atoi(argv[1]), b = atoi(argv[2]);

    switch(a) while(0)
    {
    case 1:
    printf("case 1 of outer switch\n");
    break;
    case 2:
    printf("case 2 of outer switch\n");
    switch(b)
    {
    case 1:
    printf("case 1 of inner switch\n");
    break;
    case 2:
    printf("case 2 of inner switch\n");
    continue;
    }
    printf("end of inner switch\n");
    break;
    }
    printf("end of outer switch\n");

    return 0;
    }

    -- Richard
     
    Richard Tobin, Oct 10, 2005
    #5
  6. Colin King

    Skarmander Guest

    Richard Tobin wrote:
    > In article <>,
    > Colin King <> wrote:
    >
    >
    >>Indeed. It was inspired by Duff's device, however, the beauty of
    >>using a while loop instead of a block { } statement is an amusing
    >>feature which allows me to substitute breaks with continues. :)

    >
    >
    > It also allows you to break out of either of two nested switch
    > statements without a goto:
    >
    > #include <stdio.h>
    >
    > int main(int argc, char **argv)
    > {
    > int a = atoi(argv[1]), b = atoi(argv[2]);
    >
    > switch(a) while(0)
    > {
    > case 1:
    > printf("case 1 of outer switch\n");
    > break;
    > case 2:
    > printf("case 2 of outer switch\n");
    > switch(b)
    > {
    > case 1:
    > printf("case 1 of inner switch\n");
    > break;
    > case 2:
    > printf("case 2 of inner switch\n");
    > continue;
    > }
    > printf("end of inner switch\n");
    > break;
    > }
    > printf("end of outer switch\n");
    >
    > return 0;
    > }
    >

    Ugh. I would *definitely* use a goto here, or if at all possible, put
    the nested switch in its own function. This kind of "cryptogoto" just
    makes the program less readable and isn't one bit more structured.

    As an aside, Java's labelled breaks might not be such a bad idea.

    S.
     
    Skarmander, Oct 10, 2005
    #6
  7. In article <434a6b9a$0$11068$4all.nl>,
    Skarmander <> wrote:
    >Richard Tobin wrote:


    >> It also allows you to break out of either of two nested switch
    >> statements without a goto:
    >> [...]


    >Ugh. I would *definitely* use a goto here, or if at all possible, put
    >the nested switch in its own function. This kind of "cryptogoto" just
    >makes the program less readable and isn't one bit more structured.


    I wasn't intending my comment to be interpreted as advice on good style!

    -- Richard
     
    Richard Tobin, Oct 10, 2005
    #7
  8. Colin King

    Skarmander Guest

    Richard Tobin wrote:
    > In article <434a6b9a$0$11068$4all.nl>,
    > Skarmander <> wrote:
    >
    >>Richard Tobin wrote:

    >
    >
    >>>It also allows you to break out of either of two nested switch
    >>>statements without a goto:
    >>>[...]

    >
    >
    >>Ugh. I would *definitely* use a goto here, or if at all possible, put
    >>the nested switch in its own function. This kind of "cryptogoto" just
    >>makes the program less readable and isn't one bit more structured.

    >
    >
    > I wasn't intending my comment to be interpreted as advice on good style!
    >


    Noted. My comment still stands. :)

    S.
     
    Skarmander, Oct 10, 2005
    #8
    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. Replies:
    36
    Views:
    1,521
    Chris Uppal
    Jun 6, 2005
  2. Replies:
    16
    Views:
    3,728
    steve
    Jul 11, 2006
  3. Replies:
    21
    Views:
    1,071
    Giannis Papadopoulos
    Aug 2, 2005
  4. Replies:
    12
    Views:
    988
  5. bthumber
    Replies:
    5
    Views:
    428
    Alexey Smirnov
    Jan 29, 2009
Loading...

Share This Page