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.



   <source lang="java">

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

}</source>





Multiple selections

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



   <source lang="java">

if (booleanExpression1) {

   // statements

} else if (booleanExpression2) {

   // statements

} ... else {

   // statements

}</source>





Nested if Statements

   <source lang="java">

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

}</source>



a is not 0


The else Clause

   <source lang="java">

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

}</source>



in the block
in the block


The if Statement in action

   <source lang="java">

public class MainClass {

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

}</source>



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:



   <source lang="java">

if (booleanExpression) {

   statement (s)

}</source>





Using braces makes your "if" statement clearer

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



   <source lang="java">

public class MainClass {

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

}</source>





Using && in if statement

   <source lang="java">

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

}</source>



there
9
10


Using || (or operator) in if statement

   <source lang="java">

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

}</source>



here
9
10
there
9
10