Introduction
Today, we’re diving into one of the most fundamental and simple concepts in Java programming: the if statement.
If you’ve ever said things like:
- “If it’s sunny, I’ll go to the beach.”
- “If I have time, I’ll go to the movies.”
…then you already understand how an if statement works! It helps programs make decisions based on certain conditions. Let’s break this down step by step.
1. Why do we need if?
Sometimes, a program needs to do something, but only under specific circumstances. For example:
- Show a discount if the customer has enough bonus points.
- Say a student passed an exam if their score is greater than 50.
- Turn on the light if a switch is pressed.
For all of these situations, we use the if statement.
2. What does if look like in Java?
Here’s the basic structure of an if statement:
if (condition) {
// actions to perform if the condition is true
}
Key points:
- “condition” is a test to check if something is true or not. For example, “score > 50” or “temperature < 0”.
- If the condition is true, the code inside the curly braces
{ }
runs. - If the condition is false, the code is skipped.
3. Example 1: Checking a student’s grade
Suppose we want to check if a student passed an exam. If their score is 50 or higher, we print: “You passed the exam!”.
public class CheckGrade {
public static void main(String[] args) {
int grade = 65; // student’s score
if (grade >= 50) {
System.out.println("You passed the exam!");
}
}
}
Explanation:
grade >= 50
is the condition. We’re checking: Is the score greater than or equal to 50?- If the condition is true (like when the score is 65), the program prints: “You passed the exam!”.
- If the score were, say, 45, nothing would be printed because the condition is false.
4. What if the condition is false?
Sometimes, you want the program to do something else if the condition isn’t true. That’s where else comes in.
Here’s how it works:
if (condition) {
// runs if the condition is true
} else {
// runs if the condition is false
}
Example: Checking the weather
If the temperature is above 15 degrees, we’ll suggest going outside. Otherwise, we’ll recommend staying indoors.
public class WeatherCheck {
public static void main(String[] args) {
int temperature = 10; // current temperature
if (temperature > 15) {
System.out.println("It's warm outside, you can go for a walk!");
} else {
System.out.println("It's cold outside, better stay indoors.");
}
}
}
Results:
- If the temperature is 20: “It’s warm outside, you can go for a walk!”
- If the temperature is 10: “It’s cold outside, better stay indoors.”
5. Checking multiple conditions: else if
What if there are multiple conditions? For example, if the temperature is above 25 degrees, you could suggest going for a swim. If it’s between 15 and 25 degrees, a walk would be nice.
To handle this, we use else if:
public class WeatherCheck {
public static void main(String[] args) {
int temperature = 27;
if (temperature > 25) {
System.out.println("It's hot! You can go for a swim!");
} else if (temperature > 15) {
System.out.println("It’s warm outside, you can go for a walk!");
} else {
System.out.println("It’s cold outside, better stay indoors.");
}
}
}
How does this work?
- The program checks each condition from top to bottom.
- If the first condition (
temperature > 25
) is true, the program runs the code and skips the rest. - If the first condition is false, it checks the next one (
temperature > 15
). - If all conditions are false, it runs the else block.
6. Skipping curly braces (optional)
If there’s only one line of code inside an if or else, you can skip the curly braces { }
. However, you need to be careful to keep your code readable.
Here’s a short example:
public class SimpleCheck {
public static void main(String[] args) {
int temperature = 20;
if (temperature > 15)
System.out.println("It's warm outside!");
else
System.out.println("It's cool outside!");
}
}
Results:
- If the temperature is 20: “It’s warm outside!”
- If the temperature is 10: “It’s cool outside!”
7. Conclusion
Today we learned:
- How to use the if statement to check conditions.
- How to use else to specify what happens if the condition isn’t true.
- How to check multiple conditions with else if.
Real-life example: If you want to automate decisions – like planning your activities based on the weather or determining if someone passed an exam – the if statement is your best friend!
Challenge Yourself!
Try writing these programs:
- Check if someone is an adult: Use
int age
and print if a person is “adult” (age >= 18) or “not adult.” - Check if a number is even or odd: Use
int number
and print the result. - Check if a customer gets a discount: If the purchase amount is greater than 1000, print “You get a discount!”.
Head to an online Java compiler and start coding. The more you practice, the easier it gets!