The usage of the if statement of conditional judgment in Java programming

  • 2020-04-01 04:21:26
  • OfStack

If statements use Boolean expressions or Boolean values as branch conditions for branch control. If statements have the following three forms:

The first form:


  if  (  logic expression  ) 

  {

  statements ... 

  }

The second form:


  if  ( logic expression ) 

  {

  statements ... 

  }

  else

  {

  statements ... 

  }

The third form:


  if  ( logic expression ) 

  {

  statements ... 

  }

  else if ( logic expression ) 

  {

  statements ... 

  }

 ... //You can have zero or more else if statements

  else//The final else statement can also be omitted

  {

  statement

  }

In the three forms of the if language above, the only thing that can be enclosed in parentheses after the if is a logical expression that can only return true or false.

The conditional statements above, if (logic expression), else if (logic expression) and parentheses after the else spend more lines of code is called a code block, a block of code is usually regarded as a whole to perform (unless the return process of running, break and continue keywords, or met with exceptions), so the code block is also called the conditional execution body. For example:

Program listing: codes / 04/4-2 / TestIf. Java


  public class TestIf

  {

  public static void main ( String[] args ) 

  {

  int age = 30;

  if  ( age > 20 ) 

  //Only when age> At 20, the statement block enclosed by the following curly braces is executed

  //Statements enclosed in curly braces are executed as a whole, either together or not at all

  {

  System.out.println ( " Older than 20 At the age of " ); 

  System.out.println ( "20 People over the age of 18 should learn to take responsibility... " ); 

  }

  }

  }

Therefore, if the statement blocks after if (logic expression), else if (logic expression), and else have only one line of statements, you can omit the curly braces because the single line statement is itself a whole, and you don't need curly braces to define them as a whole. The following code can execute normally (the program list is the same as above) :


  //Define the variable a and assign it a value

  int a = 5;

  if  ( a > 4 ) 

  //If a> 4, execute the following execution body, with only one line of code as the code block

  System.out.println ( "a Is greater than 4" ); 

  else

  //Otherwise, execute the following execution body with only one line of code as the code block

  System.out.println ( "a No greater than 4" ); 

In general, we recommend not to omit the curly braces for executing blocks after if, else, or else if, even if the conditional execution body is one line long, because keeping the curly braces is more readable, and keeping the curly braces reduces the chance of errors, such as:


  //Define and assign a value to the variable b

  int b = 5;

  if  ( b > 4 ) 

  //If b> 4, execute the following execution body, with only one line of code as the code block

  System.out.println ( "b Is greater than 4" ); 

  else

  //Otherwise, execute the following body

  b--;

  //For the following code, it is no longer part of the conditional execution, so it will always be executed

  System.out.println ( "b No greater than 4" ); 

The bold lines in the above code: system.out.println ("b not greater than 4"); , will always execute because this line of code is not part of the conditional execution after else, the conditional execution after else is b--; This line of code.

If, else, else if the conditional execution body is either a statement block with curly braces, the statement block as a whole as the conditional execution body; Either a one-line statement ending with a semicolon, or even an empty statement (an empty statement is a semicolon).

If the conditional execution body is followed by more than one statement, it will cause a compilation error if the curly braces of the conditional execution body are omitted. See the following code (ibid.) :


  //Define and assign a value to the variable c

  int c = 5;

  if  ( c > 4 ) 

  //If c> 4, execute the following body, there will be only c--; A line of code is the conditional execution body

  c--;

  //The following is a line of plain code that is not part of the conditional execution body

  System.out.println ( "c Is greater than 4" ); 

  //There will be no if statement for the else here, so compilation fails

  else

  //Otherwise, execute the following execution body with only one line of code as the code block

  System.out.println ( "c No greater than 4" ); 

In the above code, because the conditional execution body after if omitted the curly braces, the system only puts c--; One line of code as the conditional execution body, when c-; After the statement ends, the if statement ends. System.out.println ("c greater than 4"); The code is already a normal line of code that is no longer part of the conditional execution, causing the else statement to have no if statement, which causes a compilation error.


Related articles: