The if-else statement in Java is used to make decisions based on conditions. It allows the program to execute different blocks of code depending on whether a condition is true or false.
true or falseelse ifJava evaluates the condition inside the if statement. If it is true, the if block runs; otherwise, the else block executes.
// Basic if-else example in Java
int age = 20;
if (age >= 18) {
System.out.println("Eligible to vote");
} else {
System.out.println("Not eligible to vote");
}
If age is 20, the condition age >= 18 is true, so the output will be:
Eligible to vote
Scenario: Simulate the Java code above. Change the variable age and run the logic.
{ } even for single statementsif-else if.%.score variable (e.g., if score > 90 print "A").year is a leap year.