From the course: Java 11+ Essential Training

Evaluate conditions with if-else - Java Tutorial

From the course: Java 11+ Essential Training

Start my 1-month free trial

Evaluate conditions with if-else

- [Instructor] Writing conditional code is a common practice in all programming languages. The most common form of conditional logic is an If-Then or an If-Else statement. I've already used If statements previously in this course. They're pretty unavoidable. But now I'll go into the details of the If keyword, its associated keyword Else, and how to put these keywords together. In this starting code I've created a scanner object, as I've done previously, and I'm asking the user to enter a number between one and 12. And I'll evaluate the number that they enter. First, I'm going to declare a variable named Message. It'll be a string, but I'm not assigning it a value yet, and that's because the If statement is going to result in setting that value, depending on the number the user enters. An If statement starts with the keyword If. Then you put a Boolean expression, an expression that equates to true or false, into the parentheses. I'll do a little bit of error checking. First, I'll make sure that the month number the user enters isn't less than one, then I'll use an Or operator, and I'll check that it isn't greater than 12. If either of those conditions are true, then I don't have a valid month number. So I'll set the message to, "That isn't a valid month." Next, you can add as many Else-If clauses as you want to. These will only be evaluated if the first If statement is false. Once again, you put a Boolean expression inside a pair of parentheses. And if I have a month number that's less than or equal to three, then I'll say, "That's in quarter one." Now I'll duplicate these two lines of code. And for this next version I'll check whether the month number is less than or equal to six. And if that's true, then I'll say, "That's in quarter two." And again, you can have as many of the Else-Ifs as you need, but then at the end, optionally, you can have one Else clause. And this code will take over if none of the other conditions were true. And here I'll set the message to, "That isn't in the first half of the year." And finally, after the entire If clause is done, I'll output the message. This is a good time to talk a little bit about variable scope. The reason I declared the variable above the If clause is because when you declare a variable within a code block, it's only visible within that code block. When the code block is finished, the variable expires. So by declaring it here, and then initializing it in one of these sections, I'll know for sure that by the time I get to the end, I have a valid message. So now I'll run that code. And first, I'll enter a value that isn't a valid month. And I get that message. Then I'll run it again, and enter a value that does match one of the conditions, and I get the message I expect. Conditional logic can be based on information the user provides like this, or from a data store, or from information that you get from the system the application's running on. I'll comment out this code that's getting the month number from user input, and instead I'll get the current month from the system that's running the application. I'll create a variable that I'll name Now, and I'll get it from the expression Local\DateTime.now, as a method call. Local\DateTime is a class that was added to Java in version eight, and it's a member of the package Java.Time. It returns the current moment in time as a numeric value. And then from there, I can get the month number from the expression now.getMonthValue. The month number will be from one to 12. Notice that IntelliJ IDEA tells me that I have to get a value from one to 12 now, so my first If statement isn't going to be affected. So I'll turn my first Else-If into just an If statement, and I'll comment out this bit of code. I added comments by selecting some lines, and then pressing CMD/ on Mac or CTRL/ on Windows. Then I'll fix the indentation, and I'll run the code again. And now I'm getting the month automatically. I'm running this code in the month of October, and so I get the feedback, "That isn't in the first half of the year." If you're running it in a different month, you might see something different. So that's a look at how you can use simple If, Else-If, and Else clauses to evaluate conditions, and execute conditional logic.

Contents