Recall & Review
beginner
What is a nested if statement in Java?
A nested if statement is an if statement placed inside another if statement. It allows checking multiple conditions step-by-step.
Click to reveal answer
beginner
How does Java execute nested if statements?
Java first checks the outer if condition. If it is true, then it checks the inner if condition(s). If the outer condition is false, inner conditions are skipped.
Click to reveal answer
beginner
Write a simple example of a nested if statement in Java.
Example:
if (age >= 18) {
if (hasID) {
System.out.println("Allowed to enter.");
}
}Click to reveal answer
intermediate
Why use nested if statements instead of multiple separate if statements?
Nested if statements help check conditions in order and avoid unnecessary checks if an earlier condition fails. This makes code clearer and more efficient.
Click to reveal answer
intermediate
Can nested if statements have else blocks? How?
Yes. Both outer and inner if statements can have else blocks to handle cases when conditions are false. This allows detailed control of program flow.
Click to reveal answer
What happens if the outer if condition is false in a nested if statement?
✗ Incorrect
If the outer if condition is false, the inner if statements inside it are not checked or executed.
Which keyword can be used with nested if statements to handle false conditions?
✗ Incorrect
The else keyword is used to provide alternative code when an if condition is false, including inside nested if statements.
What is the correct way to write a nested if statement in Java?
✗ Incorrect
The correct syntax uses braces to nest the second if inside the first: if (condition1) { if (condition2) { // code } }
Why might nested if statements be harder to read?
✗ Incorrect
Nested if statements increase indentation levels and can make code harder to follow if overused.
Can nested if statements be used inside loops?
✗ Incorrect
Nested if statements can be used inside any loop to check conditions repeatedly.
Explain how nested if statements control program flow in Java.
Think about checking one condition inside another.
You got /4 concepts.
Write a simple Java code example using nested if statements to check if a number is positive and even.
Use one if inside another to check two conditions.
You got /3 concepts.