Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Switch statement without break (http://www.velocityreviews.com/forums/t439724-switch-statement-without-break.html)

Colin King 10-09-2005 02:05 PM

Switch statement without break
 

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;
}
}


Skarmander 10-09-2005 02:30 PM

Re: Switch statement without break
 
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.

Colin King 10-09-2005 09:15 PM

Re: Switch statement without break
 
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.



Eric Sosman 10-10-2005 12:34 PM

Re: Switch statement without break
 
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@sun.com

Richard Tobin 10-10-2005 12:59 PM

Re: Switch statement without break
 
In article <pan.2005.10.09.21.18.40.438851@eurobell.co.uk>,
Colin King <kings@eurobell.co.uk> 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

Skarmander 10-10-2005 01:24 PM

Re: Switch statement without break
 
Richard Tobin wrote:
> In article <pan.2005.10.09.21.18.40.438851@eurobell.co.uk>,
> Colin King <kings@eurobell.co.uk> 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.

Richard Tobin 10-10-2005 02:52 PM

Re: Switch statement without break
 
In article <434a6b9a$0$11068$e4fe514c@news.xs4all.nl>,
Skarmander <invalid@dontmailme.com> 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

Skarmander 10-10-2005 03:04 PM

Re: Switch statement without break
 
Richard Tobin wrote:
> In article <434a6b9a$0$11068$e4fe514c@news.xs4all.nl>,
> Skarmander <invalid@dontmailme.com> 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.


All times are GMT. The time now is 07:04 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.