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

The if variant

The else part of an if-else statement is optional. If it is missing, we have an if statement, which allows us to execute a certain part of code if a condition is satisfied (and do nothing otherwise).


if statement

Syntax:

if (condition)
  then-statement

Semantics:

First, the condition is evaluated. If the result of the evaluation is the value true, the then-statement is executed, and the execution continues with the statement immediately following the if statement. Otherwise, the execution continues directly with the statement following the if statement.

Example:

boolean found;
...
if (!found)
  System.out.println("element not found");

When this if statement is executed, the string "element not found" is printed on the output channel, provided the value of the boolean variable found is false.



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