If statements in C# enable branching logic by evaluating conditions (Boolean values of true or false) and executing code blocks only when conditions are true; conditions use mathematical operators (>, <, >=, <=, ==, !=) and logical operators (&& for AND, || for OR, ! for NOT) to combine multiple conditions, with evaluation proceeding left-to-right and short-circuiting for performance optimization.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Learn C# If Conditions in 16 Minutes!
Added:Hello and welcome. I'm your Code Monkey.
Here, let's learn all about if-else statements and conditions. When you think of programming, you likely think of branching logic. And this right here, conditions, it's basically what it does.
It is one of the core pillars for anything related to programming, so this is a very crucial lecture. This section of the lecture is taken from my complete C# course. This is a 12-hour course covering everything about C#, everything from beginner to intermediate to advanced. There's a free 12-hour video over here on YouTube.
Hello and welcome. I'm your Code Monkey.
In this lecture, we're going to learn about if-else statements and conditions.
We're going to learn how conditions are evaluated, how to do mathematical tests and logical operations. So, let's learn about ifs and conditions. This has to do with one type that we already saw in the previous lecture, Boolean. This is a type that can only be either true or false. Those are the only two possible values. Basically, we write a condition, and then the code evaluates that condition to either true or false. Now, the simplest condition is just equality.
So, for example, is one equal to one?
The answer is yep. So, when the code evaluates this one, it evaluates it to true. And if we check, is one equal to two? That is obviously false. One is not equal to two, so this one evaluates to false. Then we can use these conditions inside an if. The if has a condition and then a code block. And if the condition is true, then it is going to execute this code block. And if it is false, then it does not execute this code block. And again, going back to what I mentioned in the programming basics lecture, the code executes from top to bottom. So, first it executes this if, then checks the condition, and the condition is true, so it goes inside and executes the code inside, then goes back outside, then executes this if, checks the condition. This one is false, so it skips that one and continues running afterwards. So, the code is still executing from top to bottom as usual.
But by using ifs, we can control what code actually runs when. Here is a visual demo in the interactive exercises. We've got a simple if condition testing if a distance is under a certain amount. If so, then we're going to set this target color to green, and if not, we're going to set it to white. So, I can collect to move the player, and as the distance goes under that amount, yep, that one turns green.
And if it goes above, then that one turns white. So, depending on whether this condition is true or not, it is running either this line or this line of code. Here in the code, let's see our syntax. And the syntax for an if is really simple. We just write if, and again, remember how code is case sensitive, so it is not if all uppercase, it is not uppercase and lowercase, it is neither of these, instead, it is if all lowercase, again, very important. If you write it properly, it should change color. Then we write the condition inside parentheses. Remember to have a matching closing parenthesis for every opening.
The condition can be all kinds of things. They will eventually be evaluated into a boolean, so either true or false. So over here, we can just write true for testing. So we have if, then the condition, and then we just do a code block, and this is the code that will be executed if the condition is true. So for example, over here, just a console.writeline, let's say inside if.
And if we run this, and if there it is, we are inside the if, so we did run that code. Whereas now, if instead of true, if we change this into false and run it, and you have now that one does not run because that one is false. So the code goes to execute this line, then it sees this condition is false, so it is going to skip this one and continue afterwards. Now, it obviously doesn't make much sense manually write out a true or false over here on the condition. Usually, you want to test some kind of thing, some kind of proper condition. And the most basic thing is simply mathematical operations. So if we test 1 = 1, if we test this, you have that it inside, goes inside the if.
Alternatively, instead of just equality, we can simply do some mathematical operations, like for example, let's see if we are testing if two is bigger than one. So pretty simple, and the result of this test is true since two is indeed bigger than one. So if we run this, you have there's our message, it works.
Basically, what the code is doing is it when it gets over here to the if, it gets to this condition, then it evaluates this condition, and again, it is going to evaluate into either true or false, and then if it is true, it goes inside it, if it is false, it skips it.
There's a bunch of mathematical operations you can do. You can do bigger than, you can do less than, you can do bigger or equal, less than or equal, so upon these standard mathematical operations. And of course, over here, I'm using values directly, but you could replace this with a variable. So like an int for the age, 35, then let's say if age is under or equal to 40. And you have obviously this works. So, you have all kinds of operations, and then you also have the equals. Now, here's another extremely extremely important thing. When you want to test for equality, you do a double equals, not just one. It's a double equal sign. The single equals, this one is meant for assignment. Like, for example, we define a variable, then we do a single equals, and we are assigning the value 35 onto our variable. But when we want to test for equality, we do a double equals.
Again, this is very important. This is the mistake that a lot of beginners do.
They do something like a single equals, and they have no idea why there's an error. Over here, the error isn't very helpful. It says the left-hand side of the assignment must be a variable. So, if you're a beginner, this makes no sense. But that's because the single equals, this one stands for assignment, and double equals, this one is for equality. And if you try putting that inside a write line, so let's do console.writeline. If we do age double equals 35, if we do this, yep, there we go, that does return true. However, if now we put just a single equals, and yep, now instead of either true or false, it just says 35. That's because the single equals, this one is assigning the value over there, and it's not actually doing any comparison. So, again, don't make this mistake. Single equals is for assignment, and double equals is for equality. So, related to equals is the not equal sign. And for this one, we do an exclamation point and then an equals. So, the exclamation point means not. So, not equals. So, in this case, this one will not run because one is equal to one. So, one not equals one is false. Whereas, if we put a two, one does not equal two, yep, that's true. So, yep, it does run. And of course, over here, we can simply use boolean variables. So, bool isPlayer, let's define this as true. And then over here, just if isPlayer. If so, then yep, this works. So, over here, we have the code block that is going to execute if the if condition is true. And then we have another keyword that we can add after the if. We can add the keyword else, and then another code block. And now, this code block will execute if the first if is not true. So, let's do another write line inside else. And over here, we define a player true, then we test if isPlayer. So, it is going to be inside this one. So, it is not going to run this one. If we test, yep, there we go, it just says inside the But then, if I swap to false, so now it is not going to run the if, and instead it's going to run the else. If there it is, just like that. So, for example, instead of having two ifs, like if is player and if not is player, which by the way, the not equals that one can also be applied like this.
If you put the exclamation point, then it's basically going to negate whatever comes afterwards. So, over here we have not is player. So, we are testing if it is player or not is player, which is really the same thing. So, if you want to test this kind of thing, then using an else makes it much simpler. That's exactly what the code over here in the demo is doing. It is testing if distance is under four, and if so, then it's going to set the target color to green.
If not, it is going to set it to white.
If I had not written this else statement, then as soon as it became green, even if we stood outside of it, it would not change it back. Whereas like this, if it's inside, it turns green. If not, else, it turns white. And now here we are testing a single condition, but we can also combine multiple conditions. Let's say, for example, we want to test if two is bigger than one, and if three is bigger than one. The way we write a logical and is just like this, with two ampersands.
Again, it's very, very important, just like the double equals, this is a double ampersand. If you put just one, this is a completely different operation. This is a bitwise and, which is something that I covered in the advanced portion of this course. But over here, for a logical and, we do not want just one, so we want two of them. Again, don't make this mistake. So, let's see what this outputs. And if there it is, it runs the code inside the if.
So, basically here we have two conditions, and when the code gets to this point, it is basically going to evaluate each condition from left to right. So, first it evaluates this one.
Is two bigger than one? The answer is yes, so this one evaluates to true. And is three bigger than one? The answer is also yes, so this one also evaluates to true. And then we've got an and operation between a true and a true.
And this and operation, the output of this one will only be true if both conditions are true, which is the case over here. So, that's why this one runs inside the if. If we change one of these, so let's say true and false, if so, then the output for this one will be false, so it is not going to run this one, instead it's going to run the else.
In order to know what combinations make true or false, you can look at what is called a truth table. So, over here we've got the values A and B.
And then we've got the result of doing an and operation or an or operation between both those.
A and B can be either true or false, which by the way, sometimes true or false is represented as simply ones and zeros. So, 0 equals false and 1 equals true. Really just different representations of the same thing. So, over here we have the and operator. And like I said, the and this one is only true if both conditions are true. So, if A is false and B is false, both are false, so the and output is going to be false. If A is false but B is true, again we still don't have the condition that both them are true, so the output is still false. Same thing over here, if A is true, B is false, the output is still false. It is only if we have both A and B, both of them are true. If so, then the output of the and is also true.
So, the result of an and is only true if both components are true. Then, the other operation that we can see here is the or operator. This one is going to be true if either A or B is true. So, if both them are false, if so, then the or is going to be false. But if just one of them is true, then the output of the or is going to be true. Or if both of them are true, if so, then the output is also going to be true. Over here in the code, the way we write an or operator is denoted by two pipes. Again, pay close attention to the fact that it is two. If you write just one, this is a completely different operation. This is another bitwise operation, which again I cover in detail in the advanced section. So, also pay close attention here, it is two pipes. So, this makes an or operation.
Like I said, this one is going to run our code block if either of these are true. So, if this one is true, then yep, this one is going to run. And yep, it does run. The or only returns false if both of them are false. And if we set this, yep, we run the code inside the else. There is an interactive truth table in the exercises. Here up top we have the inputs and down here we have the outputs. So, for the and, the and is only true if both are true. So, if that one is true and false, the output is false. False true, that one. And if both are true, that one is true. The or is only true if either of them are true.
So, if either of these are true, the output is going to be true. And the not simply inverts it. So, if it's false, it becomes true, and if it's true, it becomes false. Now, these conditions, doing an or or an and, doing this always works just between two elements, but you can also have multiple conditions, not just two. You can have as many conditions as you want. So, here and true or false, you can have as many as you want. However, it's also important to know they are evaluated from left to right, meaning that whenever doing some kind of or or and operation, it is always done just between two conditions.
So, when the code gets here, first it evaluates this one and gets the result from this evaluation. Then after having that result, then it does the result of this one alongside the result of that one, and finally from this one along with the result of all these. So, it does all those evaluations, and importantly, always from left to right.
So, for example, let's write if 1 = 2 or 1 = 1 and 5 is bigger than 1. So, if we have these conditions, if you want, pause the video and try to figure out if the code is going to run or not. Like I said, left to right. So, first it evaluates this one. Does 1 = 2? The answer is no, so this one evaluates into false. Then it is going to evaluate this one. Does 1 = 1? The answer is true.
Yep. Then it's going to do an or operation between false or true, and again, the or is true if just either of them is true. So, in this case, this one outputs to true. Then it evaluates this one. Is 5 bigger than 1? That is true.
And then we have a true and true. This one returns true, and yep, we do run this code. Then if you want, you can also have more control over the order of testing simply by using parentheses, just like you do in normal math. So, for example, let's write some code like this. So, for example, we have false or true. This one is going to evaluate into true. Then it's going to do true and false, that is going to evaluate to false, and then false or true, that is going to evaluate into true, so it is going to run this one. So, over here we can add some parentheses, so we want to do this condition first, and then this condition first. And this way, first it is going to evaluate these two, then evaluate these two, and only then evaluate the whole thing. Another important thing is also how conditions can short circuit, meaning let's say we have an or, and with an or, it is going to return true if either of them is true. So, if we have true or false, or actually in this case, let's put some proper conditions. So, one bigger than two, that is going to be false, and two bigger than one. So, if we have this, basically over here, two bigger than one, this is going to evaluate to true.
One bigger than two, this is going to evaluate to false. Now, like I said, the or, this one is going to be true when either of them is true. So, when the compiler starts to evaluate these conditions, it starts to evaluate the first one, again, left to right, and evaluates this one into true. And since that is the case, the compiler is smart enough to know not to waste any power on evaluating this second condition, because the first one is true, then it's already going to run inside the if. So, after here, we can have a bunch more ors, and it does not matter. The compiler would only test this one, since this one is true, it just ignores everything after this. However, on the other hand, if we have an and, if so, then it's going to evaluate this one, this one is true, but then the and is only true if both of them are true. So, it cannot stop executing here, it needs to execute this one in order to see if it's true or false, in order to know, does it run inside the if or not. So, in this case, the short circuit for the and would be if this one is false. If this one is false right here, then doesn't even bother evaluating this condition, because if the first one is false, then the and is always going to return false.
This is the kind of thing that becomes really important in some performance intensive applications, especially when you're running some expensive functions to test something. For example, if you have true, or and then over here you've got some kind of function that costs quite a lot in terms of performance. If so, then the compiler is going to be smart enough, and it's not even going to waste any time running this condition, because the first one is already true.
Now, over here, let me also make one quick note. If technically you do not require a code block, so if you just have an if without an else, if so, technically you can just do this. So, you can erase the code block and just write the condition afterwards. If you have just a single instruction inside the if, then this is perfectly valid code. So, let's put this one in true, so it goes inside. And there you go, it does run the code inside the if.
However, I highly, highly advise you to always write a code block. The reason is simply because it is very easy to write an if like this, and then later on, you want to write another instruction. So, let's say, inside if two. So, you write this, but you forget to write the code block, and you assume this one is still going to run inside the if. And in this case, if we try running this, we do see if and then the if two. But, if we change this into false, ideally we would not want either of these run, but yep, the second one does run. That is because if you don't include a code block, then the only thing that is considered to be inside the if is just the next line immediately afterwards. Remember that in C# indentation does not matter, so the fact that this one has as many spaces, that does not matter at all. The only things that matters are code blocks and semicolons. If you don't include a code block, then basically just executes the code inside the if, which is going to be until the first semicolon, till the first instruction. So, in order to avoid this mistake, my advice is to always write code blocks in all your ifs. So, always put it like this, even if you have just one instruction inside it. And speaking of indentation, like I said, it does not matter. So, you can indent the code like this, or do anything crazy, anything you want. As far as the compiler is concerned, all of this is valid code inside this if. But, for your own personal sanity, definitely make sure to take some time to indent things properly. Now, here we have our ifs, and we saw the else as well. Something we can do chain ifs. So, let's do an if true, and then else, and after the else, we can write if immediately afterwards.
So, for example, here's some code, we define some kind of age, then we do the first if, testing if age is under 20. In this case, it is not, so it is going to run the else related to this if. And then inside that else, we've got another if. So, if under 30, meaning the code inside here is going to run if the age is not inside this if, so if it's above or equal to 20 and under 30. And if not, then it's going to go into the else connected to this if, and finally run this line of code. And if we test, yep, correct, age 35 is indeed above 30. So, you can chain a bunch of else ifs together, or you can simply just do a bunch of ifs. Or if you want, we can also chain a bunch of them just like this. So, basically just having some ifs one after the other. Yep, this is also perfectly valid code. All right, so here we learned about ifs, and the last thing that we did was chain a bunch of ifs together. This code works correctly, we have all of our ifs one after the other, but with this many ifs, it becomes really hard to understand what the code is doing. So, to solve this particular problem, we have another way to run conditional logic, which is by using a switch. So, that's what we're going to cover in the next lecture.
All right, I hope you learned a lot from this free lecture. Like I mentioned, there's a completely free 12-hour YouTube video covering everything from C# from beginner to advanced. Or if you want to go deeper, if so, you can pick up the premium version, which includes a bunch of bonuses. The main one being a companion project. This one basically contains interactive exercises. It contains quizzes and FAQs for every single lecture in the course. The interactive exercise, these specifically, they are really excellent because they basically force you to learn by doing. They require you to actually write code as opposed to just passively watching a video, which in turn means it helps you actively learn the concepts of each lecture. And that ensures you are actually generally learning. The premium version is paid, so if you can afford that's awesome. It will definitely help you a lot in your learning journey. But if you can't afford it, that's fine. Just watch this free lecture right here, and then check out the free 12-hour video on YouTube.
And after you get past the complete beginner stage, after that, check out my problem-solving course. This one helps you learn basically the most valuable skill of all. It is the skill that is absolutely required if you want to get a six-figure job. So, yeah, check it out.
The link is in the description. Thanks so much for watching, and I'll see you next time.
Related Videos

