When you think about it, there’s a lot of “ifs” in our lives. If the grocery store is open, we can go in and buy groceries; otherwise, we cannot. If it’s 12 noon, then it is time to take a lunch break.
Programs rely just as much on “ifs” as we do. In programming, “ifs” are called conditional statements. These statements allow you to run or not run a block of code depending on whether a particular condition is met.
In this guide, we’re going to discuss how if statements work in Java and why they are useful. We’ll walk through an example to demonstrate how they work.
If Statements
Sometimes, you don’t want a specific line of code to run in your program. That’s where conditional logic comes in. By using an if
statement, you can evaluate an expression, and depending on the outcome of that expression, run a block of code.
The if
statement evaluates whether a Boolean expression is false or true. If that expression is true, the code will be executed; if the expression is false, nothing will happen. Sometimes, if
statements are called control flow statements.
Open up a new Java file and paste in the following code:
class MenuPrices { public static void main(String[] args) { String order = "Ham Sandwich"; if (order.equals("Ham Sandwich")) { System.out.println("The price of a " + order + " is $1.95."); } System.out.println("Done"); } }
In this code, we have defined a variable called order which holds the string value “Ham Sandwich”. We then use an if statement to check whether or order is equal to the term “Ham Sandwich”. If it is, the price of a ham sandwich is printed.
Run this Java program in your programming environment. You’ll see the following response:
The price of a Ham Sandwich is $1.95. Done
The order we have specified is “Ham Sandwich”, so that line of code is run. Let’s change our order to a chicken sandwich:
String order = "Chicken Mayo Sandwich"; if (order.equals("Ham Sandwich")) { System.out.println("The price of a " + order + " is $1.95. "); } System.out.println("Done");
Our code returns: Done. The contents of our if statement are not executed because our condition –≠ order being equal to “Ham Sandwich” – was not met.
If Else Statements
The if
statement is useful, but what if you want to do something else if your condition doesn’t evaluate to true? That’s where the aptly-named Java if else statement comes in.
Let’s say that we want our program to print: This item is not on the menu.
If it cannot find the order we’ve entered. We could do so by adding an else block to our code:
String order = "Chicken Mayo Sandwich"; if (order.equals("Ham Sandwich")) { System.out.println("The price of a " + order + " is $1.95. "); } System.out.println("Done");
If our expression evaluates to false, the contents of our else
statement will execute. When you save and run the program, you’ll see the following:
This item is not on the menu. Done
When we change our order back to “ Ham Sandwich”, the output we received in the last example will be returned.
Else if Statement
if..else
statements can handle two potential outcomes: whether a condition is met, or whether a condition is not met. We can use a statement called else if
to evaluate more conditions.
Say that we want to check for three different sandwiches in our program. Their prices are:
- Ham Sandwich: $1.95
- Chicken Mayo Sandwich: $2.20
- Smoked Salmon Sandwich: $3.00
To make our program work, we’re going to add in a few else if
statements:
String order = "Chicken Mayo Sandwich"; if (order.equals("Ham Sandwich")) { System.out.println("The price of a " + order + " is $1.95. "); } else { System.out.println("This item is not on the menu."); } System.out.println("Done");
Our program can now return four possible outputs. If we run our above program, we’ll receive the following response:
The price of a Chicken Mayo Sandwich is $2.20. Done
Let’s change the value of our order variable to “Smoked Salmon Sandwich” and run our program again:
The price of a Smoked Salmon Sandwich is $3.00. Done
You can see that our program is now capable of evaluating multiple different conditions. We could add in as many as we wanted. If we were operating a cafe, we may want to evaluate against ten different conditions, one for each sandwich we sell.
Conclusion (and Challenge)
If statements allow you to control the flow of your program more effectively. The code inside an if
statement only runs if a specified condition is true. You can use else...if
statements to check for whether one of many conditions are met, and an else
statement to do something if no condition is met.
Ready for a challenge? Write a program that calculates a student’s grade on a test based on their numerical grades. Here are the grade boundaries for reference:
- 85+ is an A
- 75+ is a B
- 65+ is a C
- 50+ is a D
- Below 50 is an F
Now you’re ready to start writing your own if statements in Java like a pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.