Java Notes
If-else statements let a Java program choose what to do by checking whether a condition is true or false.
An if-else statement helps a program make a decision. Java checks a question whose answer is either true or false, then runs the code that matches the answer.
For example: “Is this score at least 40?” If yes, print Pass. If no, print Fail. This is the same kind of decision people make every day; Java just needs the question written in code.
Simple if: do something only when a condition is true
Use if when there is one action that should happen only in a particular situation.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Java checks score >= 40. Here it is true, so it prints Pass. Then Java continues after the if block and prints Result checked.
Change score to 32 and run it again. The message Pass disappears because the condition is false, but Result checked still prints because it is outside the curly brackets.
The { } brackets show exactly which lines belong to the if. Use them even when the block currently has one line; this prevents mistakes when you add another line later.
If-else: choose between two outcomes
Use if-else when one of two actions must always happen. If the condition is true, Java runs the if block. If it is false, Java skips that block and runs the else block.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Only one message prints. Java cannot run both branches from one if-else decision. Change score to 40 and confirm that 40 counts as Pass because >= means “greater than or equal to.”
Else-if: choose from several outcomes
Sometimes a program needs more than two choices, such as assigning a grade. Java checks if, then each else if from top to bottom. The first condition that is true wins; Java skips the rest.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Read this order carefully:
- The first condition rejects impossible scores below 0 or above 100.
- Java checks the highest grade boundary first: 80 or above gets A.
- A score of 74 is not at least 80, but it is at least 60, so Java chooses B.
- After Java finds B, it does not check C or F.
Always test the highest range first. If score >= 40 came before score >= 80, a score of 90 would stop at C and never reach A.
Nested if: make the second decision only after the first
A nested if is an if inside another if. Use it when the second question matters only after the first answer is true.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
First Java asks, “Is the person logged in?” Only a logged-in person needs the second question about the admin role. In this example, the person is logged in but is not an admin, so Java opens the member dashboard.
When both questions simply need to be true, combine them with &&: if (loggedIn && hasAdminRole). Nesting is useful when each outcome has different actions.
Common beginner mistakes
Using = instead of ==
= stores a value. == compares two values. In an if condition you usually want a comparison.
int score = 40;
// if (score = 40) { } // Wrong: this tries to assign 40.
if (score == 40) {
System.out.println("Exactly 40");
}Comparing text with ==
For Strings, use equals() to compare the text.
String role = "admin";
if ("admin".equals(role)) {
System.out.println("Admin access");
}"admin".equals(role) asks whether the letters are the same. It is also safe if role has no value (null).
Try it yourself
Create AgeGroup that stores an age and prints Child, Teen, or Adult.
Rules
- A negative age should print
Invalid age. - Less than 13 means
Child. - 13 through 17 means
Teen. - 18 or more means
Adult.
One working answer
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Run the program with 12, 13, 17, and 18. These boundary values prove whether the conditions are in the correct order.
Continue the lesson path