From the course: Learning C#

Conditionals with "if" - C# Tutorial

From the course: Learning C#

Conditionals with "if"

- [Instructor] Now, that we've learned some of the basics of C-sharp, we're going to learn, in this chapter, about the different ways that C-sharp lets you control the flow of your program. And this includes things like making decisions, based on data or handling error conditions or how to perform repetitive operations. So, let's start by looking at how to perform basic logical decisions. So, here in my Start folder, in Program Flow, I'm going to open up the Conditional If example and I'll click on Program.cs. Programs have to make decisions all the time. So, does a bank customer have enough money in their account to make a withdrawal, for example, or did an airline customer enter the right reservation code to check-in for their flight. In C-sharp, there are two main ways to perform a logical decision. The first is the if statement. So let's collapse that down a little bit here. So, here in my code, I have an integer with a value of 50. So, to create an if statement to check this value, what I would do is I would write something like this. If theVal, and then to check for equality, I use two equal signs. So, if theVal double equals 50 inside the curly braces for the code that we want to run, I'm going to write Console.WriteLine, theVal is 50. I can also attach an optional else condition that executes if this conditional test evaluates to false. So it's optional. I don't have to put this, but I can write else. And in this case, we'll just simply WriteLine that theVal is something else. So, let's go ahead and try to run this. And so, we'll go ahead and right click and bring this up in our Integrated Terminal. And I'll go ahead and dotnet. Oops, put that down here, dotnet run. And we can see that the statement says theVal is 50. And if I change it to something else, like 51 and then run again. Now we can see that theVal is something else. So you can chain multiple if statements together, using else if, like this. So, what I'm going to do is in between this if and the else, I'll put in else, and then if and then I'll say else if and then inside my parentheses, I'll write theVal is greater than or equal to 51 and theVal is less than or equal to 60. Then I'm going to Console.WriteLine, theVal is between 51 and 60. So, now let's run this again. And now we can see that theVal is between 51 and 60 because I've set it to 51. So, there is no practical limit on the number of else if conditions. But once you get past a certain number, like four, maybe five conditions, the code can get a little hard to read, and it might be better to consider using the switch statement, which is something that we'll examine in a separate video. The if else statement can also be shortened by using something called the ternary operator. So, let's just close the terminal for a second. If you consider a simple if else construct like this one where I have if theVal is less than 50 else it prints this, otherwise it prints something else, I can easily condense this down, using what's called the ternary operator, and it looks something like this. First, I'm going to write my Console.WriteLine statement. Then I'm going to write the condition which is the check that we have for theVal. So I'll write theVal is less than 50. Then I'll write the code for the true condition which is the question mark followed by theVal is small. And then the code for the false condition, which comes after a colon, in which case, I'll write theVal is large. So now the string that's going to be passed into the WriteLine function is going to be gated on this condition right here. So, if theVal is less than 50 then this expression is going to evaluate to theVal is small. Otherwise, it's going to use theVal is large. So this is an easy way to condense down, a larger statement like this into a smaller statement. So let's go ahead and save and let's bring our terminal back. It's in the View menu. And we'll try running this again. And now you can see that the output says theVal is large because it is greater than 50. So, this string is not being chosen. It's the second one. So, that's a quick overview of the conditional if statements and the ternary operator. But we're not done. There are other ways to make conditional decisions, and we'll learn about that in a separate video.

Contents