switch-statement

Dictionary or If statements, Jython

Hi Guys, I am writing a script at the moment that will grab certain information from HTML using dom4j. Since Python/Jython does not have a native switch statement I decided to use a whole bunch of if statements that call the appropriate method, like below: if type == 'extractTitle': extractTitle(dom) if type == 'extractMetaTags': ...

Array as the options for a switch statment

I remember from way back at university using a switch with 'binary search' or 'binary switch'. Something like that, My google foo is broken today. Anyway it goes down like this: You define an array of possible options (Strings usually), some magic happens, and those options in the array become the cases in the switch happens. I do rememb...

C# - Is there a better alternative than this to 'switch on type'?

Seeing as C# can't switch on a Type (which I gather wasn't added as a special case because is-a relationships mean that more than one distinct case might apply), is there a better way to simulate switching on type than this? void Foo(object o) { if (o is A) { ((A)o).Hop(); } else if (o is B) { ((B)o)....

When to use FOR-CASE (Foreach/switch in C#)?

I've found what seems to be the C# equivalent of a FOR-CASE structure in a proyect I'm working on: foreach (string param in params.Split(';')) { string[] parts = param.Split('='); string key = parts[0].Trim().ToLower(); string value = parts[1].Trim(); switch (key) { case "param1": this.param1 = value; break; ...

Is Switch (Case) always wrong?

Are there instances where switch(case) is is a good design choice (except for simplicity) over strategy or similar patterns... ...

Switch Statement With Strings in Java?

Simple question -- why can't I switch on a String in Java, and as far as anyone knows, is this functionality going to be put into a later Java version? EDIT: If someone can point me to an article, or themselves explain why I can't do this, as in, the technical way Java's switch statement works, it would be most appreciated. UPDATE: Alo...

Using elements of a constant array as cases in a switch statement

I'm attempting to map a set of key presses to a set of commands. Because I process the commands from several places, I'd like to set up a layer of abstraction between the keys and the commands so that if I change the underlying key mappings, I don't have to change very much code. My current attempt looks like this: // input.h enum LOG...

Why doesn't Python have a switch statement?

What is the reason Python doesn't have switch statement? ...

If/Else vs. Switch

I am curious to know if anyone has looked into this but what is the benefit/downside to using a Switch statement vs. an If/Else in the .NET Framework, specifically in C#. I can't imagine there being that big of a difference, or than maybe the look of your code. Would be interested to see what everyone says. ...

What's the best alternative to an out of control switch statement?

I have inherited a project that has some huge switch statement blocks, some with 20 cases, what is a good way to rewrite these? ...

Which is more efficient in Haskell; pattern matching or nested if/case statements?

I'm just curious about the efficiency of pattern matching in Haskell. What is a simple case of where pattern matching would be better than nested if/case statements and then the converse? Thanks for your help. ...

When to use If-else if-else over switch statments and vice versa

Hi I was just wondering if anyone has any tips over why you would want to use a switch block over a series if statements. Switch Statements seem to do the same thing but take longer to type. ...

C# Switch with String.IsNullOrEmpty

Is it possible to have a switch in C# which checks if the value is null or empty not "" but String.Empty? I know i can do this: switch (text) { case null: case "": break; } Is there something better, because I don't want to ...

Does the C# switch statment need a break;

Is C# true to C++, needing a break; per case:? ..Default is fall-thru - Unlike VB OR will it automatically break out of a case once found? ..Default is break - Like VB Edit: So it is a combination of both, Default is none - You have to specify either break; or goto; ...

svn:switch doesn't work with relative svn:external?

We have a subfolder that is a relative svn external (../project/subfolder). In a fresh trunk checkout it points to (...TRUNK/project/subfolder) and in a fresh branch checkout it points to (...BRANCH/branchName/project/subfolder). But if you do a switch on the project containing the subfolder, the subfolder stays pointing at TRUNK. Alt...

If vs. Switch Speed

Switch statements are typically faster than equivalent if-else-if statements (as e.g. descibed in this article) due to compiler optimizations. How does this optimization actually work? Does anyone have a good explanation? ...

Why the switch statement and not if-else?

I've been wondering this for some time now. I'm by far not a hardcore programmer, mainly small Python scripts and I've written a couple molecular dynamics simulations. For the real question: What is the point of the switch statement? Why can't you just use the if-else statement? Thanks for your answer and if this has been asked befo...

how to execute changed interface.

My project build is forked. Now that main and forked build will probably have some interface changes. I have client that tests interfaces in main build and I need to use the same client to test these changes in fork build. what is the best way to test interface changes in both with single client. One thought I'm heading is to get the...

#ifdef in switch statement bug?

I have some code that looks like this: someFunc(value) { switch(value){ case 1: case 2: case 3: #ifdef SOMEMACRO case 4: case 5: #endif return TRUE; } return FALSE; } SOMEMACRO is defined, and let's say the value is 4.. Why does case 4 and 5 get skipped and FALSE is returned ...

User mode vs supervisor mode

I have a few questions on the user-mode and supervisor-mode on Unix-like machines. What is the difference between user-mode and supervisor-mode? I know that the user processes cannot access all memory and hardware and execute all instructions. Is there more to this? What are the advantages of having different modes? What are the step...