Java Tutorial/Statement Control/If Statement

Материал из Java эксперт
Перейти к: навигация, поиск

Expression indentation for if statement

If the expression is too long, you can use two units of indentation for subsequent lines.



public class MainClass {
  public static void main(String[] args) {
    int numberOfLoginAttempts = 10;
    int numberOfMinimumLoginAttempts = 12;
    int numberOfMaximumLoginAttempts = 13;
    int y = 3;
    if (numberOfLoginAttempts < numberOfMaximumLoginAttempts
        || numberOfMinimumLoginAttempts > y) {
      y++;
    }
  }
}





Multiple selections

If there are multiple selections, you can also use if with a series of else statements.



if (booleanExpression1) {
    // statements
} else if (booleanExpression2) {
    // statements
}
...
else {
    // statements
}





Nested if Statements

public class MainClass {
  public static void main(String[] arg) {
    int a = 2;
    if (a == 0) {
      System.out.println("in the block");
      if (a == 2) {
        System.out.println("a is 0");
      } else {
        System.out.println("a is not 2");
      }
    } else {
      System.out.println("a is not 0");
    }
  }
}



a is not 0


The else Clause

public class MainClass {
  public static void main(String[] arg) {
    int a = 0;
    if (a == 0) {
      System.out.println("in the block");
      System.out.println("in the block");
    } else {
      System.out.println("a is not 0");
    }
  }
}



in the block
in the block


The if Statement in action

public class MainClass {
  public static void main(String[] arg) {
    int a = 0;
    if (a == 0) {
      System.out.println("a is 0");
    }
  }
}



a is 0


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





Using braces makes your "if" statement clearer

If there is only one statement in an if or else block, the braces are optional.



public class MainClass {
  public static void main(String[] args) {
    int a = 3;
    if (a > 3)
      a++;
    else
      a = 3;
  }
}





Using && in if statement

public class MainClass {
  public static void main(String[] arg) {
    int value = 8;
    int count = 10;
    int limit = 11;
    if (++value % 2 == 0 && ++count < limit) {
      System.out.println("here");
      System.out.println(value);
      System.out.println(count);
    }
    System.out.println("there");
    System.out.println(value);
    System.out.println(count);
  }
}



there
9
10


Using || (or operator) in if statement

public class MainClass {
  public static void main(String[] arg) {
    int value = 8;
    int count = 10;
    int limit = 11;
    if (++value % 2 != 0 || ++count < limit) {
      System.out.println("here");
      System.out.println(value);
      System.out.println(count);
    }
    System.out.println("there");
    System.out.println(value);
    System.out.println(count);
  }
}



here
9
10
there
9
10