Contents
Control Flow Statements
In this course, we look at control flow statements in the Java programming language.
Learning Objectives
- Decision-making statements
- Looping statements
- Branching statements
Intended Audience
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
Prerequisites
- No prior knowledge is required about the Java programming language.
- Basic computer knowledge
Hi there. in this video, we'll learn about the if-else-if ladder statement. Let's take a close look at the if-else-if ladder statement and nested if statement syntax. First, the Java if-else-if ladder statement executes one condition for multiple statements. Second, the nested if statement represents the if block within another if block. Here, the inner if block condition executes only when the outer if block condition is true. Let's make some examples with if ladder and nested if statements. In exercise project, right click on the decision making package and select "new class".
Specify the class name as IfLadderNestedIfStatement and select the checkbox for the main method. First, we need to import the Scanner class for getting input from the user. Scanner age = new Scanner (system.in). If you notice, we use the import Scanner class in java.util package to import the Scanner class. First, let's show a message to the user to enter age by using the println method. System.out.println("Please enter your age") and we declare a variable "your age" with int type and assign the next int method of Scanner class to this variable.
Let's start to write if statement. if(yourAge < 13), then we will print "You are a child" to output. Else, if(yourAge < 19), we will print "You are a teenager" to output. if(yourAge > 19), the else statement is executed and we will print "You are an adult" to output. If all test conditions are false, code inside the body of else is executed. Finally, we have to close the Scanner object by using the close method. Okay. Let's run the code. In the console screen, we enter 8. 8 is less than 13. So, first if statement is executed. The output is "You are a child". If you notice, it's provided in the condition we checked in the second condition, that is, 8 is less than 19, but the first condition is satisfied.
It does not deal with the other conditions. If this was just "if" instead of "else-if", then it would also check for other conditions because the "if" was a block by itself. We explained this in our previous lesson. Let's continue now. Let's run the code again. We enter 15. The program checks first if statement, 15 is less than 13. So, the first if statement is not executed. The second if statement is checked. 15 is less than 19. Yes, this condition is true and this statement is executed. So, the output is, "You are a teenager". Let's run the code again. We enter 45. The program checks first if statement: 45 is greater than 13. So, the condition is false. Checks second if statement: 45 is greater than 19, so the condition is false. The else statement is executed.
So, the output is, "You are an adult". Let's add a nested if statement in the else statement. If your age is less than 65 then we'll print "You are an adult" to output. We add else statement. Means that if(yourAge > 65), we'll print "You are a senior" to output. Okay. Let's run the code. On the console screen, we enter 56. Else statement is executed because 56 is greater than 13 and 19. In the else statement, 56 is less than 65. So, the if statement is executed and the output is "You are an adult". Let's run the code again. We enter 68. Again, the else statement is executed because 68 is greater than 13 and 19 too. In the else statement, 68 is greater than 65.
So, this time the else statement is executed and the output is "You are a senior". Let's write a Java program to find the largest of three numbers and implement a nested if statement. First, I will convert these codes to the comment line. Again, we'll use the Scanner class for getting input from users. Scanner number = new Scanner (system.in). Also, let's declare four variables. Their names can be number1, number2, number3, and the largestNumber with int type. Now, let's show a message to the user to enter the first number by using the println method. System.out, but I'll also write the purpose of this app to the user. So, I write ("This app finds the largest number\n Please enter the first number").
The \n Is a common escape character in Java. With \n, the cursor will automatically move to the next line and the statement after \n will be printed on the next line. So, you can write a string expression one after the other in the same print method. Let's continue. I'll assign the next int methods of the Scanner class to the number1 variable. number1 = number.nextInt. After that, let's show a second message to the user to the second number. System.out.println("Please enter the second number"), and I'll assign the next int method of Scanner class to the number2 variable. number2 = number.nextInt. After that, let's show a third message to the user to the third number. System.out.println ("Please enter the third number"), and I'll assign the next int methods of Scanner class to the number3 variable. number3 = number.nextInt. Okay. Let's start to write if statement.
First, I'll check if the three numbers entered are equal to each other. if(number1 == number2) "and" operator, we can use the "and" or "or" operator in the "if" condition. We mentioned these operators in the operator section. And if(number1 == number3), if this condition is true, I'll print the ("All numbers are the same") on the console. If three of these numbers are not equal, this time the else block will work. This is outer else. We'll add a nested if statement inside the outer else. In the else block, I'll first check number1 and number2. if(number1 > number two). Now, we'll add another nested if statement inside the outer if. if(number1 > number3), then assign number1 to the largestNumber and print it to the console. System.out.println("The largestNumber = " + number1), and I'll create an else if statement for the equality of the number1 and number3. Else-if number1 is equal to number3. If this condition is true, the number1 and the number3 are the same and they are the largest number.
So, I'll print this message to the console. S out println number1 + " a space and a space" + number3 + double quotes a space the message will be "are equal and they are the largest numbers" And the last else statement, in this case the largest number will be number3. Because in this if block, the number one is greater than the number2. And in this else block, there is only one case left. The case where number3 is greater than number1. Therefore in the else block will assign the number three to the variable largest number. The largest number equal number3. And let's print it to the console. S out println "The largest number equal plus number. So far, we've checked the equality of the three numbers with each other. We also checked if the number1 is greater than the number2. Now, let's look at the case of number1 and number2 being equal to each other and greater than number.
So, let's create an else-if block here. Else-if number1 is equal to number2. And number1 is greater than number3. If this condition is true, the number1 and number2 are the same and they are the largest number. So, I will print this message to the console. S out println number1 + " and " + number2 + double quotes a space. The message will be "are equal and they are the largest numbers". And let's continue with the last else statement. Here, we'll also check number2 and number3. If the compiler came to this block, it means that the number is not the largest. The only ones we didn't check were number2 and number3. So, here we will check number2 and number3. We'll add a nested if statement in the else block again. If the number2 is greater than number3, then assign number2 to the largest number and print it to the console.
S out println "The largest number = " + number2. And I'll create an else-if statement for the equality of number2 and number3, else-if number2 is equal to number3. If this condition is true, the number2 and number3 are the same and they are the largest number. So, I'll print this message to the console. S out pintln number2 + " and " + number3 + double quotes the message will be "are equal and they are the largest numbers" and last else statement. In this case the largest number will be number3. Because in this if block the number2 is greater than the number1. And in this else block there is only one case left. The case where number3 is greater than number2. Therefore in the else block will assign the number3 to the variable largest number, The largest number = number3. And let's print it to the console.
S out println "The largest number = " + number3. The app is ready. Let's run the code. First, I'll enter the same number five and five and again five. When we press the enter key, the message says that all numbers are the same. Now let's run the code again. This time I'll enter the same number for the first and second numbers, so they can be five. And for the third number I'll enter six before pressing the enter key, let's examine the code. First, it checks the equality of three numbers and all numbers are not the same. For this reason it skips the if block and continues with the outer else. In the else block, it first checks the number1 and number2. So, the number1 is not greater than the number because they are the same. So, this if condition will not execute and it continues with the else if block. In the else-if block, it checks if number1 and number2 are equal and they are greater than number3. So, the first condition is true, but the second condition is false.
For this reason this condition will be false and will not execute. So, the compiler continues with the last else block. In the else block, it checks the number2 and number3. Since the number3 is greater than the number2, The if and the else if block will not execute and the compiler will continue with the inner else. So, the largest number will be number3 and it prints this message to the console. So, let's press the enter and see the result. As you can see the largest number is six. Let's run the code once again. This time all enter the number1 and the number3 as the same. And the number2 will be small from the number1 and number3. So, the first number can be five, second can be four, and third will be five again. Before pressing the enter key, let's examine the code. First, it checks the equality of three numbers and all numbers are not the same.
For this reason it skips the if block and continues with the outer else. In the else block it first checks the number1 and number2, so the number one is greater than the number2. So, this inner if condition will execute and in this if condition first it will check if the number1 is greater than the number3. So, this condition will be true because they are the same number i.e five. For this reason it prints this message to the console and terminates the program. It will not deal with the other conditions anymore. So, let's press the enter key now. As you can see it says five and five are the same and they are the largest numbers. Yes, in this lecture we did enough examples to understand if statements. To make it clear you should do more exercise. Also you can use the break point again if you want. We talk about the break point in the previous lectures. So, let's take a short break here, we'll continue in the next video. See you in the next video.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.