|
-
February 16th, 2005, 04:22 PM
#1
C loop around case statements
A while back, a buddy of mine happened to show me an interesting code segment that I didn't even know was valid C code. It was something like the following format:
switch(aByte)
{
while(someCondition)
{
case 0:
doSomething;
case 1:
doSomething;
case 2:
doSomething;
case 3:
doSomething;
case 4:
doSomething;
case 5:
doSomething;
case 6:
doSomething;
case 7:
doSomething;
}
}
According to a web site I found back then, the developer who came up with such a screwy code structure worked for some graphics company, and the purpose of the particular segment was a very efficient way to copy graphics data from one place to another. It also stated that there was a question among the ANSI committee whether it SHOULD be legal C (apperently, the compiler the writer used did compile this code), and was finally determined that it is "valid" code.
I, unfortunately, can't find anything about this in any of my web searches. I do remember that the code was called "<Something's> Machine" (I think named after the original author.......).
Does anyone have any idea what this was actually called, and where I can get information on it?
-
February 16th, 2005, 04:42 PM
#2
Re: C loop around case statements
It looks like a possible way to implement a finite state machine or Turing machine in C (where aByte would be an indicator of the current state). I don't know about finding anything about the original code, but that might give you somewhere to start to look up the idea behind it.
--EJMW
-
February 16th, 2005, 04:50 PM
#3
Re: C loop around case statements
Not many people would ever think that it is valid code:
Code:
cin >> number;
switch (number)
{
case 4:
cout << "Value OK!" << endl;
break;
default:
cout << "Wrong value!" << endl;
if (number>10)
{
cout << "Not only wrong, but also too much!" << endl;
break;
case 13:
cout << "Bad luck! You jumped inside of IF!" << endl;
}//if
}//switch
Everything becomes clear when you realize, that case ## is nothing else but label, which an be put anywhere inside of switch(#){...}.
Switch is a block, and it can contain other blocks, and labels can be placed inside of those internal blocks.
However, code you showed above is the first case I have ever seen use of this feature. For me it is as legible as using gotos.
B+!
'There is no cat' - A. Einstein
Use [code] [/code] tags!
Did YOU share your photo with us at CG Members photo gallery ?
-
February 16th, 2005, 04:54 PM
#4
Re: C loop around case statements
-
February 16th, 2005, 06:22 PM
#5
Re: C loop around case statements
I've actually seen this referenced in a few different books and websites on optimization. It's screwy, but legal. It does have a well-known name, but I am drawing a blank right now. If I find it, I'll post it.
Kevin Hall
-
February 16th, 2005, 06:25 PM
#6
Re: C loop around case statements
 Originally Posted by KevinHall
I've actually seen this referenced in a few different books and websites on optimization. It's screwy, but legal. It does have a well-known name, but I am drawing a blank right now. If I find it, I'll post it.
Duff's device maybe?
Regards,
Paul McKenzie
-
February 16th, 2005, 06:30 PM
#7
Re: C loop around case statements
It's called "Duff's Device" and here is a copy of a 1988 e-mail Tom Duff wrote on his trick. You'll also find lots of information about Duff's Device via Google.
Kevin Hall
-
February 16th, 2005, 06:34 PM
#8
Re: C loop around case statements
Paul, you beat me to it!
Kevin Hall
-
February 17th, 2005, 10:25 AM
#9
Re: C loop around case statements
YES! That's it, thanks much guys!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|