If else statement

The if statement is a conditional branch statement. Here is the general form of the if-else statement:

 
if (condition) 
   statement1; 
else 
   statement2;

The else clause is optional.

Each statement may be a single statement or a compound statement enclosed in curly braces (a block).
The condition is any expression that returns a boolean value.

Only one statement can appear directly after the if or the else. To include more statements, you'll need to create a block, as in this fragment:

public class Main {
  public static void main(String[] argv) {
    int i = 1;

    if (i > 0) {
      System.out.println("Here");
      i -= 1;

    } else
      System.out.println("There");
  }
}
  

The output:


Here

It is convenient to include the curly braces when using the if statement, even when there is only one statement in each clause.

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.