As a disclaimer, this is a homework problem. But it's one where the answer can't be found from our lecture notes and we're encouraged to find the answer through research (on the internet I presume). We're given the following code fragment, and asked for the technical name for this particular "peculiar" use of switch statement (this is in C++)
switch (x) {
case 0:
if ( m > n ) {
case 1:
for ( o = 0; o < 10; o += 1 ){
case 2:
p += 1;
}
}
}
where x, m, n, o, and p are int
I've answered all of the questions given about how the code operates under different conditions, but I can not find this mysterious technical name for this kind of switch statement. I've tried a few creative google searches, and read several pages about switch statement, but can't find mention of a case like this where if and for are nested within. Can anyone point me in the right direction??
A famous technique that is closely related to this is known as "Duff's Device". The Wikipedia page has a fairly detailed discussion that includes the following passage:
C's default fall-through in case statements has long been one of its most controversial features; Duff observed that "This code forms some sort of argument in that debate, but I'm not sure whether it's for or against."
I don't know if I've ever seen or heard of anything quite this twisted, but I wonder if your prof wasn't thinking of Duff's device. The original version was:
register n=(count+7)/8;
switch(count%8){
case 0: do{ *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
}while(--n>0);
}
(to
pointed to a memory mapped IO register.) To quote Tom Duff (the
inventor), "I feel a combination of pride and revulsion at this
discovery," and "Many people (even bwk?) have said that the worst
feature of C is that switches don't break automatically before each case
label. This code forms some sort of argument in that debate, but I'm
not sure whether it's for or against."
Many, many years ago (about the time Tom Duff invented this), I came up with something along the lines of:
switch ( category[*p] ) {
// ...
case CH_DOT:
if ( category[*(p + 1)] == CH_DIGIT )
case CH_DIGIT:
p = parseNumber( p );
else
case CH_PUNCT:
p = parsePunct( p );
break;
// ...
}
I never gave it a name, however, and never let it escape into production code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With