next up previous
Next: Condition in an if-else Up: Unit 05 Previous: Conditional statements

The if-else statement

The if-else statement allows us to select between two alternatives.


if-else statement

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



next up previous
Next: Condition in an if-else Up: Unit 05 Previous: Conditional statements