AP® Computer Science A glossary
Nested if statement
Also called: nested conditional
Course: AP® Computer Science A Shows up in: Unit 2: Selection and Iteration
An if, if-else or if-else-if statement inside the body of another one. The inner Boolean expression is evaluated only if the outer one is true.
Appears in the course and exam description (CED): 2.4.A.12.4.A.2
Without braces, an else pairs with the nearest earlier if that has no else (the dangling else), whatever the indentation.
int age = 15;
boolean isMember = true;
if (age < 18) {
if (isMember) {
System.out.println("5 dollars");
}
}
Output:
5 dollars Learn it properly, with a checkpoint 2.4 Nested if Statements