x


Please explain a switch statement?

Alright so I get it's like a big "if, else" statement, and I know the structure of it, but I'm a little confused with the "case", is the case the current value of the variable? And how do you write a case? Is it: case "value of variable":, or are there no "". Also how do you close it, do you just end it with a }? Thanks!

more ▼

asked Feb 13 '12 at 11:31 PM

Posly gravatar image

Posly
391 35 42 44

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first
 var a;

a = something;

 switch (a)
 {
    case 1: // if a is an integer
           Debug.Log("a is an integer");
           break;
    case "1": // if a is a string
           Debug.Log("a is a string");
           break;
    case default:
           Debug.Log ("none of the above");
           break;
 }

The variable named in the 'switch' line is the current value of the variable, of course. Those listed in each 'case' are what to match with. Use the default if you need a catch-all.

more ▼

answered Feb 13 '12 at 11:38 PM

DaveA gravatar image

DaveA
26.8k 153 171 257

Ok thanks a bunch!!

Feb 13 '12 at 11:58 PM Posly

Two notes:
1- The break statement ends each case; if missing, the execution continues in the following case code (it's an intended feature)
2- @DaveA, it's only default:, not case default: (at least in UnityScript)

Feb 14 '12 at 12:01 AM aldonaletto

@aldonaletto C# is also just default:

As a side note, in C# if you have any instructions in a case, you must use a break; you can only fall through to the following case if there is no code per case. ie:

string WhatAmI(int i) {
    switch(i) {
    case 0:                      // this is ok
    case 1:
        return "insignificant";
        break;
    case 2:
        Debug.Log("Two!");       // this is not ok
    case 4:
        return "even";
        break;
    default:
        return "something else";
        break;
}
Feb 14 '12 at 01:20 AM Datael

@Datael: actually that's only true for C#; every other language including Unityscript allows fall-through:

var a = 2;
switch (a) {
    case 1:
       Debug.Log ("1");
       break;
    case 2:
       Debug.Log ("2");
    case 3:
       Debug.Log ("3");
       break;
}
Feb 14 '12 at 01:49 AM Eric5h5

I was intending to only be talking about C# but I guess I didn't make that clear at all did I :)

Edited my comment to make that more clear

Feb 14 '12 at 01:55 AM Datael
(comments are locked)
10|3000 characters needed characters left

Yes is a switch statement the case is the current value of the variable you are checking against. You are correct in that a switch statement work in a very similar way to a bunch of if statements or even if else statements. The upside is that a switch statement is much more efficient because you are only checking the variable once and not checking each "if". The syntax depends on the language you are coding in. If you are using C# you end a case by setting a "break" see syntax below: I have defined "state" as the current "MenuState" and use this switch statement to change what I am drawing. Hope this helps. For further information you can also look at this link: http://msdn.microsoft.com/en-us/library/06tc147t(v=vs.71).aspx

    switch (state) 

    {

    case MenuState.DoNothing:

       // Do nothing

       break;

    case MenuState.RenderSelf:

       GUI.Window (InGameWindowID, MenuRect(menuSize), doInGameOptionsMenu, "");

       break;

    case MenuState.RenderSubMenu:
                    //handle sub-menu rendering here

       break;

    }
more ▼

answered Feb 14 '12 at 01:10 AM

Dik gravatar image

Dik
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

IMHO, don't waste your time learning switch statements. Learning arrays, functions, classes ... will solve so many programming problems for you. Switches are just bad ifs -- they're one of many failed features, like repeat-until loops. "Bunch of switch statements" is programmer slang for bug-ridden program.

I sympathize with switch-users. I learned and used them -- the old case 'a', 'A': trick (toLower is easier and faster,) leaving off the break to make lines serve double-purpose ... . But, kill your darlings.

more ▼

answered Feb 14 '12 at 02:47 AM

Owen Reynolds gravatar image

Owen Reynolds
12.2k 1 7 46

I wouldn't agree that there's no use for switch statements; it's far easier to read switch statements sometimes than long lists of if/else if instructions. They're also helpful for unrolling loops in certain situations, too... But like you say they shouldn't be top priority

Feb 14 '12 at 03:03 AM Datael

Sorry, but no. Switch statements have their place. Yes, they can be abused, like just about every feature there is, but that's not a reason to never use them. (Also, I've used a do/while loop exactly once in recent memory, but it was precisely what I needed in that case.)

Plus this doesn't answer the question. If you want to argue about stuff, that's what the forums are for.

Feb 14 '12 at 03:41 AM Eric5h5

I agree to @Eric5h5: use the right tool for the job!

Feb 14 '12 at 11:48 AM aldonaletto

Want to avoid meta-discussion, but RE: Forums vs. Answers:

Many Q's here are of the form "How do I solve X, using Y." The comments often drift into what they are really trying to accomplish. Sometimes the poster just wanted an excuse to learn Y. But, more often, the answer they accept is about an easier way to solve it.

Say someone hears that switches can be used to swap colors and does some research here. A useful piece of info would be that ifs, which they probably know a lot better, have as much color-swapping power as switches.

Feb 14 '12 at 07:15 PM Owen Reynolds
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3570
x241
x6
x1

asked: Feb 13 '12 at 11:31 PM

Seen: 5751 times

Last Updated: Feb 14 '12 at 07:15 PM