If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.
I think switch statements are fine if you use them in the example provided in the video. Otherwise, if statements are the way to go. If you’re not careful you’re going end up writing hundreds of lines of code to handle something that could be handled and be more simplified with if statements.
Being new to all this, it seems to me that “switch statements” are really messy and have limited use but, again, all this is alien to me. It feels like you could easily forget to include “break;” or “default” at the end, which could you cause to spend a lot of time identifying and fixing the errors. I think for now, I would prefer to use “if/else” statements.
I think switch statements are so great!! The first time I ever encountered one was for the playstation 1 RPG Maker~ and I had no idea how to use it, until one day I got it to work a year or so later. lol… but yeah, I feel that in C# and within unity, it you can do some pretty nifty things once you understand how to apply it to the right situations~ also I personally think it looks very organized. I can see using it for a dialogue system, or to control what happens if certain conditions have been met~ “to implement a case under those conditions”. Like if you have 10 possibilities of something happening, those cases will control the flow of the game, like a choose your own ending story kind of deal
I love switch statements, to the point I usually think of using them before if statements.
If statements are great for logic checks but if you are trying to control the flow of your code then I find switch statements are almost always the best way to go.
They also have the uncanny ability to untangle some seriously gnarly nested if statements so are a great option to consider when refactoring code.
And if you’re using enums then switch statement are your friend (using the double-tab in VS to fill in the code snippet is on of the best shortcuts I know!)
The other joy of Switch statements is the speed. If used correctly you can simply have the code skip any statements that don’t apply to you.
Of the two, my gut tells me to use ‘if’, but that’s most likely due to familiarity from using it in Bash scripts in Linux. I have to say though, I do like ‘switch’ statements and the way they are laid out.
My personal opinion is it’s better to just use if statements unless you can combine switches. For example, I might write the following code as a switch statement (although I might write it as an if-else also).
switch (character)
{
case "Zelda":
case "Mario":
case "Link":
print("do something");
break;
case "Ganandorf":
case "Bowser":
print("do something else");
break;
}
I also prefer if
/ else
. I wish break
wasn’t required, then I’d prefer switch
for value comparisons.
Enjoy our courses.
When it comes to the question of whether I should use if statements or switch, here’s my logic:
if the code requires multiple if’s to check states for example, then I definitely want to use switch. I really wanted to change everything to switches when Ben went on to different choices at the start.
BUT, if I need to handle bigger states like if the player is in game (use all kinds of input and game mechanics), then I use the if.
Switches are very comfortable for making the code look a lot cleaner and more compact for small comparisons like the ones in this video.
The most important thing is that when you are typing any statement, just before finishing it, you can always press tab twice to auto-complete the statement and type in the thing you want to compare.
On thing I haven’t seen mentioned is the use of switch statements with enum types.
On VS, declare an enum:
enum Screen { MainMenu, Password, Win }
Declare a variable of that enum type:
Screen currentScreen;
Then type switch [tab] [tab] to use the switch code snippet, fill in your variable of type enum, and press enter. It will automatically create case statements for each enum type:
switch (currentScreen) { case Screen.MainMenu: break; case Screen.Password: break; case Screen.Win: break; default: break; }
This can save you a lot of time and ensure that you don’t forget to handle one of your enums!
This is the first time I have come across switch. I have always used if statements before. Switch seems to make the code look cleaner.