if else conditional statement

Java conditional statements

  • if
  • if…else
  • if…else if…else
  • Nested if ... else

if

Syntax:

if (expression) {
    // If the expression result bit true then execute the code here
}

Examples

public class Test {

   public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out.print("这是 if 语句"); } } } 

 

if…else

Syntax:

if (boolean expression) {
    //true
}else{
    //false
}

Examples

public class Test {

   public static void main ( String args []) { int X = 30 ; if ( X < 20 is ) { the System . OUT . Print ( "It is an if statement" ); } else { the System . OUT . Print ( "It is else statement " ); } } }


if…else if…else

 


Syntax:

if (Boolean expression 1) {
   // execute code if the value of true Boolean expression 1
} Else if (Boolean expression 2) {
   // execute code if the value of true Boolean expression 2
} Else if (Boolean expression 3) {
   // execute code if the value of true Boolean expression 3
}else {
   // code execution if not more than the Boolean expression is true
}

note:

  1. At most one else statements
  2. Else if there can be a number of statements, but must be re-else before
  3. Wherein upon detecting a else if statement is true, and the other else if else will be skipped.

Examples

public class Test {
   public static void main(String args[]){ int x = 30; if( x == 10 ){ System.out.print("Value of X is 10"); }else if( x == 20 ){ System.out.print("Value of X is 20"); }else if( x == 30 ){ System.out.print("Value of X is 30"); }else{ System.out.print("这是 else 语句"); } } } 

 

Nested if ... else

Syntax:

if (Boolean expression 1) {
   //// value of true if the Boolean expression to execute code 1
   if (Boolean expression 2) {
      //// execution code is true if the Boolean expression 2
   }
}

Examples

public class Test {

   public static void main(String args[]){ int x = 30; int y = 10; if( x == 30 ){ if( y == 10 ){ System.out.print("X = 30 and Y = 10"); } } } } 

Guess you like

Origin www.cnblogs.com/bomily0212/p/12082966.html