4. 2. 1. The if statement syntax |
|
The if statement is a conditional branch statement.
The syntax of the if statement is either one of these two: |
if (booleanExpression) {
statement (s)
}
|
|
or |
if (booleanExpression) {
statement (s)
} else {
statement (s)
}
|
|
For example, in the following if statement, the if block will be executed if x is greater than 4. |
public class MainClass {
public static void main(String[] args) {
int x = 9;
if (x > 4) {
// statements
}
}
}
|
|
In the following example, the if block will be executed if a is greater than 3.
Otherwise, the else block will be executed. |
public class MainClass {
public static void main(String[] args) {
int a = 3;
if (a > 3) {
// statements
} else {
// statements
}
}
}
|
|