Syntax:
if (condition) then-statement else else-statement • condition is an expression of type boolean, i.e., a conditional expression that is evaluated to true or false • then-statement is a single statement (also called the then-branch of the if-else statement) • else-statement is a single statement (also called the else-branch of the if-else statement) 1
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).
Condition in an if-else statement The condition in an if-else statement can be an arbitrary expression of type boolean, for example: • a variable of type boolean; Example:
boolean finished; ... if (finished) ... • one of the comparison operators (==, !=, >, <, >=, or <=) applied to variables (or expressions) of a primitive type; Example:
int a, b, c; ... if (a > b + c) ... • a call to a predicate (i.e., a method that returns a value of type boolean);
example:
String answer; ... if (answer.equalsIgnoreCase("YES")) ... • a complex boolean expression, obtained by applying the boolean operators !, &&, and || to simpler expressions; Example:
int a, b, c, d; String answer; ... if ((a > (b+c)) || (a == d) && !answer.
equalsIgnoreCase("YES")) ...
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
if (condition) then-statement • condition is an expression of type boolean • then-statement is a single statement (also called the then-branch of the if 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"); Whenthis if statementisexecuted, thestring "element not found" isprintedontheoutputchannel, provided the value of the boolean variable found is false.
Block of statements The syntax of if-else allows us to have only a single statement in the then-branch (or the else-branch). If we want to execute more than one statement in the then-branch (or the else-branch), we have to use a block construct. A block of statements groups several statements in a single composite statement. Block of statements Syntax:
{
statement ... statement
} • statement is an arbitrary Java statement Semantics: The statements in the block are executed in sequence. The variables declared inside the block are not visible outside the block itself. Example:
int a, b, bigger; ... if (a > b) { bigger = a; System.out.println("smaller = " + b); }