C# Tutorial 3 Conditionals While Exceptions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to part 3 of my CVR video tutorial in this part of the tutorial we're going to cover if-else elsif the ternary operator switch go-to while do wild ception handling and a whole bunch more like always all the code is available in the description underneath of the video as well as a transcript for the video and I have a lot to do so let's get into it alright so here we are and I'm sure you're very well aware I'm using Visual Studio here but you can use the Maron or whatever you want to use the very first thing I'm going to talk about are what are called conditionals and they just mean that we are going to form certain operations under certain conditions now you are going to have relational operators and this is how we are going to compare values which are greater than let's say greater than or equal to less than or equal to equal to and not equal to you're also going to be able to use logical operators to combine these two little ways of comparing and they are going to be and or and not and now what I'm going to do is show you a whole bunch of examples of how those different things are used let's say you have an age which is equal to 17 and you want to decide what grade this person should go into so what we're going to do is we're going to say if the age of this student is going to be greater than or equal to 5 and if the age is going to be less than or equal to 7 in that situation what we are going to do is we are going to print out on to our screen a message that's going to say this student should go to elementary school so we'll say go to elementary school yes I know they should go to kindergarten if they're 5 but whatever then what we're going to be able to do is make another condition here we will say else is and we're going to be comparing multiple things again so we're going to say if the age is greater than 7 and both of these have to be true or it's going to come back as false of course age is less than 13 in that situation let's go and copy this we are going to say that they need to go to to school and there that is and let's throw another else--if here we'll say else if and in this situation what we're going to do is we're going to say if age is greater than 13 and the age is less than a 19 in that situation we are going to say that they need to go to high schools so let's go throw that in there then we're going to have a condition in which none of these are true and this is like a catch-all for everything and here we'll say go to college alright so there we are that is elseis and if and else and all those other different things and how you can use them now let's go and give you another example using or we're going to use or anytime we want to say that something is okay as long as either of these is true so here what we're going to do is we'll say age is less than 19 in that situation if either one of those is true we're going to say that this person you shouldn't all right so that's an example of how we can use or and let's go and see not as well so what not does is it basically just takes a true and turns it into a false or it takes a false and turns it into a true oh and by the way you can also combine string operators just by going like this and throwing a plus inside of there trying to cover every way of doing everything and throw in a closing parenthesis and let's run this just to see what happens all right and you can see right here go to high school you shouldn't work and it turned true into false and I was using right instead of right line and that's reason why we're getting some of those weird things but either way I now I'd like to talk about the ternary operator and what it's going to do is it's going to assign one value if a condition is true and otherwise it will assign the second value so of course that's going to be it doesn't have to be true or false but it's very often used for true or false statements so here what we're going to say is if the age is greater than or equal to 16 then we are going to line a value of true so if that's true it's going to assign a value of true to can drive and otherwise it's going to assign a value of false all right so that's how that works and then we have the switch statement which is going to be used whenever you have a limited number of options and just so you know there is no way of checking ranges and that in my opinion is a short-sighted thing with c-sharp I really really wish that they would make it so that you could use ranges inside of switch statements like you can with Python for example if you want to check multiple different conditions you have to stack these cases so exactly like what I'm doing right here so what I'm saying here is if the value of age is equal to one or two in that situation I am going to print a message out on our screen here and that message is going to be go to day care all right then what we're going to do is we're going to check look make sure you throw a break statement inside of here get rid of that throw a break statement inside of here you must always do that and that's going to throw us completely out of the switch statement that's what break does in that situation so then what we're going to do is we're going to say if the age is three or if the age is four in that situation we're going to say go to preschool so go to preschool and there that is and then we can say we can throw spaces inside of here if we'd like we could say if the age is five whoops make sure you throw a break inside of here however I'll make sure y'all saw a break inside of there otherwise it will get cranky with you and then we'll say something like go to another school go to another school just end this never-ending list of different things like that and we will say break now let's say kindergarten go to kindergarten kindergarten there we are and close off that break statement and then what we will do by default if none of those are true we will say in that situation go to their school so go to another school just throw something in there then something else we can do is use a go-to statement what this is going to do is jump us out of the switch block and to wherever we define or go to so that's another way of ending this if you don't want to use break then what you can do and go to is a very bad thing to use and I personally never use it but just want to cover it because you may see somebody else use it so what's going to do is jump out of there jump to where our other school exists and do whatever is in here so what we're going to do is we are going to say elementary middle arms high school just throw them in there alright so that is switch and that is go to and then what will happen will jump out of here and it will print what is inside of there and otherwise it will just skip this code all together another things important is if you want to compare strings it is best to use the equals function so we're going to say let's define a person's name and let's go and define another name and let's go and have them be equal if you want to check equality between two different strings you should do something like this and we'll say name equals and then we're going to throw inside of here the name we want to compare it to and then you're going to put and define what type of string comparison you are doing so we're going to say string comparison throw this down here and then specifically ordinal and that is way of checking to see if there are equal strings or if these strings are equal and let's go through that and we'll just say names are equal so that there throw that there and run it and you're going to see go to another schools what comes up here because the age was 17 then it jumps to the go to printout elementary middle and high school and then it says the names are equal alright so that is a rundown of conditional statements and of course that this tutorial continues I will do or with them and now I want to talk about the while loop alright so you're basically going to use the while loop in comparison to the four and B for each loop whenever you want to continue execution as long as a condition is true so let's create a while loop that's going to print out odd numbers between 1 and 10 so we'll go in I is equal to 1 and another thing to know about the while loop is you have to always define the iterator for the while loop outside of the while loop before you define the while loop so here we're going to say while I is less than or equal to 10 in that situation we are going to do some things now remember we want to print out odd numbers one way to do that is to use a mathematical operator called modulus which is a % and what modulus does is it returns the remainder of a division so if we want to find out if something is an odd number what we're going to do one thing we can do here is we can do modulus 2 and we know that if this is equal to 0 well that means this is an even number so in that situation we are going to increment the value of I and this is exactly the same as if we would say I is equal to I plus 1 and then what we're going to do is we want to jump back to the beginning of the while loop and start execution over again and to do that we use continue all right so this is going to say if it is an even number we want to increment it to the next number which is going to be odd and then we want to jump back up here and start executing again now what we're going to do is I'm going to show you the break statement again remember the break statement jumps us out of loops so let's say we decide that we want to break out of here if I is equal to 9 so we can do something like I is equal to 9 and then we can just throw the break statement right here since it is going to be you know just one thing is going to occur after this if statement then what we're going to be able to do is jump in here and then we will print the value of I out on the screen let's get rid of that quote so don't just do that like us and then what we can do is we have to remember to increment the value lie all right so if we do that and we run it you're going to see that it prints only the odd numbers and it doesn't print nine all right so pretty cool stuff with a V while loop and of course we will be doing much more with the while loop as the tutorial continues because I really like while loops doesn't take much to get me excited obviously so now we're going to Dominica and talk about the do-while loop now you're going to use the do-while loop whenever you want to make sure that you execute the code inside of your block at least once so what I'm going to do here to show you how to generate a random number so we're going to say random and I'm going to call this R and is equal to new random create a random object and then what we can do is we can say something like secrets number is going to be equal to and we want to generate random numbers between 1 and 10 to do that you go 1 and 11 and throw that like that and then we're going to come in here and we're going to have like a number guessing game so we'll say guess is equal to zero and then we will start our do-while loop so we're going to say do and then we will have all the things that we want to have it to so we'll say console rights and I'm moving too fast of course hit the pause button here must say enter a number between 1 and 10 and we're going to make this be a right so that we'll have room to put it in there and then after that we want to get the number that they type in so we're going to type in number guessed is going to be equal to and also we want to convert the number that's passed in which is going to be a string by default into an integer and another way of doing that is by using converts I've showed you three different ways to convert into multiple different numbers or data types and this is just another one of those so we will say console and read line like that and we will get that and store it into number guests now what we can do down here is after the curly bracket we can say while and put our condition inside of here so we're going to continue looping as long as the secret number is not equal to the number or guest in there that is and make sure you put the semicolon there that is very very very common error and now you will not make it and if you're wondering if there are any other ways of converting yes indeed here are other conversion options you can use two boolean instead of two int 32 and 32 is a regular integer two in 64 is a long by the way so you have two boolean 2 by 2 character to decimal 2 double 2 in 64 which is exactly the same as long to string of course you've seen that before and they are all going to convert from any type into any other type right so useful way to do conversions between different data types and if we run it so we just create a game enter a number between 1 and 10 can I get it the first time let's say 7 no is wrong let's try 1 nope 2 no 3 or 4 I guess I hit and 4 was the number all right and it would have been a better game if it would have told me I guess the number but I forgot to type that in and that's not a big deal and now what we're going to do is talk briefly about exception handling because I have a little bit more time to go now basically we use exception handling to catch errors that could potentially crash our program what is an example of an error like that well let's say we have num1 is equal to 5 and we have another double which is going to be num2 which is going to be equal to 0 what I'm going to do here is try to divide by 0 actually c-sharp does not throw an exception in this situation so what I'm going to do because I know it can't be done I am going to throw exception so I'm going to create my own static function down here it is going to return a double and I'm going to call it do division and it is going to receive a double and we will call this X and it's going to receive another double that we will call Y and it's going to try to perform this vision right here so what I'm going to do is I know ahead of time that you can't divide by zero so I'm going to come in here and say if the value of y is equal to zero well then I want to throw an error and in the next part of the tutorial I'll do something a little bit more complicated and the error that I want to call here is division by error exception so I want to say hey you can't do that and you have to put the parentheses on it right there and then what I'm going to do is otherwise if they do not send a value of zero I'm going to say okay I can do that division and return the answer okay so now up inside of here I know that there's the potential for an error to occur whenever this function is called so how am I going to handle it well I'm going to surround any code that could potentially cause an error with what's called a try block and here what I'm going to do is I'm going to say console and write line and I will say five divided by zero is equal to and then I will go do whatever I'm going to do here jump down to the next line and then I will call my do division function and then I will pass in num1 and num2 knowing that it's going to cause an error and then afterwards what I'm going to do is I'm going to catch that error to keep the program from crashing and the specific error I'm going to catch is the divide by zero exception so I'm going to come in here so if that error is triggered I am going to catch it and I'm going to do something with it what I'd like to do is go and copy this is print out some information and so let's just keep it simple and we'll say something like you can't divide by zero there's that error and that's going to print out and otherwise if I didn't have that it would crash the whole entire program so that's why I'm going to keep it that way then you can also go and print out additional information to the screen by referencing functions available in this e X variable up here and one of them is let's just go and print the error message we can say gets five and we can go name and this is going to give us the type of exception that occurred and let's go and also print out the message associated with that and to do that we just type in message like that and that's going to give us the error message no other thing if you would just come in here and just type in exception like this with e^x that is going to be a catch-all that is going to catch every single exception that is ever triggered and we'll get more into that later on but I just want to talk well let's just do it so we could come in here even though this isn't going to be triggered we can say exception e^x and then you can call this anything it doesn't have to be called e^x and in this situation we'll just go and print out exactly the same type of stuff so we'll just copy this and we will paste it in there and we'll say something simple like because we don't know what error occurred we'll just say an error occurred and there we are so there is that guy that's going to be called if the exception isn't caught by specifically naming exception like we have right here and then you can also use another function or another block here called the finally block and it is always going to rub it's always going to run and it's going to provide for cleanup after you handle all these exceptions so in here and we'll do something like this and we'll just type in cleaning up whenever we get into files and other stuff I'll do more with exception handling and if we run this guy we are going to see that it says you can't divide by 0 it prints out the actual exception and it prints out the message for the exception which is attempted divided by 0 and then cleaning up shows up here from the finally block all right so there is a rundown of some conditionals and some additional looping structures we haven't covered and a brief introductory situation in which we covered exception handling and like always please leave your questions and comments below otherwise till next time
Info
Channel: Derek Banas
Views: 56,590
Rating: undefined out of 5
Keywords: C# Tutorial, C#, Learn C#, C Programming
Id: pVxJT3VxD0c
Channel Id: undefined
Length: 19min 45sec (1185 seconds)
Published: Sat Jan 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.