IWETHEY v. 0.3.0 | TODO
1,095 registered users | 0 active users | 0 LpH | Statistics
Login | Create New User
IWETHEY Banner

Welcome to IWETHEY!

New Re: What's the diff?
There are some things you can do with switch that you can't as easily (arguably) do with if/then:
\nswitch (foo) {\ncase 1:\n   /* something for 1 */\ncase 4:\n   /* something for 1 and 4 */\n   break;\ncase 2:\n   /* something for 2 */\ncase 3:\n   /* something for 2 and 3 */\n   break;\ndefault:\n   /* something for other things */\n}\n\nvs.\n\nif (foo == 4 || foo == 1) {\n   /* something for 1 and 4 */\n   if (foo == 1) {\n      /* something for 1 */\n   }\n} else if (foo == 2 || foo == 3) {\n   /* something for 2 and 3 */\n   if (foo == 3) {\n      /* something for 3 */\n   }\n} else {\n   /* something for everyone else */\n}\n

The switch is easier to read (IMO) but can get a bit confusing with the drop-throughs if you don't understand (or overlook) the break placements. Comments are encouraged when using that technique. ;-)

But then again, in C you can only use ints in the switch test, so if/then has the added flexibility of arbitrary tests.

Having said that, Python only has if/then as well, but IMO it's as easy to read as a switch statement because of the tabs-for-blocks paradigm:
\nif foo == 1 or foo == 4:\n    print "something for 1 and 4"\n    if foo == 1: print "something for 1"\nelif foo == 2 or foo == 3:\n    print "something for 2 and 3"\n    if foo == 3: print "something for 3"\nelse:\n    print "something else"\n\nor slightly more explicitly:\n\nif foo == 1 or foo == 4:\n    print "something for 1 and 4"\n    if foo == 1:\n        print "something for 1"\nelif foo == 2 or foo == 3:\n    print "something for 2 and 3"\n    if foo == 3:\n        print "something for 3"\nelse:\n    print "something else"\n\n


Perl has a somewhat cleaner idiom than C as well for single operation if blocks:
\nif (foo == 4 || foo == 1) {\n   print "something for 1 and 4";\n   print "something for 1" if (foo == 1);\n} else if (foo == 2 || foo == 3) {\n   print "something for 2 and 3";\n   print "something for 3" if (foo == 3);\n} else {\n   print "something for everyone else";\n}\n


My preference, of course, is for Python. There are no braces or break statements to fat-finger and scope is obvious from the indentation.
Regards,

-scott anderson

"Welcome to Rivendell, Mr. Anderson..."
New Couple more
The switch is uniquely qualified to implement a duff device.

\tsend(to, from, count)
\tregister short *to, *from;
\tregister count;
\t{
\t\tregister n=(count+7)/8;
\t\tswitch(count%8){
\t\tcase 0:\tdo{\t*to = *from++;
\t\tcase 7:\t\t*to = *from++;
\t\tcase 6:\t\t*to = *from++;
\t\tcase 5:\t\t*to = *from++;
\t\tcase 4:\t\t*to = *from++;
\t\tcase 3:\t\t*to = *from++;
\t\tcase 2:\t\t*to = *from++;
\t\tcase 1:\t\t*to = *from++;
\t\t\t}while(--n>0);
\t\t}
\t}

Apologies for the K&R but this is the exact code from Tom Duff's usenet posting. This sick and wrong hack gives partial loop unrolling when doing data moving. You can't exactly do this with if else's. Oh for the days when men were men and compilers were compilers and it was you against the awesomely slow machine struggling to coax behavior that was just barely good enough.

OK, whatever.

For completeness I thought I'd toss in the Squeak implementation of caseOf: otherwise:

A search of the stock images reveals that it is used just 3 times - two of those are in the parser.


someObject caseOf: {
[someExpression]->[someCodeToPerform].
[otherExpression]=>[otherThingToDo]. }
otherwise: [do this].


which is in fact implemented as:

caseOf: aBlockAssociationCollection otherwise: aBlock

aBlockAssociationCollection associationsDo:
[:assoc | (assoc key value = self) ifTrue: [^assoc value value]].
\t^ aBlock value




