How to Program in C# - Conditions (E03)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
- So far, we've only made programmes that would do the same thing every time. While that is cool, programming doesn't start to get really interesting before we can have our programme determine what to do next based on what we tell it. This is where the IF statement comes in. IF statements allow us to check for a condition and execute different code depending on if its true or false. Say, for example, we want to create a quiz game. Here, we need to check if the player's answers are correct. We can, of course, do that using an IF statement. In this case, we could have the programme ask something like "What's the best movie trilogy of all time?" We could then collect some input from the player and check if it corresponds to the correct answer. If it does, and the user typed in "Lord of the Rings," we can print out "Correct!" If it doesn't, and the user typed in anything else, we can print out "Incorrect," and send them to the fiery depths of Mordor. (beep) So, IF statements is one of the most essential tools for any programmer. But, before we get into it, this video is sponsored by Denis Panjuta. Denis is an awesome game dev tutor, who just updated his super successful complete C# masterclass. This class will make you super comfortable writing code in C#, and using it for programming your own games in Unity. It will give you knowledge on many different aspects of C# and teach topics such as databases, link in WPF, and much more. At the end of the course, you will even have made three games on your own. If this sounds like something you're interested in, simply click the link in the description to get started and get a massive discount. Now, when we want to write an IF statement, the syntax is actually quite simple. We write "if," then some parentheses, and then we open and close some curly brackets. Inside the parenthesis, we put our condition. In other words, what we want to check for. In the case of our quiz game, we probably have some kind of player-answer variable that holds whatever the player typed in. We can check if this is equal to Lord of the Rings. If it is, our condition is met, then everything inside of the curly brackets will be executed. So, in this case, we would print out "Correct." Of course, we often want to define what happens if the condition isn't met. If this is the case, we use an IF/else statement. This is as simple as writing "else" at the bottom, followed by two curly brackets, where we can put what we want to happen if the condition isn't met. In this case, we would print out "Incorrect." (indistinct) Mordor! Now, we can use different symbols to create conditions. The double equals means "is equal to," and an exclamation sign and an equal sign means "is not equal to." If we are dealing with numbers, the greater than or less than signs can be used to check if a number is greater than or less than the other number. If we put an equal sign after that, it means "greater than or equal to," or "less than or equal to." Of course, I don't expect you to remember this right away, so here's a table that you can look at if in doubt. With that, let's write some code. Let's say you're going to the cinema to watch a movie and you want to buy a ticket. Well, using IF statements, we can create a programme that allows you to do this. So let's, first of all, use "Console.WriteLine" to just print out something like "Welcome! Tickets are $5. Please insert cash." Then we can have the user insert some cash. In our case, let's store this in an integer. So, we'll create an integer called "cash," and set it equal to "Console.ReadLine." Remember, like we talked about in our last video on variables, we first have to convert this to an integer. So, we'll use "Convert.ToInt32," and wrap the parentheses around the "ReadLine." There we go. So, we've now asked the user to input some cash, and the user can then type in a number, which is the amount of dollars they put in. What we can then do is use an IF statement to do something based on how much they put in. So, we'll make some new lines here, and we'll write "if," then parenthesis, and then we'll write the condition. In our case, we can check "if (cash < 5)," because the ticket price is five dollars. In this case, the user hasn't put in enough money. So, we can go ahead and open and close some curly brackets, and I'm just going to put them down here on a separate line so it's very clear that inside of these curly brackets is what is going to happen if this condition is true. We can just put something in here like "Console.WriteLine("That's no enough money.");" There we go. So if the user inputs less than five dollars, we can going to print out that that's not enough. Right away, if we run this programme, we can see it says "Welcome! Tickets are $5. Please insert cash." I'm going to type in "3" here, and it says "That's not enough money." Awesome. Of course, we also want to write what happens when the user inputs enough. For that, we can use an IF/else. So, right now we're saying, if we input less than five dollars, do this. Then, after that, we can write "else," so in all other cases. Again, I'm going to put this on a separate line just to make it easier to read. So, if the user inputs five dollars or more, then we can write "Here is your ticket," and print out a ticket for them. Again, if we run this programme now, and we type in five dollars, it says "Here's your ticket." Awesome. But if the user inputs more than five dollars, we want to give them their ticket as well as some change. So, we actually need to add a separate condition for this. We can do this using an else/IF. So, if the user inputs less the five dollars, we write out "That's not enough money." If not, then we can check. So, "else if (cash == 5)." If the user inputs a exactly five dollars, then we can write out "Here's your ticket." If not - so we can put another else statement here - if not...well then it's not less than five dollars and its not equal to five dollars, so it's more than five dollars. Here, we can go ahead and calculate how much change we should give back to the user. So we can use a variable, call it "change," and set it equal to the amount of cash that is put in, minus five, which is the ticket price. So, that's how much we want to give back. So, we can write out "Console.Writeline ("Here is your ticket and" + change + "dollars in change.")" There we go. So, we basically have three different states here: less than five dollars, equal to five dollars, or more than five dollars. We are using this by just chaining together IF and else/IF statements. Really, really cool. If we go ahead and run this now, we can see that if we input more than five dollars, so I'm going to put in eight dollars here, it says "Here's your ticket and 3 dollars in change." Awesome. So, that's a great way to start using IF statements in your code to do different things depending on whether or not a condition is met. Let's have a look at another example. So, imagine that we're at a theme park, and we want to create a programme that checks whether or not people are allowed to go onto a rollercoaster. In this case, we want to determine this based on the age and height of the user. So, as you can see at the top here, I'm declaring two variables: an integer for the age and an integer for the height. So, two simple numbers, and then asking the user to input their age, and collecting it just like we did before. After that, I ask the user to input their height in centimetres and, again, we collect that just like before. If I just quickly run this programme, we can see that I can input an age. Let's just do "24", and a height, let's just do "190." There we go. So, we can now use IF statements to determine if the user can enter the rollercoaster based on these two variables. So, just like before, we can create some lines. We can check "if(age >= 18)." Well, then, we can write out "Console.WriteLine("You can enter!")" Notice how this time, I'm just leaving the curly bracket up here. It does the exact same thing. If this is harder for you to read, feel free to put it on a separate line, but this is just shorter and, kind of, the standard practise these days. So, now we are saying that anyone who is 18 or older can enter the rollercoaster. We also want to check if the person is tall enough. To do this, we could go in here and create another IF statement inside the other one, so we could go "if(height >= 160)," then the person can enter. Then, put this in here. This is called nesting because we are putting one IF statement inside of another, but there's actually an easier way to do this. So, instead of creating two IF statements, we can just go up here, and add another condition that needs to be met. To do that, we use two ampersand signs. This means "and." So, "if(age >= 18 && height >= 160)," well then, this condition is met and we can go ahead and write "You can enter!" If not, we'll put an "else" here, "Console.WriteLine("You don't meet the requirements.")" If we try and run this, we can put in an age. Let's say 22 - that's old enough - and a height - let's do something that isn't tall enough - so, let's just input 140. As you can see, it says "You don't meet the requirements," because only one of the variables is correct. Whereas, if we re-run the programme and input 22 and then, let's say, 190, it says "You can enter." Just like we can check whether both of these conditions are true, we can also check if just one of them is. That's the "or" sign or "||." So, this means that the condition is met if either of these two are true. So, if we're older than 18 or our height is greater than 160. If we save that and run it now, we can input an age of "14," which is not old enough, and then a height which is large enough, so 180, we can see that it says "You can enter," because, now, one of the conditions is met. So, that's how you can work with multiple conditions. Awesome. Now, for the challenge before the next video, I would like you to create a programme - kind of a game, actually - where the player has to solve math problems. An example of this could be "10 * 2 + 3." So, the answer to this is, of course, 23. So, you have to show the user problem, have them input an answer, and if the answer is correct, you can tell the user that it's correct. If not, you have to tell them that it's wrong. So, in my programme here, I'm showing "15 + 2 * 2." That fifteen plus four, or nineteen, which is correct, and it tells me so. Then, it actually gives me another problem. Let's say I get this answer wrong and say that's twelve, which is wrong and it shows that. Then, there's a final one here, and I believe the answer to this is six, which is correct. So, I've just put in three problems here. You can put in as many as you want, but that is your challenge for the next video. So, before we end, I just want show you one quick thing, which is the switch statement. So, here I have an example, where it asks the user to input a number between one and five. We, then, collect the number as we normally do, and then we want to do something based on what number was passed in. So, we have all of these IF statements here to check for the different cases. So, if the number is "1," then we just write out "One" in the Console. Else, if it's "2," we write out "Two." If it's "3," we write out "Three," and the same with four and five. While this is totally valid, and it should work, in fact, if we run our programme and type in a number- I'm going to type in "Three" here, we can see that it prints out "3." However, whenever we are dealing with a single variable, that can have many different values, and we want to do something based on the value, that is the perfect example of where a switch statement is really useful. So, instead of writing this chain of IF statements here, we can actually delete that, and we can write "switch." We then give it the variable that we want to check for. In my case, I called that "num," and then open and close some curly brackets. Inside of these, we can then create code for the different cases. So, in the case that "num" here is one, so "case 1:" and then underneath here, we put in what happens. So, in our case, we want to want to write out "Console.WriteLine("One");" We can multiple lines of code here, we can do more stuff, or we could just end it here by typing "break;" So, that is the syntax. So, we're basically saying that we want to switch based on the "num" variable, and in the case that it's "One," we go ahead and do something. In our case, we write out "One" and then we break. We can do that for all of the different cases. So, we could go "case 2:," so in the case that this number is two, we write out "Two," and then "break;" We can keep going like this for all of the different numbers, so I'm just going to do that. (smooth music) So, as you can see, while this works in the exact same way as before, and it takes up just the same amount of space, it actually makes it much easier to tell what's going on. We're switching based on the "num" variable and we can clearly see the different cases. The cool thing about a switch is that we're not sure that we're going to pass in a value between one and five. So, we can actually put in a default case here, which is going to happen if none of these cases are met. So, in this case, we could just write out "Console.Writeline("Default")," and then, again, a "break;" Now, if we run this programme here, and if we type a value that isn't between one and five - let's say, "7," we can see it says "Default." So, for all other values that aren't added as a case here, it's just going to go to the default state. So, that's a switch statement. There's really not more to it. You can choose if you want to use this or IF statements. It's completely up to you. That's pretty much it for this video. If you enjoyed it, make sure to subscribe and ring that notification bell so you don't miss the next one. As always, you can find my solution to this week's challenge on the Brackeys forum. There's, of course, a link to that in the description. Also, make sure to go check out the complete C# masterclass by clicking the link in the description. On that, thanks for watching and I will see you in the next video! Thanks to the awesome Patreon supporters who donated in June, and a special thanks to Nubby Ninja, faisal marafie, Lost to Violence, Loved Forever, SRT Mike, Piano Sathornlak, Replica Studios, Jason Uritescu, Léo Lesêtre, Alison the Fierce, Michail Korobov, Owen Cooper, Gregory Pierce, Naoki Iwasaki, Dante_Sam, Donatien Gascoin, SiriusWolf, TheMightyZeus, Jacob Sanford, Marc-Antoine Girard, and Erasmus. You guys rock.
Info
Channel: Brackeys
Views: 283,339
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, beginner, easy, how, to, learn, course, series, tutorial, tutorials, fix, tip, game, development, develop, games, programming, coding, basic, basics, C#, code, C sharp, if, statement, statements, syntax, if-else, condition, conditions, conditional, conditionals, switch
Id: u_Qv5IrMUqg
Channel Id: undefined
Length: 14min 55sec (895 seconds)
Published: Sun Jul 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.