if-else if-else statements in Java: Video Lecture 7
There are several control statements in Java, among which if-else if-else and switch statements are used for selection operations. The video lecture below explains if-else if-else statements. Programmers use if-else if-else statements for decision making in the code. That is, if-else if-else statements help in executing a part of the code based on certain conditions.
Contents
Prerequisites
Please make sure to go over the previous lecture on Boolean Variables and Relational Operators, if you haven’t already done so. The last lecture will give a good idea on Conditions, which is the basis for the current video.
The Video: if-else if-else statements in Java
The video explains if-else if-else statements in Java. It uses a simple example to explain the concept. Overall, the video will help beginners to start practicing if-else if-else statements.
The Program Used in the Video
The program demonstrated in the video is provided below. Please save the code in a file named ConditionTest.java
if you want to use the code directly. We suggest that you type the code while you watch the video. When you type the code, you practically develope an understanding regarding the syntax you type.
import java.util.Scanner;
class ConditionTest{
public static void main(String[] args){
int x;
int y;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the 1st number");
x=scan.nextInt();
System.out.println("Enter the 2nd number");
y=scan.nextInt();
if (x>y){
System.out.println("x is greater");
}else if (x<y){
System.out.println("x is smaller");
}else{
System.out.println("Equal numbers");
}
}
}
Exercise to practice if-else if-else statements
Write a program that asks the user for a GPA on a scale of 0.0 to 4.0. If the user enters a number larger than 4.0 or less than 0.0, the program must output “Invalid GPA.” If the user enters a valid number, the program should display the corresponding letter grade based on the table below.
GPA range | Letter Grade |
4.0 | A+ |
3.75 to less than 4.0 | A |
3.5 to less than 3.75 | A- |
3.25 to less than 3.5 | B+ |
3.0 to less than 3.25 | B |
2.75 to less than 3.0 | B- |
2.0 to less than 2.75 | C |
0 to less than 2.0 | F |
Please do not hesitate to send us your code via the comments section below. We will be happy to provide you feedback.
Notifications
Subscribe to receive notifications of new posts.
Subscribe to our YouTube Channel to receive notifications when we post YouTube videos.
Transcription of the Audio of the Video
From the previous video, you are familiar with comparisons and Boolean variables, we are now moving to what we can do with the comparisons. Practically, we can use comparisons for decision making in the program. That is, a part of our code will be executed if certain conditions are met; otherwise, another part of the code will be executed.
Starting of the code
I am starting my code where I will show how we can use if-else statements. The name of the class is ConditionTest; Of course the file name is ConditionTest.java. I have already written the main method, inside which I will write my code.
I am declaring two integer variables, x and y.
the variable scan is my Scanner object, using which I will get input from the user. I have written my code to get input for the first number x, and the second number y.
The first if statement
At this point, what I will do is, I will print a message if x and y have a certain relationship. The statement is written using “if” then in parentheses state the comparison. Within the parentheses, you must have a Boolean value. Notice that the comparison x>y results in a boolean value, either true or false.
Again, at first write if, then within parentheses provide the condition or comparison that results in a Boolean value. After the parentheses provide the scope of the code that should be executed if the condition in the parentheses results in “true”. You define the scope using a left curly bracket and a right curly bracket.
Anything you write inside the curly brackets will be executed, only if the condition you have provided within the “if” statement results in true. Inside the scope of the if statement I have written, I am writing a System.out.println statement. If x is greater than y, then the System.out.println will print “x is greater than y.”
Save, compile, and run
Let us save the file, compile it and run it. If the user enters a larger number for x than y, then the program should print “x is greater than y”, otherwise the program will print nothing after the user enters the numbers.
The user enters 10 for x and 5 for y. The program immediately prints “x is greater” because the first number the user provided is greater than the second number.
Let us run the code again. The user now provides 10 as the first number and 20 as the second number. Now, the program compares x>y, which results in the comparison 10>20, which in turn results in false. Since the if statement results in false, the System.out.println is not executed. The program just ends without printing anything.
Change the code to include another scope
Now, if we want the program to print something when x is not greater than y, then we can create another scope, which will be executed if the if statement results in false. What we write is, “else” and then we create another scope using left and right curly brackets. You can print anything inside these curly brackets. Anything you print via a System.out.println statement will be printed on the terminal when x is NOT greater than y. Let us just print “x is not greater.”
Save, compile, and run the code
Let us save the file, compile it, and then run it.
Now, notice that for the first number 10, and the second number 20, the program outputs ” x is not greater.”
Earlier, the program was not printing anything for 10 and 20.
Running the program again with 20 and 10. Now the program is saying “x is greater.” That is if the first number if greater than the second number, the program says “x is greater”, otherwise the program says “x is not greater.”
What happens if you have two equal numbers
Now, what will happen if the user provides two equal numbers, say 10 and 10? Based on the code we have, the program will say that x is not greater. Notice that in the code, the if statement will result in false because x is not greater than y. Therefore, the first System.out.println will not be executed. The execution will go to the else part and execute the System.out.println of the scope of the else, which is “x is not greater.”
What if we want to say something different when x and y have the same value? Practically, we have to include a scope which will be executed when x is equal to y.
In the code, currently, we have if x is greater than y, then do something, otherwise, do another thing. The otherwise part executes only when x is smaller than or equal to y.
We will separate the smaller than part and the equal part. In the else part, we will include another if statement, with a condition x is smaller than y. When x is smaller than y, we want the program to print “x is smaller”.
Save this code, compile, and run the program. When the user puts 10 as the first number and 20 as the second number, the program outputs, “x is smaller.” It is the correct outcome.
When the user enters 20 as the first number and 10 is the second number, the program prints “x is greater.” This is a correct outcome too.
Now notice that when the user enters 10 as the first number and 10 as the second number, the program does not print anything. That is, when the user provides two equal numbers, the program does not go to any of the if scopes.
“The equal to” situation
We would like to see output for “the equal” situation too.
Notice that the first if statement handles the situation with x is greater than y. The second if statement, that is with else,handles the “x is smaller than y” situation. The only situation left is the “x is equal to y” situation. Therefore, we can simply add an “else” to handle the scope of “x is equal to y.” Inside the scope of “else”, we write “equal numbers,” within System.out.println.
We have now written a program that takes two numbers, x and y, from the user and gives an idea of which number is larger, or smaller, or if they are equal.
Let us save the file, compile the code, and run the program. Notice that for equal numbers, the program now outputs “Equal numbers.”
The general structure of if-else if-else statements
[Show the following on screen]
if (condition 1) {
------------
} else if (condition 2) {
------------
} else if (condition 3) {
------------
}
------------
------------
} else if (condition n) {
------------
} else {
------------
}
[End of — Show the following on screen]
Here is the general structure of “if then else” statements. The first “if” statement is straightforward. If more “if” statements are required in a sequence, they must be preceded by an “else”. You can write as many else if conditions as you need. You may end the if-then-else sequence with just an “else”. The last “else” will cover all situations that are not covered by all the if and else if statements you have in the sequence.
This overall sequence is called if-then-else statements. One thing I should mention is that, each scope under an “if” statement or an “else if” statement or an “else” statement may have many lines of code depending the problem the programmer is trying to solve. In our example, we just had a System.out.println statement within each scope.
Once a condition of the sequence is satisfied, the corresponding scope is executed. Nothing else will execute. The purpose of if-then-else sequence is to execute only one scope.
Conclusion
Please practice what we have discussed today. For exercises, please visit the link provided in the description. Subscribe to this YouTube channel and to our website Computing4All.com. We do not assume that you know anything about programming. Our video lectures, so far, are prepared for beginners regardless of background. Please start from the beginning of this lecture series if you are entirely a new learner.
Thank you very much for watching the video.