I think that it's extraordinarily important that we in computer science keep fun in computing. When it started out, it was an awful lot of fun. Of course, the paying customer got shafted every now and then, and after a while we began to take their complaints seriously. We began to feel as if we really were responsible for the successful, error-free perfect use of these machines. I don't think we are. I think we're responsible for stretching them, setting them off in new directions, and keeping fun in the house. I hope the field of computer science never loses its sense of fun. Above all, I hope we don't become missionaries. Don't feel as if you're Bible salesmen. The world has too many of those already. What you know about computing other people will learn. Don't feel as if the key to successful computing is only in your hands. What's in your hands, I think and hope, is intelligence: the ability to see the machine as more than when you were first led up to it, that you can make it more.

--Alan Perlis
New It's a kind of concealed goto.
By which I mean you can create branching code with some complex paths. Not that's necessarily bad, mind you. I've done more than enough goto-level programming where that was the most efficient way to code the algorithm that having this available can be very useful.

Although I don't do it very often, I've used drop-through enough that I would find it quite inconvenient if I couldn't do it. It's a tradeoff in programmer awareness versus flexibility and power. So I was a little surprised that Perl 6's proposed given {} blocks do not allow drop-through. I'd be curious to see what kind of workarounds and hacks are done to get around this lack.

I also didn't know Python didn't have switch. I guess it's a more controversial control structure than I realized.

Wade.

Is it enough to love
Is it enough to breathe
Somebody rip my heart out
And leave me here to bleed
 
Is it enough to die
Somebody save my life
I'd rather be Anything but Ordinary
Please

-- "Anything but Ordinary" by Avril Lavigne.

New Scott's closest, but not even he got it - my fault, though:
AdminiScott illustrates:
There are some things you can do with switch that you can't as easily (arguably) do with if/then:
\nswitch (foo) {\ncase 1:\n/* something for 1 */\ncase 4:\n/* something for 1 and 4 */\nbreak;\ncase 2:\n/* something for 2 */\ncase 3:\n/* something for 2 and 3 */\nbreak;\ndefault:\n/* something for other things */\n}\n\n[...]

The switch is easier to read (IMO) but can get a bit confusing with the drop-throughs if you don't understand (or overlook) the break placements. Comments are encouraged when using that technique. ;-)
Actually, I knew all that... I just don't happen to agree that it's any (OK, much) better.

What I meant was only that this is just (OK, almost) as bad as an if-elseif construct; that "drop-through" shit is stupid, confusing, a HUGE potenial source of errors, and just plain frigging *unnecessary*.

"Comments are encouraged", in-freaking-deed...

[Edit:] My bad, though, for writing just a terse one-liner.


   [link|mailto:MyUserId@MyISP.CountryCode|Christian R. Conrad]
