Learn How To Program In C# Part 3 – If Statement


Hey, everyone. Today we’ll be learning about control flow
or flow of control statements. There are three of them: decision-making statements,
looping statements, and branching statements. In this video, we’ll be going over decision-making
statements. When you usually run your code, what happens
is from top to bottom, statements get executed. But with control flow, you can break that
flow and you can manipulate it the way you want to. Let’s create a decision-making statement
that will control our flow. Make a new project. Let’s save it as ControlFlowDecision. This is the Main method where the execution
of our program will begin. One more thing, we already know how to write
to the console with Console.WriteLine(). But now let’s learn how to get an input
from the console. The way we do that is:
Console.ReadLine(); And let’s look at this method. It’s returning a string. And we know how to declare a variable of type
string. string
player=Console.ReadLine(); What’s going to happen is this is our operator. You already know about that. It’s the assignment operator, taking the
right side and assigning it to the left side. You already know that. Now let’s see how our decision-making statement
will look like. You can see that we have a reserved keyword
if. And in the parentheses, we have a little block
here. if ()
{ }
It says, “Expression expected.” Now it’s not just an expression. It’s a Boolean expression that it’s expecting. A Boolean expression is a type of expression
that gets evaluated to a Boolean value. A Boolean value is pretty much either true
or false. The bottom line is that any expression you
put there has to be a Boolean expression so that you can get a result of true or false. And that’s how you will determine if you
execute the current block or the statements below it. if (player.Equals(“AI”))
{ }
Equals is just a method that is available to us, prewritten for us. We’re just saying if the person enters the
string “AI”. If this expression is true, then this block
will get executed. Let’s put something here. if (player.Equals(“AI”))
{ Console.WriteLine(“The player is AI.”);
} Now there’s actually more we can do with
this. if (player.Equals(“AI”))
{ Console.WriteLine(“The player is AI.”);
} else if ()
{ }
It looks the same. We’ve got a block. We’ve got two parentheses, and it says else
if. What happens is if player.Equals(“AI”) is
not true, if it doesn’t get evaluated to true, then what’s going to happen is we
will skip to the else if section, and we will see what’s in here. We’ll try to evaluate that condition. So in here we’re going to say:
if (player.Equals(“AI”)) {
Console.WriteLine(“The player is AI.”); }
else if (player.Equals(“Human”)) {
Console.WriteLine(“The player is Human.”); }
If player.Equals(“AI”) is true, then Console.WriteLine(“The player is AI.”) will get executed. If player.Equals(“Human”) is true, then Console.WriteLine(“The
player is Human.”) will get executed. However, if player.Equals(“AI”) is true, it
will never get to Console.WriteLine(“The player is Human.”) because if player equals
to “AI”, it will never even try to check this other condition. If we take out the else,
if (player.Equals(“AI”)) {
Console.WriteLine(“The player is AI.”); }
if (player.Equals(“Human”)) {
Console.WriteLine(“The player is Human.”); }
then what’s going to happen is it will check player.Equals(“AI”), and it will check player.Equals(“Human”)
as well. Even if the player is AI, it will still try
to check the other condition. It will try to evaluate it. The reason is that now we have two different
decision-making statements. So when you’re adding an else, you’re
just extending your condition. It’s still the same decision-making statement,
but you’re extending your statement. So even if we say something else in the else
if, if player.Equals(“AI”) is true, then else if part will never get hit. However, if we don’t include else, then
the next if statement will be evaluated in its own term. I just wanted to make sure that you understand
how it works, that you can extend it, you can have separate ones. We can extend our decision-making statement
further and say else. else is pretty much the default case. if (player.Equals(“AI”))
{ Console.WriteLine(“The player is AI.”);
} else if (player.Equals(“Human”))
{ Console.WriteLine(“The player is Human.”);
} else
{ Console.WriteLine(“The player is not AI
and not Human.”); }
If your player is not AI and if it’s also not human, then the last block will always
get hit. This is your default condition. Let’s run this. This is a break point. You already know about that. It’s pretty much stopping the flow of execution. It’s allowing us to go through line-by-line
and see what’s happening under the hood. Let’s click Start. Open this up, step into. And it’s expecting us to put something as
an input. I put AI. Let’s see what happens. It says, “The player is AI.” It did the assignment. It knows that the player is AI. Actually, step over. As you can see, it stepped into this block. You see the pointer here? The condition was true. There’s a player AI, and it’s matched
to AI. So it started executing this block. We changed the flow. So as you can see, it skipped. It didn’t even go into else if block. It didn’t even go into the else block. It just went to the end. Now, if we look at it, it says, “The player
is AI.” Let’s try one more time. Let’s try to run this and let’s try to
do one more experiment. Let’s say Robot and Enter. Now the player is robot. So as you see, it didn’t even go into the
first if block because the condition wasn’t satisfied. It just went to the else. However, it’s not even that, so it just
went to the default case. So it’s going to come here, it’s going
to print it out, and it’s going to see that the player is not AI and not Human. But do you remember what I said? The following two blocks,
else if (player.Equals(“Human”)) {
Console.WriteLine(“The player is Human.”); }
else {
Console.WriteLine(“The player is not AI and not Human.”);
} are just an extension of your current condition. Now we have another one down below it. if (player.Equals(“AI”))
{ Console.WriteLine(“The player is AI.”);
} else if (player.Equals(“Human”))
{ Console.WriteLine(“The player is Human.”);
} else
{ Console.WriteLine(“The player is not AI
and not Human.”); }
if (player.Equals(“Robot”)) {
Console.WriteLine(“The player is Robot.”); }
Let’s try this. Let’s put in Robot. And it is case sensitive because we’re not
taking care of that. I wanted to show you the decision-making statements. We’ll go over string manipulation later. Now if we put in Robot, the output is:
The player is not AI and not Human. The player is Robot. It did evaluate the last if statement and
it was true. And it printed that the player is Robot. Why? Because the first if statement is a separate
decision-making statement, which is extended. And the last if statement is a separate one
from that. So it’s been evaluated on its own. So as you can predict, it will say, “The
player is Robot.” Wonderful. I hope this helps you understand more about
the decision-making statements or just learn about them if you didn’t know because these
tutorials are for beginners. So even if you didn’t know about decision-making
statements, it’s absolutely fine. Now you know. I hope you enjoyed the video. I will see you in the next part. If you haven’t subscribed, please subscribed
so that I can keep updating you with the latest content.

8 Replies to “Learn How To Program In C# Part 3 – If Statement”

  1. Your Tutorial of C# is great and i understand it, but i cant type something in in the Window where you wrote AI, Human,…

    The cursor is there but I cant write.

  2. Dude you are the most helpful person ever. I love this series. I knew the basics already, but even your first 2 videos taught me a lot. Thanks and hope you are successful with this channel.

  3. Around 6 minutes in, there's a few times where you say "Step" and click something out of screen. What are you doing?

  4. @TheBiggestAuto I actually had the same exact issue. What I ended up doing was inserting the break point not at the string player = Console.ReadLine(); line, but instead at the third to last curly bracket, "}".

    @AvetisG Is there any way you can explain why changing the position of the break point would solve this issue? Also, I really appreciate what you do! Love your videos, they are very informative and very easy to follow.

  5. I have a question while doing this. After I enter the text for example "human" do we press enter for the command to execute?

  6. I get this problem whereby the 2nd condition, the curly bracket at the bottom(above the word else) has a red curly bracket. Apart from that everything is good, any way to fix that?

Leave a Reply

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