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.
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.