TOP 15 Data compression Interview Questions and Answers 2019 Part-2 | Data compression | Wisdom jobs
wisdomjobs
281 views•2019-06-28

CTS 158: 802.11w Management Frame Protection
ClearToSend
4K views•2019-02-04

NDSS 2019 Send Hardest Problems My Way: Probabilistic Path Prioritization for Hybrid Fuzzing
NDSSSymposium
496 views•2019-04-02

How realistic is Cities: Skylines?
CityBeautiful
159K views•2019-02-14

GUIs & TUIs: Choosing a User Interface for Your Python Project | Real Python Podcast
realpython
2K views•2025-04-04

The OSI Model - Explained by Example
hnasr
225K views•2019-05-12

Cloud Computing - Introduction
elithecomputerguy
98K views•2019-10-07

From Traveler's Dilemma to Dynamic Routing | Demystifying Networking
IITBombayJuly
5K views•2019-08-04
Trending

2.4 BILLION Records Got Leaked...
DeepHumor
15K views•2026-07-22

Playstation NO DISC/NO BUY Fight Is Over...
DavidJaffeGames
4K views•2026-07-23

Should I buy a Sawmill?
essentialcraftsman
29K views•2026-07-22

Americans Confused in Australia for 17 Minutes Straight
IWrocker
17K views•2026-07-23