The if-else statement allows us to select between two alternatives.
Syntax:
if (condition)
then-statement
else
else-statement
Semantics:
First, the condition is evaluated. If the result of the evaluation is the value true, the then-statement is executed, otherwise the else-statement is executed. In both cases, the execution continues with the statement immediately following the if-else statement.
Example:
int a, b; ... if (a > b) System.out.println("bigger value = " + a); else System.out.println("bigger value = " + b);
When this if-else statement is executed, the string "bigger value = " followed by the bigger one among a and b is printed on the output channel (the monitor).