switch-case

switch case vs if else

I was wondering if there was any difference in the way the following code was compiled into assembly. I've heard that switch-case is more efficient than if else, but in this example I am not quite sure if that would be the case. if(x==1){ ... }else if(x==2){ ... }else{ ... } and switch(x){ case 1: ... break; case 2: ... break;...

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; ...

What is the best/easiest way to use nested switch/case statements?

What is the better practice of the following two switch/case statements? Is there an easier way (less code) to do this? switch (myValue) { case 1: { methodFor1(); break; } case 2: case 3: { methodFor2or3(); if (myValue == 2) methodFor2(); if (myValue == ...

what is the alternate way of doing function of switch-case (and if-else) in c?

what is the alternate way of doing function of switch-case (and if-else) in c? ...

Working on php switch-case logic

I am working on accessing a db table that will read through a text entry to find a string...then based on that string, create a new variable. Here's the source: <?php $haystack = "Additional Licenses: +2 Licenses /br/ Back-up CD-ROM: No"; $needle = "+0"; switch ($needle) { case '+1': if (strstr($haystack, ...

referencing each case of a switch from a conditional inside a different method in java

Hello, I am implementing some methods which use switch statements to distinguish between different cases: private void doThis(){ switch(command){ case on: {status = doCalculationsA; break;} case off: {status = doCalculationsB; break;} case idle: {status = doCalculationsC; break;} case stdby:{status = doCalculationsD;...

return variable approach

Ok I have ten variables but one needs to be set, but I need to check for each of the values until one is set. I'm doing a SWITCH/CASE statement but I'm not sure if this is the best approach because I don't see how I can return only the variable that I need set. $passed_var = 'A'; // Static $var_array = getSelectedVar($passed_var); for...

C# switch in lambda expression

Hello, Is it possible to ha ve a switch in a lambda expression ? IF not, why ? Resharper display it as an error. ...

Random AI / Switch case?

So I have a very simple game going here..., right now the AI is nearly perfect and I want it to make mistakes every now and then. The only way the player can win is if I slow the computer down to a mind numbingly easy level. My logic is having a switch case statement like this: int number = randomNumber case 1: computer moves the comp...

jQuery Switch Case Plugin?

I know the switch case statement is inherent to javascript and you can't change it. I'm still learning javascript and jQuery so I can get by, but I don't know enough to write something that might be on the level of jQuery itself. So, take this as an idea or a question about if this idea is feasible. This is my idea, a switch case statem...

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...

Why does this program not output 20?

#include<stdio.h> int main() { int a = 1; switch (a) { int b = 20; case 1: { printf("b is %d\n", b); break; } default: { printf("b is %d\n", b); break; } } return 0; } ...

Are switch case statements flexible?

Hi I have created the following code so when a user chooses a department (Sales, Billing, Marketing, Finance, etc) and uses the contact form I created, the e-mail is sent to the proper person in the company. <?php $ip = $_POST['ip']; $httpref = $_POST['httpref']; $httpagent = $_POST['httpagent']; $visitor = $_POST['visitor']; $visito...

Threaded State Machines in Java

Is there a way of Holding a thread in a State waiting for changes? I mean wait tll something happend (change var, call method, etc..) perhaps it needs using Event Listeners, or synchronized objects/methods.. The usual aproach for statemachines like this statemachine example That uses a do{..}while(true) loop that could work for singl...

Java Enums - Switch statements vs Visitor Pattern on Enums - Performance benefits?

I have been searching around for days to find an answer to this performance based issue. After digging the Internet so far I have learned that there are couple of ways to use the Enums in java, well documented in here. Well, definitely as a starter one would like use Enums in a switch-case statement, which provides clarity and better un...

How would I do an OR statement with a Switch case? (PHP)

How would I go about converting this if statement: for($i = 1; $i < $argc; $i++) { ... if(in_array($argv[$i], array('-V', '--version'))) { $displayVersion = TRUE; } ... } Into a switch case without needing to write two switch statements? ...

Discrepancy in switch statement results

function skyCoverage( $metarClouds ) { foreach( $metarClouds[0] as $cloudReport ) { $coverageCode = substr( $cloudReport, 0, 3 ); // I check $coverageCode here, and it is indeed "CLR" switch( $coverageCode ) { case "CLR": $cloudCoverage = 0; break;...

How do I replace a switch statement with IOC so I can maintain SOLID principle

I wanted to avoid the switch statement. I have over 30 document types. There is also a possibility I will need to add more document types moving forward. I would rather pass IDocument and have the type specified in the implementation of IDocument. Something else I forgot to mention was ProgressNoteViewModel, LabViewModel ... all inhe...

How to make the C# Switch Statement use IgnoreCase

If I have a switch-case statement where the object in the switch is string, is it possible to do anyway ignoreCase compare? I have for instance: string s = "house"; switch (s) { case "houSe": s = "window"; } Will s get value "window". How to override the switch-case statement so it will compare the strings using ignoreCase? ...

create an object in switch-case

hello, i use visual studi 2008. (c++) in my switch case a wanted to create an object, but i doens't work. is it right, that i can't create an object in a switch case? if that's right,whats the best way to work around it, a new method that's creates that object? edit the code: switch (causwahl){ case '1': cAccount *oAccount = new cA...