Is there no switch function in Python
Roy Smith
roy at panix.com
Fri Sep 10 11:16:12 EDT 2004
Isaac To <iketo2 at netscape.net> wrote:
> C switch is much more flexible than one would expect. It is not just
> a cheap replacement of a sequence of if-then-else. E.g., in an
> exercise of "The C++ Programming Language" of Bjarne Stroustrup, you
> can see the following example code:
>
> void send(int *to, int *from, int count) {
> int 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);
> }
> }
La-de-dah, Bjarne discovers loop unrolling. You don't need a switch
statement to unroll loops.
Hold on a second. Cases 1 through 7 jump into the middle of the do
loop!? Pardon me while I barf. I didn't even know that was legal.
That's the kind of code that gives C++ a bad name.
More information about the Python-list
mailing list