5.Java basic syntax ----- Process Control

First, the classification

Sequential structure: program sequentially performed from the top down

Branch structure: if-else if-else, switch-case

Loop structure: while loop, a for loop, do-while loop, a for loop enhanced

Second, the specific instructions

1. The branched structure

1.1if branch structure

 

 

 

 

 

 Explanation

1.else structure is optional;

2.if-else structure can be nested;

3. If the structure is only an if-else statement is executed, the {} may be omitted. But it is not recommended omitted, poor readability

Code 1:

public class test {
    public static void main(String[] args) {
        int age = 18;
        if (age >= 18) {
            System.out.println ( "You have grown up"); // You have grown up 
        }
        System.out.println ( "Congratulations"); // Congratulations 
    }
}

Code 2:

public class test {
    public static void main(String[] args) {
        int age = 23;
        if (age >= 18) {
            System.out.println ( "You have grown up"); // You have grown up 
        } the else {
            System.out.println ( "There are a lot of minor restrictions" );
        }

        System.out.println("end");//end
    }
}

Code 3:

public class test {
    public static void main(String[] args) {
        int age = 30;
        if (age < 18) {
            System.out.println ( "minor" );
        } else if (age < 30) {
            System.out.println ( "young" );
        } else if (age < 50) {
            System.out.println ( "prime"); // prime 
        } the else {
            System.out.println("老年");
        }
    }
}

1.2switch-case structure

 

 

 Description:

The value of the switch expression ① sequentially matching constants in each case. Once the match is successful, the corresponding structure in the case, calling its execute the statement. When the call after executing the statement, the statement continues to execute down the implementation of other structures in the case until the end of the break keyword encountered or the end of this switch-case structure.
② break, can be used in switch-case structure, this represents a keyword upon execution, to come out switch-case structure
expression ③ switch structures, only one of the six following types of data: byte, short, char, int, enumerated types (JDK5.0 new), String type (JDK7.0 new)
after ④ case can only declare a constant. You can not be declared range.
⑤ break keyword is optional.
⑥ default: if-else equivalent structures else default configuration is optional, and the location is flexible.
⑦ If the case of a plurality of switch-case statement is performed in the same structure, may be considered to merge.
⑧ break in the switch-case is optional

public class test {
    public static void main(String[] args) {
        String season = "summer";
        switch (season){
            case "spring":
                System.out.println ( "Spring" );
                 BREAK ;
             Case "Summer" :
                System.out.println ( "hot summer" ); // hot summer
                 BREAK ;
             Case "Autumn" :
                System.out.println("秋高气爽");
                break;
            case "winter":
                System.out.println ( "snow" );
                 BREAK ;
             default :
                System.out.println ( "season mistyped" );
                 BREAK ;
        }
    }
}

Use scene branch structure:

For determining the constants, not related to the range, using the priority switch-case structure, since the higher the efficiency;

2. loop structure

4 Elements cycle structure:

① initial conditions;

② cycling conditions ---- boolean type;

③ loop;

④ iteration condition;

2.1for cycle

 

 Description:

The cycling conditions ① part boolean type expression, generally circular end portion when the loop condition returns false;

② a plurality of initial conditions of the same type can be declared part of the variable;

③ iteration section can also declare more

Code:

public  class Test {
     public  static  void main (String [] args) {
         // iterate between the even-numbered 1-100, and prints and their 
        int SUM = 0 ;
         for ( int I =. 1; I <= 100; I ++ ) {
             IF (I% 2 == 0 ) {
                System.out.println(i);
                sum += i;
            }
        }
        System.out.println(sum);//2550
    }
}

2.2while cycle

 

 Description:

① Do not forget the iteration part, or will fall into an infinite loop;

②while loop for circulating and can be converted to each other;

Code:

public  class Test {
     public  static  void main (String [] args) {
         // iterate between the odd-numbered 1-100, and prints and their 
        int SUM = 0 ;
         int I =. 1 ;
         the while (I <= 100 ) {
             IF ( 2% I! = 0 ) {
                System.out.println(i);
                sum += i;
            }
            i++;
        }
        System.out.println(sum);//2500
    }
}

2.3do-while loop

 

 Description:

①do-while performing at least one loop structure;

② development, while using more and for, do-while rarely used;

Code:

public  class Test {
     public  static  void main (String [] args) {
         // iterate between the odd-numbered 1-100, and prints and their 
        int SUM = 0 ;
         int I =. 1 ;
         do {
             IF (I = 2% 0! ) {
                System.out.println(i);
                sum += i;
            }
            i++;
        }while (i <= 100);
        System.out.println(sum);//2500
    }
}

3. infinite loop structure

while(true)或for(;;)

4. determine whether a cycle is finished?

Cycling conditions ① return false;

② execution to break;

5. nested loop

Remember one point: the outer loop controls the row number, column number of the inner loop control.

public class test {
    public static void main(String[] args) {
        //九九乘法表
        for (int i = 1;i <= 9;i++){
            for (int j = 1;j <= i;j++){
                System.out.print(j + "*" + i + "=" + (j * i) + "\t");
            }
            System.out.println();
        }
    }
}

6.break keyword

  It can be used in switch-case structure, loop structure. Indicates the end of the current cycle, the keyword can not execute the statement declared.

Code:

public class test {
    public static void main(String[] args) {
        for (int i = 0;i < 10;i++){
            if (i == 3) {
                break;
            }
            System.out.println("i = " + i);
        }
        System.out.println ( "Game Over"); // This statement can be executed, as it has been out for a loop 
    }
}

break with a label:

public class test {
    public static void main(String[] args) {
        label:for (int i = 0;i < 10;i++){
            for (int j = 0;j < 10;j++){
                if (j == 4) {
                    break label;
                }
                System.out.println("i * j = " + (i * j));
            }
        }
        System.out.println ( "Game Over"); // This statement can be executed, as it has been out for a loop 
    }
}

7.continue keyword

  Used in a loop configuration, as indicating the end of cycle. Keywords can not be declared after the statement is executed.

Code:

public class test {
    public static void main(String[] args) {
        for (int i = 0;i < 100;i++){
            if (i % 10 == 0) {
                continue;
            }
            System.out.println(i);
        }
    }
}

 

 

 

Author: Java beauty

Date: 2020-03-28

Guess you like

Origin www.cnblogs.com/897463196-a/p/12585401.html