(I live in Finland, and my e-mail in-box is at the Saunalahti company.)
Your lies are of Microsoftian Scale and boring to boot. Your 'depression' may be the closest you ever come to recognizing truth: you have no 'inferiority complex', you are inferior - and something inside you recognizes this. - [link|http://z.iwethey.org/forums/render/content/show?contentid=71575|Ashton Brown]
Expand Edited by CRConrad Feb. 23, 2003, 05:52:26 PM EST
New VB does some things right
VB allows more of a set-based approach. It kind of resembles:
\nselect on (foo) \ncase 1:\n   /* something for 1 */\ncase 1,4:\n   /* something for 1 and 4 */\ncase 2:\n   /* something for 2 */\ncase 2,3:\n   /* something for 2 and 3 */\ndefault:\n   /* something for other things */\nend select\n


All without those fricken "break" dealies.
________________
oop.ismad.com
New If I'm not mistaken...
Having the same option in multiple case statements is legal in VB but it only peforms the first case that is matched.
New poor substitute for Boolean expressions
Having the same option in multiple case statements is legal in VB but it only peforms the first case that is matched.

You are right. Bad example on my part.

But, if you want potentially multiple blocks to execute, then you might as well use IF blocks. I tend to shift to IF blocks for all but the simplest of case statements anyhow. They scale better in complexity.

And, I don't like BREAK statements. Too easy to forget, and they are a dumb substitute for Boolean expressions IMO. Too Goto-like.

________________
oop.ismad.com
New Well, close. But cooler is the range stuff
Select aNumber\n   Case Is < 0\n      FunctionA\n   Case 0 to 3\n      FunctionB\n   Case 4, 15, 32, 78\n      FunctionC\n   Case Else\n      FunctionD\nEnd Select

Many fears are born of stupidity and ignorance -
Which you should be feeding with rumour and generalisation.
BOfH, 2002 "Episode" 10
New Fugly syntax kind'a repeats itself: YTF "select" AND "case"?
Our residentnoncapitalisedpoet claims VB's "range" case construct is so cool:
Select aNumber\nCase Is < 0\nFunctionA\nCase 0 to 3\nFunctionB\nCase 4, 15, 32, 78\nFunctionC\nCase Else\nFunctionD\nEnd Select
Just so nobody labours under the misapprehension that this was a VB "innovation" or anything... Pascal has had that since 197x!

The ony difference being, the Pascal syntax is (as always) *much* cleaner and more elegant than VB's:
Case AnInteger of\n  -MAXINT .. -1  : ProcedureA;\n        0 ..  3  : ProcedureB;\n   4, 15, 32, 78 : ProcedureC;\n            Else   ProcedureD;\nEnd;  {Case AnInteger}
Behold the genius of Wirth in action: One construct, one keyword!


   [link|mailto:MyUserId@MyISP.CountryCode|Christian R. Conrad]
(I live in Finland, and my e-mail in-box is at the Saunalahti company.)
Your lies are of Microsoftian Scale and boring to boot. Your 'depression' may be the closest you ever come to recognizing truth: you have no 'inferiority complex', you are inferior - and something inside you recognizes this. - [link|http://z.iwethey.org/forums/render/content/show?contentid=71575|Ashton Brown]
New Not trying to imply invention
Just pointing out:

1) tablizer's "VB" didn't look like any VB I've ever seen, and
2) I didn't think he pointed out anything really useful. So I thought I would.

We now return you to your regularly scheduled language war.

Many fears are born of stupidity and ignorance -
Which you should be feeding with rumour and generalisation.
BOfH, 2002 "Episode" 10
New Pascal was invented before Visual BASIC
I remember using it in Turbo Pascal 2.0 before VB came out. Thanks for the reference.

In Pascal, Semi-Colons are your friends. :)


[link|http://pub75.ezboard.com/bantiiwethey|
New and improved, Chicken Delvits!]
     Programming algorithms, switch() and Perl. - (static) - (25)
         well its not perl - (boxley) - (1)
             That's a point. - (static)
         What's the diff? If it needs a "break", it's just as stupid! -NT - (CRConrad) - (13)
             The difference is ... - (drewk) - (1)
                 Right, slight assembler efficiency gain. -NT - (admin)
             Re: What's the diff? - (admin) - (10)
                 Couple more - (tuberculosis)
                 It's a kind of concealed goto. - (static)
                 Scott's closest, but not even he got it - my fault, though: - (CRConrad)
                 VB does some things right - (tablizer) - (6)
                     If I'm not mistaken... - (ChrisR) - (1)
                         poor substitute for Boolean expressions - (tablizer)
                     Well, close. But cooler is the range stuff - (tseliot) - (3)
                         Fugly syntax kind'a repeats itself: YTF "select" AND "case"? - (CRConrad) - (2)
                             Not trying to imply invention - (tseliot)
                             Pascal was invented before Visual BASIC - (orion)
         One more thing: - (jb4) - (2)
             Constant vs an expression - (ben_tilly) - (1)
                 That's sorta what I figger... -NT - (jb4)
         Comments and thoughts... - (Simon_Jester) - (5)
             Along similar lines... - (ChrisR) - (1)
                 Multimorphism - (tablizer)
             Sometimes that's overkill. - (static)
             $foo->{$key} - (admin)
             You are probably thinking of - (ben_tilly)

I hope you don't mind if I don't read this. I don't know if it's going where I don't want it to be going, but if it is going there, I *don't* want to go there, so the best place for me to be is here, not there, and here is where I'm staying.
*puts on blinders*
*whistles very loudly*
*claws out own eyes*
225 ms