If Else

suggest change
int i = 2;
if (i < 2) {
  System.out.println("i is less than 2");
} else {
  System.out.println("i is greater than or equal to 2");
}

With i equal to 2, the condition i < 2 is false, so the else block runs. That block is every remaining value, not only values greater than 2.

An if statement runs the following statement when the parenthesized condition is true. Braces group several statements. A single statement does not need braces, but the brace form above makes the extent obvious.

The else block is optional. It runs when the if condition is false, and it is skipped when that condition is true.

See also: Ternary If

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents