Back to Boolean Expressions and if Statements

Lesson PreviewLesson 3 of 8 in this unit

3.3 if-else Statements

Part of Boolean Expressions and if Statements in AP® Computer Science A. Read the opening section, then create a free account for the full interactive lesson, practice, and progress tracking.

An if-else statement extends a basic if statement by providing an alternative path when the condition is false. The if part checks a boolean condition, and if it’s true, runs its block of code; otherwise, the else part runs its block of code.

java
int score = 50;
if (score >= 60) {
    System.out.println("Pass");
} else {
    // This block executes when score is less than 60
    System.out.println("Fail");
}

Output

Fail

* Notice that the first conditional statement evaluated to false, so the program resorted to executing the code in the else block.

Continue the full lesson in Stellar

Create a free account to unlock the remaining sections, checkpoints, course practice, and progress tracking for AP® Computer Science A.