Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
An if statement makes Java test a boolean expression before it runs the statements inside its body. With score equal to 73, the comparison score >= 50 is true, so Java prints Pass. The condition is checked first, and only then does control enter the body.
int score = 73;
if (score >= 50) {
System.out.println("Pass");
}If the condition is false, Java skips the entire body and continues with the statement after the if statement. The skipped body does not print anything. An if statement therefore provides a possible action, not a guaranteed action.
Given score = 73 and the condition score >= 80, what happens to the if body's print statement?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Adding else supplies the action for the false result of the same condition. For score equal to 73, score >= 50 is true, so Java runs the first body and skips the else body. If that comparison were false, Java would run the else body instead.
int score = 73;
if (score >= 50) {
System.out.println("Pass");
} else {
System.out.println("Retry");
}Exactly one of these two bodies runs. The else does not test a second condition, because it represents the false result of the if condition. This makes if-else useful when every input must receive one of two outcomes.
An else-if chain checks conditions from top to bottom. For score equal to 73, the 80 threshold is false, so Java checks 70. That condition is true, so it prints Merit and leaves the whole chain. Java does not continue to test the 50 threshold or run the else branch.
int score = 73;
if (score >= 80) {
System.out.println("Distinction");
} else if (score >= 70) {
System.out.println("Merit");
} else if (score >= 50) {
System.out.println("Pass");
} else {
System.out.println("Retry");
}Separate if statements do not stop one another. With score equal to 73, both score >= 70 and score >= 50 are true, so two separate if bodies would print Merit and Pass. Connecting them with else-if makes the choices exclusive: once one condition succeeds, the remaining branches are skipped.
Replace these separate if statements with an ordered else-if chain so score = 73 prints only "Merit".
if (score >= 80) {
System.out.println("Distinction");
}
if (score >= 70) {
System.out.println("Merit");
}
if (score >= 50) {
System.out.println("Pass");
}Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
An else-if chain must test narrower or higher thresholds before broader or lower thresholds. If score >= 50 comes first, score equal to 73 passes immediately and Java prints Pass. The later score >= 70 test is never reached, even though it is also true.
int score = 73;
if (score >= 50) {
System.out.println("Pass");
} else if (score >= 70) {
System.out.println("Merit");
} else {
System.out.println("Retry");
}Braces define the statements that belong to a branch. Without braces, Java controls only the next statement after if or else. A second statement then runs regardless of the condition, which can make the output look as if it belongs to the selected branch when it does not.
if (score >= 70)
System.out.println("Merit");
System.out.println("Pass");Use braces when a branch should contain more than one statement. They make the boundary visible and keep both statements on the same selected path. The same rule applies to every branch in the classifier, so the threshold order and the braces must both match the intended behavior.
if (score >= 70) {
System.out.println("Merit");
System.out.println("Pass");
}