switch-statement

How does switch compile and how optimized and fast is it?

As I found out that I can use only numerical values in C++'s switch statements, I thought that there then must be some deeper difference between it and a bunch of if-else's. Therefore I asked myself: (How) does switch differ from if-elseif-elseif in terms of runtime speed, compile time optimization and general compilation? I'm mainly ...

Eclipse: Java Enum auto-completion of switch case

Is there a CTRL+space -like way of "auto-constructing" a switch case around a given Java Enum in Eclipse? I'd like a stub with all Enum cases... EDIT: Dupe of http://stackoverflow.com/questions/868261/is-there-a-template-or-something-for-generating-a-switch-statement-for-java-enum ? :-( ...

Python comparing string against several regular expressions

I'm pretty experienced with Perl and Ruby but new to Python so I'm hoping someone can show me the Pythonic way to accomplish the following task. I want to compare several lines against multiple regular expressions and retrieve the matching group. In Ruby it would be something like this: # Revised to show variance in regex and related ...

Switch Statement C++ - error C2046: illegal case, error C2043: illegal break

#include <iostream> #include <string> using namespace std; //void multiply(int b); int main() { float total = 0; float b = 0; cout << "Enter number: " << endl; cin >> b; char TorD; cout << "Would you like to times (*), divide (/), add (+) or minus (-) this number?" << endl; cin >> TorD; switch (TorD) case '*' : { ...

What is wrong with this c# method?

I use this method to get file extension, public string ReturnExtension(string fileExtension) { switch (fileExtension) { case ".doc": case ".docx": return "application/ms-word"; } } When i compile it i got the error BaseClass.ReturnExtension(string)': not all code ...

Which is better a switch statement or if-else if-else statement?

Possible Duplicates: is else if faster than switch() case ? What is the relative performance difference of if/else versus switch statement in Java? I am just wondering if one is better (i.e. more efficient). To me the seem to be the same other than the syntax. ...

Can/Should you throw exceptions in a c# switch statement?

Hi All, I have an insert query that returns an int. Based on that int I may wish to throw an exception. Is this appropriate to do within a switch statement? switch (result) { case D_USER_NOT_FOUND: throw new ClientException(string.Format("D User Name: {0} , was not found.", dTbx.Text)); ...

Help with a simple switch statement

I need to find the value of a variable and use it to add a class to a div, based on a switch statement. For example, my variable is $link and if $link has google.com IN IT at all, I need $class to equal 'google', if $link as yahoo.com IN IT at all, $class then needs to equal 'yahoo' So, I need something like this, but I'm not sure how...

Convert the ada code to its C .

I have a piece of ada code shown below which is a simple switch case statements.Is there any better way to convert this into C. for I in 1..100 loop case I is when 100 => Dollars := Dollars + 1; when 25|50|75 => Quarters := Quarters + 1; ...

While using a switch case loop, can i use the for loop for iterating the cases?

In PHP, While using a switch case loop, can i use the for loop for iterating the cases? for example switch ..... foreach($xyz as $abc) { CASE:$abc } default; UPDATE I am fetching the value from DB, this value is name of table, by using "case" I want to execute a particular query according to the table name.. Is this possib...

Iterate through main function in C?

Here is my main function: int main(int argc, char **argv) { LoadFile(); Node *temp; char *key; switch (GetUserInput()) { case 1: temp = malloc(sizeof(Node)); printf("\nEnter the key of the new node: "); scanf("%s", temp->key); printf("\nEnter the value of the new node: "); scanf("%s", temp->value); AddNode(...

Switch statements: do you need the last break? (Javascript mainly)

When using a switch() statement, you add break; in between separate case: declarations. But what about the last one? Normally I just leave it off, but I'm wondering if this has some performance implication I'm not thinking about? I've been wondering about this for a while and don't see it asked elsewhere on Stack-O, but sorry if I m...

Switch statement usage - C

Hello, I have a thread function on Process B that contains a switch to perform certain operations based on the results of an event sent from Process A, these are stored as two elements in an array. I set the first element to the event which signals when Process A has data to send and I have the second element set to the event which ind...

Determine an elements position in a variable length grid of elements

I have a grid of a variable number of elements. Say 5 images per row and a variable number of images. I need to determine which column (for lack of a better word) each image is in... i.e. 1, 2, 3, 4 or 5. In this grid, images 1, 6, 12 and 17 would be in column 1 while 4, 9 and 15 would be in column 4. 1 2 3 4 5 6 7 8 9 10 12 ...

Better Alternative to Case Statement

I currently have a switch statement that runs around 300 odd lines. I know this is not as giant as it can get, but I'm sure there's a better way to handle this. The switch statement takes an Enum that is used to determine certain properties that pertain to logging. Right now the problem sets in that it is very easy to leave out an enume...

Switch statement for string matching in JavaScript

How do I write a swtich for the following conditional? If the url contains "foo", then settings.base_url is "bar". The following is achieving the effect required but I've a feeling this would be more manageable in a switch: var doc_location = document.location.href; var url_strip = new RegExp("http:\/\/.*\/"); var base_url = url_strip...

Dictionary with delegate or switch?

Hi, I am writing a parser, which call some functions dependent on some value. I can implement this logic with simple switch like this switch(some_val) { case 0: func0(); break; case 1: func1(); break; } or with delegates and dictionary like this delegate v...

How to make a big switch control structure with variable check values?

For example, I have a huge switch control structure with a few hundred checks. They're an animation sequence, which is numbered from 0 to n. Someone said I can't use variables with switch. What I need is something like: NSInteger step = 0; NSInteger i = 0; switch (step) { case i++: // do stuff break; case i++: // do stuff ...

iPhone OS: making a switch statement that uses string literals as comparators instead of integers

So i'd like to do this: switch (keyPath) { case @"refreshCount": //do stuff case @"timesLaunched": //do other stuff } but apparently you can only use integers as the switch quantity. Is the only way to do this parse the string into an integer identifier and then run the switch statement? like this: nsinteger n...

Converting a hardcoded switch statement into a dynamically loaded many-keys-one-value lookup

What is a good collection/container to use for basically storing the contents of a switch-statement with multiple fallthrough situations? I guess, a more accurate way is, what's a good C# solution for a "many-keys-one-value" lookup? I checked through the System.Collections namespace and its associated namespaces, but didn't find anything...