Java Else If Statements Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
are you confused about EFL statements in Java I don't blame you they can be tricky but if you watch this video I promise you you won't be confused anymore and you'll have a fully working program using if-else statements what's up it's Alex back again I hope you're all having a good happy holiday this is like my favorite Christmas sweater it's got a derpy penguin on it if you're new here on this channel I make Java tutorials every week for you so if that's something you're into consider subscribing let's start off our if-else statement program by making a new Java project we'll call it if-else hit finish and then open it up right click source make a new class and we'll call this if-else again the first check mark and finish today you and me are going to make a program about great a user will enter a number grade from 0 to 100 and will return back to them the letter grade for that number will first ask the user for a grade between 0 and 100 I made a video covering how to get input from a user I'll link it on the card now if you want to check that out and to do that java wrote for us this thing called a scanner so we type scanner we name it we'll just call it scan and we'll set it equal to a keyword new scanner this just says hey Java I heard you've got this thing called a scanner I want to make one and call it scan to let it know that we're gonna make it hover over it and click import scanner and that'll bring in that code into our program to get input from a user we usually interact with this console here in Eclipse and that's called system dot in now will prompt the user to enter great so we'll print out to the screen using this handy bit of code that we use in every video we'll say enter a grade like that save it run it and we see the code there there the text there now if I type something nothing will happen because we didn't actually use it we set it up we didn't use it yet to get an integer which is a fancy name for a number like a grade we would store it as the integer we'll just call it N and we'll scan that input from the user so we do scan dot next integer just like that and for proof of concept we'll just print out and to see what it actually is so we'll run it you say enter a grade I could type in here I'll say like 90 and hit enter and that will print back out 90 so we know that n is whatever you enter but now we want to set up our cases if it's 90 or 100 we give an a if it's 80 to 90 we give it B and so on so to set up those I'm just gonna make this bigger to set up an if statement it's really easy you just type in the keyword if put some parentheses and then after that you put some curly braces now we'll say if our number n is greater than or equal to 90 and it's also less than or equal to a hundred we can concatenate those two conditions by doing ampere two ampersands like bats you'll say and it's less than or equal to 100 we will print out a so if we save this and run it and we enter something between 1959 t5 we'll get 95 hey so it looks like if what's in these parentheses is true then this code gets run so anything that's true gets run so this could be I could just say true here which is just a boolean variable which just means true or false if we ran this and entered something not in the range we'd still get a because this is true and this gets run if this was false and we saved it and ran it and even if we entered something in the range we wouldn't get anything printed out because it's false if and else if statements are mostly used for variables like this so we're going to do what we do before the next condition we'll do else if so if you have multiple paths you just tack on another else if just like if it's greater than it equal to 80 and less than 90 beat so I'm just going to expand this we will copy and paste this just to set up our other paths if it's greater than equal to 70 in less than 80 we will print out a C and if it's between I think 65 and 70 then it's a d then our last condition 0 and less than 65 then it's an F so if we save this and ran it let's say we got an 81 let's see what happens we get a B you'll actually just delete this line printing what we entered because it's a little confusing we'll say let's try 68 we get a D so based on these conditions it determines what code gets run and I'm going to space these out just because everything's a little cluttered together it's kind of hard to understand but let's try something a little different so we got a negative 4 for whatever reason nothing would happen because we're checking ranges between two numbers and we don't have any conditions for less than 0 or greater than 100 so this would be an example of just using normal else this is like anything else will be in here the number you entered is not in the range okay let's save that and run it we'll try like and try negative 12 no your interest now the range I'm just gonna quickly recap what we did we're using Java code to get input from a user it's called the scanner to do that you prompt the user to enter a grade and then we get the grade that enter if that grades greater than or equal to 90 and less than 100 we print out a if it's between 80 and 90 we know it's a B between 70 and 80 we know it's at C between 65 and 70 we notice that D and if it's between 0 and 65 we do an F but if it's anything else tell them hey it's not actually in the range it sort of gives you a taste of all three different kinds so if else if and else statements I hope it helped you I'm thinking of making a podcast programming terms for beginners it would be sort of like an informal hey this is what this word means there's no technical nonsense in the way of you understanding what a programming term means if you think you would enjoy that please let me know in the comments and that'll help encourage me to see if I should do it if this video was helpful please smash that like button and share it if you think it might help someone you know you could be anywhere in the world by you're here with me and I appreciate that so much I'll see you in the next video [Music]
Info
Channel: Alex Lee
Views: 121,993
Rating: 4.973321 out of 5
Keywords: java else if statements, java else if, java else statements, java if else statements tutorial, java if else, java if, java if else statements, java if else program, java else, else if in java, how to use if else statements in java, else if java, if else java, how to use if statements in java, if else statements in java, if else statements, java if else statement, if statement, java, if else java program, if else java tutorial, alex lee, else if
Id: yvWnj_HfG6s
Channel Id: undefined
Length: 7min 39sec (459 seconds)
Published: Thu Dec 20 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.