Sub topic 2
Run One Block with if and else
A program does not have to do the same thing every time it runs. Suppose a quiz program needs to show one message when a learner passes and a different message when they do not. In Java, you can write that decision with if and else.
A condition is an expression that has the value true or false. A block is a group of statements inside braces. A statement is an instruction for Java to carry out. An if statement checks a condition before running its block. An else block gives Java something to do when the condition is false.
Use if for an Optional Action
Start with a quiz program that only shows a special message when a learner passes. A score of 70 or more counts as passing. Save this program as Main.java and run it.
public class Main {
public static void main(String[] args) {
int score = 85;
if (score >= 70) {
System.out.println("You passed.");
}
System.out.println("Quiz complete.");
}
}Read the if line as “if the score is at least 70.” The parentheses hold the condition, and the braces mark the start and end of the block. The spaces before the print statement are called indentation. They make the block easier to see, but the braces tell Java where it ends.
With a score of 85, the condition is true. Java prints “You passed.” Then it moves past the block and prints “Quiz complete.”
Change the score to 69 and run the program again. This time the condition is false. Java skips the whole block and prints only “Quiz complete.” That last message is outside the block, so it runs either way. An if statement does not need an else when there is nothing special to do for a false condition.
Add else for a Second Action
Now suppose every learner should get feedback about their score. Keep the passing message and add a message for a score below 70. Replace the contents of Main.java with this version.
public class Main {
public static void main(String[] args) {
int score = 69;
if (score >= 70) {
System.out.println("You passed.");
} else {
System.out.println("Keep practicing.");
}
System.out.println("Quiz complete.");
}
}The two blocks are called branches. A branch is one path the program can take at a decision. When Java reaches this if statement, it checks the condition once. If the result is true, it runs the if block and skips the else block. If the result is false, it skips the if block and runs the else block. There is no condition after else because the first check already tells Java when to use it.
With a score of 69, the program prints “Keep practicing.” followed by “Quiz complete.” It never prints both score messages on the same run.
Predict the Output Before You Run
Try the scores in the table one at a time. Change only the number assigned to score. Before each run, say whether the condition is true or false. Then predict both lines of output and check what the program actually prints.
| Score | Is the score at least 70? | First line | Second line |
|---|---|---|---|
| 69 | False | Keep practicing. | Quiz complete. |
| 70 | True | You passed. | Quiz complete. |
| 85 | True | You passed. | Quiz complete. |
The most useful pair to test is 69 and 70. The score of 70 is right where the result changes. If you use greater than instead of greater than or equal to, 85 still passes and 69 still does not. Only the test at 70 shows the mistake.
Check What Belongs Inside the Braces
If a message appears when you did not expect it, look at the braces first. If you move “Quiz complete.” inside the else block, it will print only for scores below 70. If you move it inside the if block, it will print only for passing scores. Leave it after the last closing brace of the decision so both paths reach it.
Use braces even when a branch has only one statement. That way, if you later add a second message for passing learners, you can put it inside the same braces and see that both messages belong to the same branch.
Practice
Exercise
In the program with if and else, what prints when score is 70? Why?
Show answer
It prints “You passed.” and then “Quiz complete.” The condition includes 70, so Java runs the if block, skips the else block, and continues after the decision.
Exercise
Change the program so that a non-passing score gets no score message, but every run still prints “Quiz complete.” What should you remove?
Show answer
Remove else and its entire braced block. Keep the if block and leave the “Quiz complete.” print statement outside it. Test with 69 and 70 to check both paths.