switch (n % 2) {
case 0:
do {
++i;
case 1:
++i;
} while (--n > 0);
}
Someone is really ought to record a "WAT" video about C. switch (n % 2) {
case 0:
do {
++i;
case 1:
++i;
} while (--n > 0);
}
Someone is really ought to record a "WAT" video about C.As for the footgun issue, I mean, it could be one in theory, sure. But I don’t think I’ve ever seen it actually fired. And I can’t really appreciate the Javaesque “abuse” thinking—it is to some extent the job of the language designer to prevent the programmer from accidentally doing something bad, but I don’t see how it is their job to prevent a programmer from deliberately doing strange things, as long as the result looks appropriately strange as well.
(There are reasons to dislike C’s switch statement, I just don’t think the potential for “abuse” is one.)
The relevant example code is;
int function(void) {
static int i, state = 0;
switch (state) {
case 0: goto LABEL0;
case 1: goto LABEL1;
}
LABEL0: /* start of function */
for (i = 0; i < 10; i++) {
state = 1; /* so we will come back to LABEL1 */
return i;
LABEL1:; /* resume control straight after the return */
}
}
becomes; int function(void) {
static int i, state = 0;
switch (state) {
case 0: /* start of function */
for (i = 0; i < 10; i++) {
state = 1; /* so we will come back to "case 1"*/
return i;
case 1:; /* resume control straight after the return */
}
}
}