The if statement syntax : If Statement « Statement Control « Java Tutorial






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
    }
  }

}








4.2.If Statement
4.2.1.The if statement syntax
4.2.2.Expression indentation for if statement
4.2.3.Using braces makes your 'if' statement clearer
4.2.4.Multiple selections
4.2.5.The if Statement in action
4.2.6.The else Clause
4.2.7.Nested if Statements
4.2.8.Using && in if statement
4.2.9.Using || (or operator) in if statement