From the course: Learning Java 17
Decision-making with if in Java - Java Tutorial
From the course: Learning Java 17
Decision-making with if in Java
- [Instructor] There are two main parts to the fortune-teller program. First, we ask the user to pick a number between one and 10. Then we output the user's fortune depending on which number is inputted. Let's implement it in Java. In the code, we already have a print statement that asks the user to pick number between one and 10. We also have a scanner that reads in the next integer a user inputs with the next int operation. We save the output in a variable called inputtedNum. Now we have to make a decision in our program. If the inputtedNum is less than five, the program should output the good luck fortune. If the inputtedNum is equal to or greater than five, the program should output the shoe selection fortune. Before we were just using blocks. But there's a specific type of control flow statement that starts with if so we call it an if statement. An if statement is a control flow statement where if the condition is true, it performs some kind of action. For our program, if the inputtedNum is less than five, meaning the condition is true, we execute the code and the if block or in those curly braces. The print statement would only be executed if the condition is true, meaning inputtedNum is less than five. So what if inputtedNum is not less than five? Well, then the condition evaluates to false. When we mapped out our program in an earlier lesson, we said that if the inputtedNum is not less than five, we want to print out the other fortune. To do this, we can add an else statement to the if statement. The else curly braces encompass the code that will be run if the condition is false. Similar to the if block, the code inside the else block only runs if the condition is false. We don't always have to have an else block associated with the if block, but we do in this case because we want to perform an action if the condition is not true. We want to print out that other fortune. Let's add an if statement to the code. We'll write if inputtedNum is less than five, we'll print out the good luck fortune. Otherwise we'll print out the other fortune. Only the if block or else block will be run in an execution of the program because the condition cannot be true and false at the same time. The condition can be true or it can be false, but only one fortune will be printed. This means that the code inside the if block doesn't know about the code inside the else block. And the code inside the else block doesn't know about the code inside the if block. This will become more important later on. Let's run our program. We'll pick four, a number less than five and we get the first fortune. Let's run it again. We'll input 10 and we get the second fortune.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.