switch-statement

Is there a reason to prefer a switch over an if statement with only one condition?

I found the following code in my team's project: Public Shared Function isRemoteDisconnectMessage(ByRef m As Message) isRemoteDisconnectMessage = False Select Case (m.Msg) Case WM_WTSSESSION_CHANGE Select Case (m.WParam.ToInt32) Case WTS_REMOTE_DISCONNECT isRemoteDisconnect...

switch statement doesn't work

how come this java switch statement keeps telling me my statements are not statements public void setConstant(float inNumGrade) { this.yourNumberGrade = inNumGrade; switch (this.yourLetterGrade) { case 'A': this.yourNumberGrade >= 0.90; break; case 'B': this.yourNumberGra...

need to create a summary of a large switch statement in C#

Alright, i dont know how to explain it well.. but i have a switch statement, string mystring = "hello"; switch(mystring) { case "hello": break; case "goodbye": break; case "example": break; } of course this is an example, and in the real situation, there will be different things happening for each case. ok, hope you get the point, now...

How can I optimize a multiple (matrix) switch / case algorithm?

Is it possible to optimize this kind of (matrix) algorithm: // | case 1 | case 2 | case 3 | // ------|--------|--------|--------| // | | | | // case a| a1 | a2 | a3 | // | | | | // case b| b1 | b2 | b3 | // | | | | // c...

Is it possible to use || in PHP switch?

switch ($foo) { case 3 || 5: bar(); break; case 2: apple(); break; } In the above code, is the first switch statement valid? I want it to call the function bar() if the value of $foo is either 3 or 5 ...

How to write SSIS switch/case expression?

Hi, This is a SQL Server Integration Services (SSIS) expressions question (I'm pretty new to it). I would like to write a switch/case expression in a Derived Column transform - basically the new column can have 5 different possible values, based on the value of an input column. All I got from Google is the (condition) ? (true value) ...

Performance difference in alternative switches in Python.

I have read a few articles around alternatives to the switch statement in Python. Mainly using dicts instead of lots of if's and elif's. However none really answer the question: is there one with better performance or efficiency? I have read a few arguments that if's and elifs would have to check each statement and becomes inefficient wi...

Which is faster - if..else or Select..case?

I have three condition to compare. Which one is more faster between the following two? Please point me out. Thanks all! If var = 1 then Command for updating database ElseIf var = 2 then Command for updating database ElseIf var = 3 then Command for updating database EndIf and Select Case var Case 1 Command for up...

How can I use goto in a switch statement in Objective-C?

In my code I need to be able to jump (goto) a different case within the same switch statement. Is there a way to do this? My code is something like this: (There is a lot of code I just left it all out) switch (viewNumber) { case 500: // [...] break; case 501: // [...] break; . . . . . case 510: // [...] break...

c# switch statement question

Hi, I'll cut to the chase. I have two questions about switch that are simple, but I can't figure them out. First: in c# switch statements, do case statements have to be consecutive (with ints)? For example: switch(someInt) { case 1 // some code case 2 // some code case 3 // some code } or is it possible t...

using switch statements with constants or enumerations? (Which is better)? C#

HI, I've got a simple question, but one that has been bugging me for a while. Question: When using switch statements in C#, is it considered better practice to use enums over constants or vice versa? Or is it a matter of preference? I ask this because many people seem to like using enums, but when you are switching on an int value, you...

PHP CASE statement not working with ZERO values.

I don't understand what's happening here. Logically, it doesn't make any sense to me. <?php $level = 0; switch ($level) { case $level > 80: $answer = 'high'; break; case $level > 60: $answer = 'moderate-to-high'; break; case $level > 40: $answer = 'moderate'; break; case $level > 20: $answer = 'low-to-moderate'; break; defau...

Issue with C function returning a gchar**

I have a function defined as the following (in C): gchar **Scan_Return_File_Tag_Field_From_Mask_Code (File_Tag *FileTag, gchar code) { switch (code) { case 't': /* Title */ return &FileTag->title; case 'a': /* Artist */ return &FileTag->artist; case 'b': /* Album */ ...

Can I use a logical "or" in a PHP switch statement case?

Is it possible to use "or" or "and" in a switch case? Here's what I'm after: case 4 || 5: echo "Hilo"; break; ...

How would you make this switch statement as fast as possible?

2009-12-04 UPDATE: For profiling results on a number of the suggestions posted here, see below! The Question Consider the following very harmless, very straightforward method, which uses a switch statement to return a defined enum value: public static MarketDataExchange GetMarketDataExchange(string ActivCode) { if (ActivCode == ...

Jump Table Switch Case question

Hi. I am trying to understand some things about jump tables and its relationship between a switch case statement. I was told that a jump table is a O(1) structure that the compiler generates which makes lookup of values essentially about as fast as you can get. However in some cases a Hashtable/Dictionary might be faster. I was also tol...

Efficient switch statement

In the following two versions of switch case, I am wondering which version is efficient. 1: string* convertToString(int i) { switch(i) { case 1: return new string("one"); case 2: return new string("two"); case 3: return new string("three"); . . default: return new...

Why can't I switch on a class with a single implicit conversion to an enum

I am wondering why it is that a single implicit conversion to an enum value doesn't work the same way it would if the conversion were to a system type. I can't see any technical reason however maybe someone smarter than I can shed some light for me. The followoing fails to compile with, "A value of an integral type expected" and "Cannot...

Why is the corresponding statement not executed even if echo empty($location) prints out 1

echo empty($location); switch($location){ case (empty($location)): expression 1; break; case ($location%10000==0): expression 2; break; case ($location%100==0): expression 3; break; default: expression 4; break; } When I echo empty($location), it prints out 1, why is expression 1 not executed? ...

How to avoid long switch ..need help refactoring

hi guys, i needed help refactoring the following class, Following is a class Operation with variety of Operations in switch : i want to avoid the switch statement.I read few articles on using polymorphism and state pattern .but when i refactor the classes i dont get access to many variables,properties Im confused on whether to use o...