switch-statement

using switch in strings

Trying to use switch in strings by first coverting string into char and then apply switch but still didnt done it....here is my code.. import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import javax.swing.JOptionPane; class HappyBirthday { public static void main(String[] args) throws IOE...

Is using decimal ranges in a switch impossible in C#?

I'm just starting out learning C# and I've become stuck at something very basic. For my first "app" I thought I'd go for something simple, so I decided for a BMI calculator. The BMI is calculated into a decimal type which I'm now trying to use in a switch statement, but aparently decimal can't be used in a switch? What would be the C...

Weird switch behavior in .NET 4

I have a problem understanding what's causes the compilation error in the code below: static class Program { static void Main() { dynamic x = ""; var test = foo(x); if (test == "test") { Console.WriteLine(test); } switch (test) { case "test": ...

using a switch statement to see which 'bin' a number falls into

lets say i have a bunch of bins, each of which holds a range of numbers. an example is: bin 1: 0-8 bin 2: 9-16 bin 3: 17-24 etc is there a way to use a switch statement to figure out which bin a number belongs to? i'm trying to figure out how to make cases reflect that a number is within a range, but they don't seem to be 'constant ex...

PHP switch case problem

hey folks, I have a session variable called loggedon. I set it to 1 when the user is logged on and 0 when they are logged off. I do a swtich statement as seen below. but it isn't working as it should. $state = $_SESSION['loggedon']; switch ($state) { case 0: include("../includes/login.php"); b...

HashMap and case construct

Hi! For readability reasons I'm trying to avoid using Char based case constructs, using Java 6. I cannot switch to 7 jet... Map<String, String> map = new HashMap<String, String>() { { put("foo", "--foo"); put("bar), "--bar"); ... } private static final long serialVersionUID = 1L; // java pr...

How to make switch case accept multiple data types in Java?

While using Java's switch case, it excepts only char and int, but I want to provide string cases. How to make this possible? ...

Is there programming language with better approach for switch's break statements ?

It's the same syntax in a way too many languages: switch (someValue) { case OPTION_ONE: case OPTION_LIKE_ONE: case OPTION_ONE_SIMILAR: doSomeStuff1(); break; // EXIT the switch case OPTION_TWO_WITH_PRE_ACTION: doPreActionStuff2(); // the default is to CONTINUE to next case case OPTION_TWO: doSomeStuff2()...

When are references declared in a switch statement?

To my surprise this code works fine: int i = 2; switch(i) { case 1: String myString = "foo"; break; case 2: myString = "poo"; System.out.println(myString); } But the String reference should never be declared? Could it be that all variables under every case always are declared no matter what, or how is this resolved? ...

Closest cocoa equivalent of enum

Is there a Cocoa class has similar functionality to enumerated values from C? I know that I can just use enums in Cocoa, but what if I want to put an enum in an NSArray (which only accepts objects)? ...

C switch statement: has default to be the last case?

Consider the following switch statement: switch( value ) { case 1: return 1; default: value++; // fall-through case 2: return value * 2; } This code compiles, but is it valid (= defined behaviour) for C90/C99? I have never seen code where the default case is not the last case. EDIT: As Jon Cage and KillianDS wri...

Appropriate use of Multi-level Break in Java

I was recently coding a small java program (as design for an 8086 Assembler program) and I wound up in an interesting position -- I needed to exit out of a while loop from an inner switch-statement, something like this (pseudocode, obviously): :MyLoop While(foo) switch (bar) case '1': print '1'; break case '0': print '...

how to combine switch and if else statements

I'm taking an online java class and the teacher has asked for the following: Write a menu program that ask a user for a number indicating the four basic math equations(addition, subtraction, multiplication, division). Using a if/else structure to do the required operation, ask the user for inputs and solve the equation. I am new to this,...

Any weird purpose of switch / default in this code?

Hi, I am porting some code from C to C++ and I found this code: if(ErrorCode >= SOME_CONSTANT) { Status = RETVAL_OK; switch ( ErrorCode ) { default: Status = RETVAL_FAILED; break; } } This code generates a compilation warning: warning C4065: switch statement contains 'default' but no ...

xcode... switching to a new view display an image which is randomly selected... iphone

when i switch to a new view instantaneously a random image is selected via the arc4random function and displayed in the view can some one give me direction on how to achieve the instant execution of random function selection process with out a button to prompt the random function process. any examples of code, were should the code b...

Objective-c formatting style causes an error in a switch-case

I'm getting an error in my switch statement with some multi-line Objective-c code: - (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { // Notifies users about errors associated with the interface switc...

Java Program Organization: How can I get rid of this massive case statement?

I am creating a program that will fill in a given grammar. Right now I am modeling the "kinds of words" like this: public class WordDescriptor { public static final String noun = "N"; public static final String plural = "p"; public static final String nounPhrase = "h"; public static final String usuParticipleVerb = "V"...

How to sort in SQL, ignoring articles ('the", "a', "an" etc)

This comes up a lot, and I can see it's come up on StackOverflow for XSLT, Ruby and Drupal but I don't see it specifically for SQL. So the question is, how do you sort titles correctly when they begin with "The", "A", or "An"? One way is simply to TRIM() those strings: ORDER BY TRIM( LEADING 'a ' FROM TRIM( LEADING 'an ' FR...

Break out out forloop but within switch statement php

When I normally want to break out of a foreach loop before all of the iterations have completed I simply use a break; statement. e.g. foreach($nodelist as $node) { if($metCriteria) { break; } } But my next example has a switch statement in it. And if one of the conditions are met then I need to break from the foreach loop...

Switch off of type

Possible Duplicate: Switch Case on type of object (C#) I have a method signature that looks like this: public static TLocalType ToLocalType<TLocalType, TContract>(TContract contract) { I would like to be able to do different things based on the actual type of TContract (ie call different methods). Is there a way to...