Fast Conditional Branching In Java Made Simple With If, Else If, and Else Statements | Java Part 3

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone let's see if we can get this intro to this java tutorial done in under 30 seconds today we're going to be covering conditional branching which basically means we're going to have if statements else if statements else statements we're going to cover some boolean algebra so if something's greater than or less than something if it's equal to something or if it's not equal to something this is all stuff we're going to use when we're calculating some ages and you can apply this to like character levels and stuff like that in video games if you want to that's enough for me though let's go ahead and let's jump into some code okay so we're going to be taking a look at conditional branching so we're going to do if else if and else statements today and we're going to start by i guess we're going to create like a basic scenario we can work with a popular example people like to use is like the legal drinking age so i guess we'll just stick with the cliche and then when we cover switch cases in the next video we can move on to something more fun maybe we'll do like pokemon or something but okay so we'll start with an int we'll say int i don't know age i guess my age equals 25. so for the sake of this example we'll say that i'm 25 years old so let me just comment that for this example i'm 25 years old okay cool now we have our age we also need our legal drinking age so we'll say int legal drinking age equals 21. and we'll add a comment to this as well that says this is for the united states and then i guess we can just consider the people in europe um lucky in this in this scenario i guess but okay we have our age we have our legal drinking age now what i would like to do is to compare my age to my legal drinking age and see if i'm old enough to drink so the naive solution which won't work of course but that's fine one of the core concepts of software engineering is to try the thing that doesn't seem like it'll work at first and then keep going until you have something that works and then you can always make it better afterwards so let's see if we can solve this problem so the obvious solution is just system that out that print line um you are old enough to drink because you are plus age so i guess my age right so you're old enough to drink because you are 25. so let's run this run this code see what happens you're old enough to drink because you are 25. okay cool now what happens if i change this and i say i am five years old and i run this and of course you already know where this is going um it's not going to work it says you're old enough to drink because you're five now that might be true in some places but it's definitely not true in the united states so how can we make this only print out if you're old enough to drink well let's change this back to 25 and let's try using an if statement so for this the syntax is if parentheses and then your condition so i guess for now we can say true if true then i want to print this so this will always be true because i'm always passing in true and if we run this it'll be just like before except now it's inside of this if statement that's always true if i make this false and i run this code this won't print this will never execute you'll never go inside this if statement nothing will print out i'm going to come down here just for the sake of seeing when the program ends and i'm going to say system.out.println and let's just do a whole bunch of exiting program super seriously we're done here and then we'll do some dashes or whatever just take up a lot of space so we can see where the end of the program is i'll run this we can go through here and it says exiting program super seriously we're done here okay cool so now what if we want this to print if you're old enough well if we remember from our math classes we can take my age and we can take the legal drinking age and we can compare these two this works just like it does in math so you have one on the left one on the right and then you compare it if you want my age to be bigger than the legal drinking age you can use the greater than symbol which means this number if this number is bigger or greater than this number do this thing or you can use less than so if my age is less than the legal drinking age then do this or you could say you know you could replace this with this and put my age over here and you could say if the legal drinking age is less than my age print this because if i'm 25 and legal drinking age is 21 21 is less than 25 so print this and this would still be right personally i like to have my age out front and the legal drinking age behind it just because that makes more sense to me but feel free to try out both versions because the more you try out stuff like this the more familiar you get with it more experience the better you get so don't think that you have to do it exactly the way that i'm trying it in fact i would encourage you to try and break things because that's where you do most of your learning but okay we can run this we can see if this will at least print out the legal drinking age it says you're old enough to drink because you're 25. if i change this to 5 and i run this then we'll see if this actually executes and it does not it just says exiting program so that's pretty cool now what happens if i want it to say you're not old enough to drink if you're not old enough to drink well we can come over here after the last brace from this if statement we can say else and this one doesn't need a condition because this will just run any time this if statement is false so if this isn't true then do this so this doesn't need a condition here like you don't need parentheses here that says true or false you just need else and your braces and then in here we can say system.out.println you can't drink yet what are you doing and we'll spell it w-a-t-e-r because that's honey and then we can try this out so my age is five if my age is less than legal drinking age you can't drink yet what are you doing cool now what happens if i do this if i say my age is equal to 21 or even more specifically if my age is equal to the legal drinking age so what happens here so if my age is 21 and the legal drinking age is 21 and we say if my age is greater than the legal drinking age well 21's not greater than 21 so we're going to come into this else statement right if we run this we can actually see that that's true it says you can't drink yet so one way to fix this would be to take the legal drinking age move it down one and then we say if you're 21 or older then you can drink but of course this is confusing because the legal drinking age is actually 21. so we're kind of lying by changing it to 20. and we can't change our age to 22. it doesn't work like that what we can do we actually have two solutions here the first is an elsa statement and the second is to change this little piece of conditional logic here so we'll come back to this and we'll do an elsif first because it's a bit more complex and then we can make it a bit simpler and easier to read so we'll move the else down here and we'll say else if and because this is another condition that isn't the default case we actually need parentheses for this one so we'll say else if my age and then double equals to say it's to actually do the comparison so if my age is equal to the legal drinking age and we need some braces and we can move this else up here and add a space so if my age is exactly equal to the legal drinking age system.out.printline i can't believe you are old enough to drink you're plus my age so it's gonna say i can't believe you're old enough to drink you're 21. so if we run this with my age set to 21 and the legal drinking age set to 21 they'll say i can't believe you're old enough to drink you're 21. cool now the other thing we could do if we didn't want to specifically print out a special message here and we just wanted it to say you're old enough to drink you can actually get rid of all of this and you can say if my age is greater than or equal to the legal drinking age print out that you're old enough to drink of course this is a bit simpler because it's just one extra symbol but you kind of lose that flexibility of the custom message if you don't need a custom piece of logic for your elsif then this is definitely a good way to go of course this works with less than or eager equal to or you could even say if my age does not equal the legal drinking age so if your age is anything except for 21 print this so if we run this and my age is set to 22 for example we'll run this um it says you're old enough to drink because you're 22. if i change this to 2 it'll also say you're old enough to drink because you're 22. or because you're two sorry and if we change this to 21 in this case my age cannot equal the legal drinking age so instead it'll say you can't drink yet what are you doing so that's sort of how you can make sure that one variable does not equal the other variable um and then i guess the last thing we can cover is a bit of it's called boolean algebra but you have this idea of ands or ors which is a confusing way to put it but hear me out if my age is equal to the legal drinking age or my age is greater than the legal drinking age then we want to print this out so this is just a way for you to combine two conditions into one if statement and of course you can keep this going and you can say or or over here and then just keep going and add as many as you want to i don't usually like to add a whole bunch of them because it makes it really hard to read and the general rule of thumb is you should always make your code readable for the most junior person that you expect to see the code so you know a good thing to do while you're learning is to remember what it felt like to learn so that when you get better you can write code that past you would be able to read at least that's how i try to think of things one thing to keep in mind here is sometimes your logic can get a little bit messed up and it might group the wrong things together so what i'd like to do is put these explicitly in parentheses so that the code knows it's this condition or it's this condition and one other thing you can do here is you can actually take this whole thing right here and say uh boolean exactly old enough to drink oops to drink equals this condition and then we can do boolean older than legal drinking age equals this condition and then we can take this variable right here put it in the first part take this variable right here put it in the second part and this works exactly the same way if we were to run this we're 21 years old which means we are exactly old enough to drink and because this is an ore only one of these two needs to be true so because this is true it'll go straight in here they'll say hey you're old enough to drink right here it says hey you're old enough to drink um we can also do and so if you're old enough to drink and you're older than the legal drinking age which is going to be pretty hard because this one right here needs to be greater than 21 and this one needs to be exactly 21 so these two can't ever be true but what we could do is we could say if you're exactly old enough to drink or you're older than the legal drinking age and then we'll create a new boolean up here and we'll say boolean you like drinking equals true so if you're old enough to drink and you like drinking then do this and in this case you would want to wrap this in parentheses like so because you sort of treat this as one condition and this is the other condition of course you can do the same thing as before so you can grab this whole thing cut it out and say boolean can drink equals this and we can say if you can drink and you like drinking then we can say i don't know go have a drink else um i don't know you either can't drink or you don't want to live your life though so we're not telling them they're bad because they can't drink or they don't want to we're telling them they're you know they're okay they can keep living their life but we cover both of these cases if we run this with someone who's 21 who likes drinking it prints out go have a drink but if we say they don't like drinking this will say hopefully you either can't drink or you don't want to okay but what if they do like drinking sorry i just um lost power in one of my monitors um okay so if you like drinking which is true and your age is 20 and we run this we know that we can't drink now so hopefully it'll say you can't drink or you don't want to now of course you do like drinking but you can't drink right because this is false but this is true and both of these have to be true because this is an and a bit later down the line we can cover some of this boolean algebra which is just ways to use the ands and the ors to make some some cooler logic it's a bit more complicated but usually the ors and the ands are all you need plus these uh equals not equals greater than operator so the comparison operators plus the ors and the ands basically gives you everything you need as long as you have your if else if else statements as well but that's going to do it for the code part of this tutorial let's go ahead and let's jump to the outro video okay and that's going to do it for this java tutorial hopefully this helped hopefully i'll see you in the next one don't forget to subscribe and like and all that other youtube jazz unless this video didn't help and youtube hasn't taken away the ability to dislike a video yet then feel free to dislike it but you know that just helps improve the platform if you do that so remember to dislike your videos which seems a bit counter-intuitive for a youtuber to say but what are you going to do that's going to do it for me though next time we'll talk about switch cases and maybe some other stuff i don't know yet i don't really plan these things until i sit down at 2 am on a work night to do them uh but that's going to do it for me thank you so much for watching i appreciate you guys and i will see you in the next video have a wonderful day [Music] you
Info
Channel: Deanin
Views: 101
Rating: undefined out of 5
Keywords: deanin, deanin java, java, java tutorial, if, else, else if, java if, java else, java else if, java condition, conditional branching, conditional branch, java conditional branching, java conditional branch, java control flow, control flow, java logic, learn java, java programming, java if else, java tutorial for beginners, if else, java if else statement, if statement, java for beginners, java course, java if else statements tutorial, learn java for beginners, if-else
Id: mziep7CQ_O8
Channel Id: undefined
Length: 16min 1sec (961 seconds)
Published: Wed Jul 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.