473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

Nov 15 '05 #1
7 4034
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.
Nov 15 '05 #2
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.


Nov 15 '05 #3
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;

--
Er*********@sun.com
Nov 15 '05 #4
In article <pa****************************@eurobell.co.uk>,
Colin King <ki***@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
Nov 15 '05 #5
Richard Tobin wrote:
In article <pa****************************@eurobell.co.uk>,
Colin King <ki***@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.
Nov 15 '05 #6
In article <43***********************@news.xs4all.nl>,
Skarmander <in*****@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
Nov 15 '05 #7
Richard Tobin wrote:
In article <43***********************@news.xs4all.nl>,
Skarmander <in*****@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.
Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
14
by: Chris | last post by:
Hi, can you specify a range in a switch - statement ? switch (i) { case 100 - 999 : // do something break; case 1000 - 9999:
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
19
by: rdavis7408 | last post by:
Hello, I have four textboxes that the user enters the price per gallon paid at the pump, the mileage per gallon and I would like to then calculate the cost per gallon and use a switch statement to...
5
by: mark4asp | last post by:
Every time the function below is called I get the alert. So I put a deliberate error in there and I check the value of (reportType=='MANDATE') in Firebug, which is found to be true. But still the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.