|
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!
(comments are locked)
|
a = something; 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. Ok thanks a bunch!!
Feb 13 '12 at 11:58 PM
Posly
Two notes:
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:
Feb 14 '12 at 01:20 AM
Datael
@Datael: actually that's only true for C#; every other language including Unityscript allows fall-through:
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)
|
|
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
(comments are locked)
|
|
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 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)
|
