Yes, Mark, it does. C works in mysterious ways, and this is just one
in a long, possibly endless, procession of them. Duff's device is one
of those hallowed "WTF? Oh.. Ooooh! I get it! Whoever cooked this up
is an evil genius!!!" optimizations useful in the early days of
computing. The wikipedia page has a lengthy treatment on the topic,
but the basic mechanics behind why this is legal in C is this:

 1. A switch statement's body is simply like any other block, with as
any extra that you can shove any number of case statements in it.
 2. switch works by jumping to the right case statement.
 3. In C, you may jump right into the middle of a loop, without
breaking things.

Most folks would say that in java you can only jump out of LOOPS,
which explains why it doesn't work, but this isn't true. It doesn't
work in java because, switch blocks are defined as containing a 0-n
set of (case/default statement followed by 0-n statements), whereas in
C its defined as containing 0-n statements, and case/default are also
statements. You CAN in fact jump around indiscriminately in java code
and it works just fine. Behold this code:

foo: {
    System.out.print("Hello, ");
    if (0 < System.currentTimeMillis()) break foo;
    System.out.println("World!");
    System.exit(0);
}

System.out.println("Universe!");

which will print "Hello, Universe!" if your system clock is set later
than Jan 1st, 1970, and causes no compilation problems at all. This
indiscriminate breaking out of any code block, not just those
representing loops, is in fact a holdover from C, where 'break' in a
switch statement, which was after all defined just as a glorified goto
form, required this. In java this isn't necessary (switch being
differently defined), but nevertheless you can break out of whatever
you want, loop or no.

Which, I guess, is my entry for 'strange loop': It's so strange, one
could argue it's not a loop at all!

NB: I don't need a ticket, I can't make the date, but I couldn't
resist :P


On Aug 4, 3:23 am, Mark Derricutt <m...@talios.com> wrote:
> Does this even compile?
>
> As you have the do {} inside the first case statement, and all other case's
> are inside the do.     Since there's not in scope of the case I'd expect
> this to fail?   But then - I'm no C/C++ guy so who knows what wacky hackery
> they get up to :)
>
> --
> Pull me down under...
>
> On Wed, Aug 4, 2010 at 12:25 PM, Kevin Wright <kev.lee.wri...@gmail.com>wrote:
>
>
>
> > For wacky loop madness, it doesn't get much stranger than loop unrolling in
> > C/C++
> > So I present to you... <drum roll> ...Duff's Device!
> > (lifted direct from Wikipedia)
>
> > send(to, from, count)register short *to, *from;register count;{
> >    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);
> >    }}
>
> > Such elegance!
> > Such conciseness!
>
> > Why oh why did I ever allow Scala, with it's complexity and ugly, ugly
> > functional constructs to enter my life?
> > It's nothing but a virtuous life of clean, simple,
> > comprehensible imperative code for me from now on!
>
> > On 4 August 2010 01:09, Vince O'Sullivan <vjosulli...@gmail.com> wrote:
>
> >> for (Strange loop : loops)
> >>    if (loop.isStrange())
> >>        continue;
>
> >> On Aug 3, 6:23 pm, Alex <alexdmil...@yahoo.com> wrote:
> >> > The deadline for the contest will be Aug. 31st!  Give us your loops!
>
> >> > Alex
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "The Java Posse" group.
> >> To post to this group, send email to javapo...@googlegroups.com.
> >> To unsubscribe from this group, send email to
> >> javaposse+unsubscr...@googlegroups.com<javaposse%2bunsubscr...@googlegroups
> >>  .com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/javaposse?hl=en.
>
> > --
> > Kevin Wright
>
> > mail/google talk: kev.lee.wri...@gmail.com
> > wave: kev.lee.wri...@googlewave.com
> > skype: kev.lee.wright
> > twitter: @thecoda
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "The Java Posse" group.
> > To post to this group, send email to javapo...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > javaposse+unsubscr...@googlegroups.com<javaposse%2bunsubscr...@googlegroups 
> > .com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/javaposse?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to javapo...@googlegroups.com.
To unsubscribe from this group, send email to 
javaposse+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to