if, else if, and else Statements in Java
A fundamental part of programming is the ability to create different flows in an application. For example, we may want to show certain text to the user when they've won a game. This is where the if
, else if
, and else
statements in Java come in handy. Code can be executed if it meets a certain condition.
What Are Conditions?
Conditions are what is placed between the parentheses of if
, else if
, and else
statements. They evaluate to a true or false value and tell the program whether a block of code should be executed. When the condition is true, then we execute some code corresponding to the if
statement. If the condition is false, then the corresponding code gets ignored.
if Statement
The if
statement is the first condition we want to test for. If the condition is true, the next line or block of code gets executed. Otherwise, if the condition is false, the corresponding code is not executed.
Let's say that we want to tell the user that they passed when their grade is 70% or higher.
int percentOnExam = 80;if (percentOnExam >= 70) {System.out.println("You passed!");}
Since 80 is greater than or equal to 70, the if
condition evaluates to true. Since this is true, the block of code gets executed, and we print out "You passed!"
Alternatively, since this is one line, we could have just written this as:
int percentOnExam = 80;if (percentOnExam >= 70)System.out.println("You passed!");ORint percentOnExam = 80;if (percentOnExam >= 70) System.out.println("You passed!");
If the condition is met, Java will execute the next line of code or block of code. If we only have one line, we could omit the curly brackets. This results in less lines of code, but could be harder to read and easier to introduce bugs.
For example, suppose if the user passed, we also want to tell them "Congratulations!" In the below example, the user does not pass but will see the "Congratulations!" text being printed out.
int percentOnExam = 60;if (percentOnExam >= 70)System.out.println("You passed!");System.out.println("Congratulations!");
The bug is the fact that there are no curly brackets wrapping the two lines of code. Because of that, the if
statement only corresponds to the next line of code. This means that "Congratulations!" will always be printed out regardless of what percentage you received.
This would be the correct implementation:
int percentOnExam = 60;if (percentOnExam >= 70) {System.out.println("You passed!");System.out.println("Congratulations!");}
Now, if your percent on exam is not passing, you won't see a "Congratulations!" message.
else if Statement
The else if
statement is useful when we want another condition to be evaluated if the previous condition is false. In order to use the else if
statement, there must be a preceding if
statement.
For example, suppose we want to tell a user they received a perfect score on an exam if they received 100%. And if they didn't but passed the exam, we just tell them they passed:
int percentOnExam = 90;if (percentOnExam == 100) {System.out.println("You got a perfect score!");} else if (percentOnExam >= 70) {System.out.println("You passed!");}
Using the else if
statement, we can test for other conditions in case the first if
condition evaluates to false. Once a condition evaluates to true, then any following else if
statements will be ignored.
Here's an example of chaining multiple else if
statements to show the letter grade a user received on an exam:
int percentOnExam = 90;if (percentOnExam >= 90) {System.out.println("You got an A");} else if (percentOnExam >= 80) {System.out.println("You got a B");} else if (percentOnExam >= 70) {System.out.println("You got a C");} else if (percentOnExam >= 60) {System.out.println("You got a D");} else if (percentOnExam < 60) {System.out.println("You got an F");}
else Statement
In the previous example, if we know the student did not receive an A, B, C, or D, then we can safely say they received an F. This is where the else
statement is useful. If all the if
and else if
conditions evaluate to false, then the else
statement will run.
The code could then be written as:
int percentOnExam = 90;if (percentOnExam >= 90) {System.out.println("You got an A");} else if (percentOnExam >= 80) {System.out.println("You got a B");} else if (percentOnExam >= 70) {System.out.println("You got a C");} else if (percentOnExam >= 60) {System.out.println("You got a D");} else {System.out.println("You got an F");}
The else
statement acts like a default value if every condition before it evaluates to false. It can be used as long as there is an if
statement first. We don't even need to have an else if
statement as shown:
int percentOnExam = 90;if (percentOnExam >= 70) {System.out.println("You passed");} else {System.out.println("You failed");}
Conclusion
Here we learned how to use the if
, else if
, and `else statements. Knowing how to use these are a fundamental part of programming because they allow different flows and logic into an application.