Conditional statement (if-else)

if-else syntax

  • if-else syntax, only a block of statements to be executed
  • if and else are Java keywords
  • if grammar
  • The if-else seen as an expression, the order of execution of the program as a whole or

if (boolean value) {

  if block

} else {

  else block

}


 

For example: buy buns, freshly baked buns if, then buy two more; otherwise it bought three.

public  class IfElseBaozi {
     public  static  void main (String [] args) {
         int Baozi =. 3 ; 

        Boolean baoziGangChuLong = to true ; 

        IF (baoziGangChuLong) { 
            Baozi = Baozi + 2 ; 
            System.out.println ( "bun just came out, buy" + baozi + "a meat bun." ); 
        } the else { 
            System.out.println ( "bought" + baozi + "a meat bun." ); 
        } 
    } 
}

 

nested if-else

  • It is an if-else statement, the statement may be part of another, or may be a part of if-else, i.e. nested.

 

Seeking a, b, c of the maximum number of three.

public  class Example2 {
     public  static  void main (String [] args) {
         int A = 100 ;
         int B = 100 ;
         int C = 23 is ;
         // points these types of situations: abc other large; Maximum A; B Maximum; C Maximum ; ab & other large and a maximum; AC and other large maximum; BC and other large maximum. 
        IF (A == C == B && B) { // A = B = C 
            System.out.println ( "A, B, C, etc. as large as" + A); 
        } the else {
             IF (A> B) {
                 IF (A> C) { 
                    System.out.println ( "A maximum of" + A); 
                }else{ // a<=c
                    if (a==c){
                        System.out.println("a,c最大,为"+a);
                    }else{
                        System.out.println("c最大,为"+c);
                    }
                }
            }else{ // a <= b
                if (b>c){
                    if(a==b){
                        System.out.println("a,b最大,为"+a);
                    }else{
                        System.out.println ("b maximum, as" + B); 
                    } 
                } the else { // B <C = 
                    IF (B == C) { 
                        System.out.println ( "b, C maximum, as" + B); 
                    } the else { 
                        the System .out.println ( "C maximum, for the" + C); 
                    } 
                } 
            } 
        } 
    } 
}

if-else simplification

  • Or else if blocks of statements if only one statement , may be omitted braces.
  • When there is an if statement else statement may be abbreviated else if (condition) in the form of

if (boolean value)

  if block

else

  else block

 

if (boolean value) {

  if block

} else if (boolean值) {

  if block

} else {

  else block

}

public  class OneStatementIfElse {
     public  static  void main (String [] args) {
         int A = 10 ; 

        System.out.println ( "omit the braces" );
         IF (A> 0 ) 
            System.out.println ( "A greater than 0" );
         the else 
            System.out.println ( "a less than or equal 0" ); 

        System.out.println ( "a comparison of written full" );
         IF (a> 0 ) { 
            System.out.println ( "a greater than 0 " ); 
        } the else {
             IF(A == 0 ) { 
                System.out.println ( "A equals 0" ); 
            } the else { 
                System.out.println ( "A less than 0" ); 
            } 
        } 

        System.out.println ( "A Comparison of all omitted braces method " );
         IF (a> 0 ) 
            System.out.println ( " a greater than 0 " );
         the else  IF (a == 0 ) 
            System.out.println ( " a equals 0 " );
         the else 
            the System. Out.println ( "A less than 0" ); 

        System.out.println ("Comparison of the size of the code block with a plurality of the optimum writing statements" );
         IF (A> 0 ) { 
            System.out.println ( "A greater than 0" ); 
            System.out.println ( "Buy" + a + " a meat bun. " ); 
        } the else  IF (a == 0 ) { 
            System.out.println ( " a equals 0 " ); 
            System.out.println ( " do not buy the meat bun. " ); 
        } the else { 
            the System .out.println ( "a less than 0" ); 
            System.out.println ( "eat more meat buns." ); 
        } 

    } 
}

Simplified procedure seeking the maximum number of

public class IfElseNestSimple {
    public static void main(String[] args) {
        int a = 10;
        int b = 99;
        int c = 99;

        System.out.println("a=" + a + ". b=" + b + ". c=" + c + ".");
        if (a == b && b == c) {
            System.out.println("a,b,c等大。");
        } else if (a > b && a > c) {
            System.out.println("a最大,为" + a);
        } else if (b > a && b > c) {
            System.out.println("b最大,为" + b);
        } else if (c > a && c > b) {
            System.out.println("c最大,为" + c);
        } else if (a == b && a > c) {
            System.out.println("a和b最大,为" + a);
        } else if (a == c && a > b) {
            System.out.println("a和c最大,为" + a);
        } else if (b == c && a < b) {
            System.out.println("b and c are the largest, is" + B); 
        } 
    } 
}

Guess you like

Origin www.cnblogs.com/buildnewhomeland/p/12147279.html