Learn How To Program In C# Part 5 – Switch Statement vs If Statement


Hey, everyone. In this video, we will be going over the differences
between if statement and switch statement. string choice=Console.Readline();
switch (choice) {
case “Avetis”: Console.WriteLine(“This is my name.”);
break; case “Visual Studio”:
Console.WriteLine(“This is my IDE.”); break; default:
Console.WriteLine(“There was no match.”); break;
} This code snippet is from our previous tutorial. What I did is I translated this switch statement
into the following if statement. if (choice.Equals(“Avetis”))
{ Console.WriteLine(“This is my name.”);
} else if (choice.Equals(“Visual Studio”))
{ Console.WriteLine(“This is my IDE.”);
} else
{ Console.WriteLine(“There was no match.”);
} It’s an extended if statement. We have our else if and else at the end. So if the choice is Avetis, it’s going to
do the same thing. It’s going to print out:
This is my name. If it’s not and is Visual Studio, then it’s
going to print out: This is my IDE. Otherwise, in the default case, it’s going
to say: There was no match. The question is when do you use switch statement
and when do you use if statement? Essentially, what a switch statement allows
us to do is it allows us to take in an expression which evaluates down to a value. And usually, it’s going to be a variable. It’s not going to be a concrete number. So it allows us to take in some type of variable,
an unknown. And it allows us to have cases for it. So it allows us to have specific cases for
it for which we execute specific commands. Those can be one-liners, or we can have an
entire block of code there. And we can do the same thing with if statements. However, when we introduce multiple variables,
things get a little bit difficult, if not impossible, for a switch statement. Let’s introduce another variable called
choice2. string choice2=Console.ReadLine();
I modify the code as follows: string choice=Console.Readline();
string choice2=Console.ReadLine(); if (choice.Equals(“Avetis”) && choice2.Equals(“Ghukasyan”))
{ Console.WriteLine(“This is my name and last
name.”); }
else if (choice.Equals(“Visual Studio”)) {
Console.WriteLine(“This is my IDE.”); }
else {
Console.WriteLine(“There was no match.”); }
&& is a Boolean operator, and it takes in two operands, on the left and on the right. This says that if the left side of it or the
right side of it are both true, then the entire thing will be true. If at least one of them is false, if choice.Equals(“Avetis”)
or choice2.Equals(“Ghukasyan”) evaluates to false, then the entire thing will be false. And if the entire thing is false, then obviously,
the block {
Console.WriteLine(“This is my name and last name.”);
} will not be executed, and it will go on to
the next one. We’ve already gone over the operators, so
you should understand how this works with the operands and choice.Equals(“Avetis”) && choice2.Equals(“Ghukasyan”)
being an expression. This is a bit more complex. We have two variables. What if we have a third variable? What if we have a fifth variable? Introducing more variables and more complexity
makes it extremely difficult to do it with a switch statement. There are ways in which we can do it, but
those are going to be very, very hacky and very difficult to read and understand. Instead, we’ll use if statements when it
gets complex. However, when it’s simple to understand
and it’s a value for which we have different cases, then I prefer to use switch for simple
cases like that. If it’s a bit more complex, then I go with
an if statement. And that’s pretty much the difference. I just wanted to bring that up so you know
the differences and you see how to use them in different cases. That’s it. I hope you’ve enjoyed the video. Please don’t forget to subscribe so that
I can keep updating you with the latest content.

6 Replies to “Learn How To Program In C# Part 5 – Switch Statement vs If Statement”

Leave a Reply

Your email address will not be published. Required fields are marked *