switch-statement

Evaluating a boolean variable in PHP: Should one use If/Else or Switch?

I love getting myself into these ridiculous situations where I over-evaluate my coding practices, so here goes! This question probably transcends PHP (as well as sanity). When evaluating a Boolean variable, which of these is best practice? (assume that there's too much //do stuff for a ternary) if ($bool) { // do true stuff } els...

Deal with a lot of if-else , switch

Hi! What is the best way to deal with something like this : if(key==Space) { switch(weapon) { case GUN: p->shotGun(); break; case BOW: p->shotBow(); break; } } else if(key==Enter) { //... } else if(key==Up) { //... } ...

What kind of neat use cases exist for given/when?

Perl 5.10 introduced a proper switch construct with given/when and it seems like a powerful tool. Currently however, perldoc perlsyn lacks some good examples. One case where I found it handy lately was for using it with file test operators: given (-d "foo/bar/") { when (1) { ... } # defined is wrong as -d returns '' on a file. ...

How to use switch/case (smiple pattern matching) in Scala?

I've found myself stuck on a very trivial thing :-] I've got an enum: object Eny extends Enumeration { type Eny = Value val FOO, BAR, WOOZLE, DOOZLE = Value } In a code I have to convert it conditionally to a number (varianr-number correspondence differs on context). I write: val en = BAR val num = en match { case FOO => ...

php switch case problem

I am trying to say $level > -100 && $level < 100 $level = 0; switch($level){ case $level > -100: break; ...

how to take the text in an array which come from a resource ?

this is a part of my code : public void onItemClick(AdapterView<?> parent, View v, int position, long id) { TextView title_t = new TextView(this); title_t.setText(""); String title = title_t.getText().toString(); switch(position){ case 0 : new AlertDialog.Builder(this).setTitle(title).setMessage("blah blah").setNeutralButt...

Switch-Case Operator -- C

Is there a way to store user inputs in switch case from one operation and use it across switch operations at run-time. Example: If its a software for a Bank and I want to take information from the user and validate if his a/c number is correct and also check if he has enough bank balance to withdraw money. I need to know how to store t...

C# Switch statement with/without curly brackets.... what's the difference?

Has C# always permitted you to omit curly brackets inside a switch() statement between the case: statements? What is the effect of omitting them, as javascript programmers often do? Example: switch(x) { case OneWay: { // <---- Omit this entire line int y = 123; FindYou(ref y); break; ...

Large Switch Statements And Efficiency

I'm taking a day off banging my head against memory management and opengl-es to try and boost efficiency. Part of the app we're working on accesses vast tables of data. These data were supplied to us in the form of switch statements, mostly. In all, I have four switch statements, each with 2000+ cases, expected to grow. How poor is the...

CFScript switch statement throws error when passed a string?

Update: Thanks Ben, I decided to copy the URL to another structure and modify that one with StructUpdate(). Here's the code if anyone's interested (specific to my application, but you can edit the lines with comments to get a useful function). function rebuildURL(key, value) { var URLstring = ""; var VarCount = 0; var tmpURL = dupli...

Shortcut in Visual Studio 2010 to generate all possible Case's in Switch.

Hello, I can't find key binding to expand all possible cases in switch, eg. have switch with enum argument, in Borland C++ i doing this with [TAB] key while switch code is selected. I want to do this in Visual Studio 2010. Can anyone help me? ...

Can't get methods in a Class to work.

static private function removeAccentedLetters($input){ for ($i = 0; $i < strlen($input); $i++) { $input[$i]=self::simplify($input[$i]); } return $input; } static private function simplify($in){ switch ($in) { case 'Á': return 'A'; case 'á': return 'a'; default: return $in; ...

Java style: Variable declaration in a switch

The following code does not compile because eater is defined twice: switch (vegetable) { case TOMATO: Eater eater = new Eater(Tomato.class, many parameters); eater.eat(more parameters); return true; case POTATO: Eater eater = new Eater(Potato.class, many parameters); eater.eat(more parame...

Switch-Case: declaration-with-initialization & declaration-and-then-assignment

In the switch-case statements declaration-with-initialization is invalid but declaration-and-then-assignment is allowed. As shown in the following code snippet. What is difference between these two type of initializations from the compiler side? And why is the first type of initialization invalid and second type a valid one. switch(va...

Drupal hook_user switch for registration and submit

I need to send registration details somewhere else when Drupal completes a registration. I can't find the correct variables for the switch. I tried this but it's not working. This function is in my module "externalnewsletter". function externalnewsletter_user($op, &$edit, &$account, $category = NULL) { if ($op == 'register' && $cate...

c/c++ programming

main() { int i=0; for(i=0;i<20;i++) { switch(i) case 0: i+=5; case 1: i+=2; case 5: i+=5; default i+=4; break; } printf("%d,",i); } } this is frm aptitude question....plz explain hw this works??? ...

Python "switch statement" and string formatting

Hello guys, I'm trying to do switch statement (with dictionary) and the answer needs to be a formatted string, so for example: descriptions = { 'player_joined_clan': "%(player)s joined clan %(clan)s." % {"player": token1, "clan": token2}, #etc... } Now, this would work if those both tokens were always defined, which is not the...

ActionScript - Using "is" comparative in switch statement?

i have many objects of the same custom class, and another many objects of another custom class. i would like to create a switch statement to determine from which of the classes the object belongs. the following code doesn't compile, so i'm not sure if this is possible. is the only alternative to use if statements? function mouseClick...

Switch error:: cannot appear in a constant-expression

Hi Guys - this is a strange one... I am playing with some decompression algo. Instead of going through the char buffer[] and looping until a stop-bit in buffer[i] is found, I am trying use some bit-mask techniques but with chars. I have the following example: // In a *.h file const char ch = '\x81'; // To avoid Endianess unio...

Switch statement with multiple constant-expression in c#. Is it possible?

Possible Duplicate: Multiple Cases in Switch: Is it possible to do a multiple constant-expression switch statement like switch (i) { case "run","notrun", "runfaster": //Something like this. DoRun(); break; case "save": DoSave(); break; default: InvalidCommand(command); break; } ...