if Statement (Java)
An if statement runs a block of code only when a condition is true. With else and else if, a Java program can choose between several different paths.
Java
The condition must be a boolean and goes in parentheses, with the body in curly braces. Use else if to test another condition only when the earlier ones were false, and else as a catch-all.
For checking one value against many fixed options, a switch statement can be a cleaner alternative.
Examples
Java
int score = 75;
if (score >= 90) {
System.out.println("A");
} else if (score >= 70) {
System.out.println("B");
} else {
System.out.println("C");
}Output:
B