JavaSE note 02 - process control, loop control, Random

Keywords: Java Algorithm JavaSE

1. Branch management

1.1 process control

  • Process control statement classification
    • Sequential structure

      • The simplest and most basic control process is executed in sequence without specific syntax structure
      • Execution sequence: start → statement A → statement B → statement C → end
      • Code example
      public class OrderDemo {
          public static void main(String[] args) {
              System.out.println("start"); 
              System.out.println("sentence A"); 
              System.out.println("sentence B"); 
              System.out.println("sentence C");
              System.out.println("end");  
          }
      }
      
    • Branch structure if switch

      • if statement 1

        • Format:
        if(Relational expression){
        	Statement body;
        }
        
        • Execution process:
          • First, evaluate the value of the relationship expression
          • If the value of the relational expression is true, the statement body is executed
          • If the value of the relational expression is false, the statement body is not executed
          • Continue with the following
        • Code example
        public class IfDeno {
            public static void main(String[] args) {
                System.out.println("start"); 
                int a = 10;
                int b = 20;
                if(a == b){
                    System.out.println("equal");
                }
                System.out.println("Unequal");
        
                int c = 10;
                if(a == c){
                    System.out.println("equal");
                }
                System.out.println("end");  
            }
        }
        
      • if statement 2

        • Format:
        if(Relational expression){
        	Statement body 1;
        }else{
        	Statement body 2;
        }
        
        • Execution process:
          • First, evaluate the value of the relationship expression
          • If the value of the relational expression is true, statement body 1 is executed
          • If the value of the relationship expression is false, statement body 2 is executed
          • Continue with the following
        • Code example
        public class IfDemo2 {
            public static void main(String[] args) {
                System.out.println("start"); 
                int a = 10;
                int b = 20;
                if(a > b){
                    System.out.println("a The value of is greater than b");
                }else{
                    System.out.println("a The value of is not greater than b");
                }
                System.out.println("end"); 
            }
        }
        
      • if case

        • Odd even judgment
        import java.util.Scanner;
        public class test3 {
            public static void main(String[] args) {
                Scanner sc = new Scanner(System.in);
                System.out.println("Please enter a number:");
                int i = sc.nextInt();
                if((i % 2) == 0){
                    System.out.println("The number entered is even");
                }else{
                    System.out.println("The number entered is odd");
                }
            }
        }
        
      • if statement 3

        • Format:
        if(Relationship expression 1){
        	Statement body 1
        }else if(Relational expression 2){
        	Statement body 2
        }
        ......
        else{
        	Statement body n
        }
        
        • Execution process:
          • First, evaluate the value of relationship expression 1
          • If the value is true, statement body 1 is executed, and if the value is false, the value of relationship expression 2 is calculated
          • If the value is true, the statement body 2 is executed, and if the value is false, the value of relationship expression 3 is calculated
          • ......
          • If the value of the relational expression is true, the statement body n is executed
        • Code example
        import java.util.Scanner;
        
        public class IfDemo3 {
            public static void main(String[] args) {
                System.out.println("start");
                Scanner sc = new Scanner(System.in);
                System.out.println("Please enter a week (number 1-7): ");
                int i = sc.nextInt();
                if(i == 1){
                    System.out.println("Monday");
                } else if(i == 2){
                    System.out.println("Tuesday");
                } else if(i == 3){
                    System.out.println("Wednesday");
                } else if(i == 4){
                    System.out.println("Thursday");
                } else if(i == 5){
                    System.out.println("Friday");
                } else if(i == 6){
                    System.out.println("Saturday");
                } else if(i == 7){
                    System.out.println("Sunday");
                } else {
                    System.out.println("Input error");  
                }
        
                System.out.println("end");
                
            }
        }
        
      • switch Statements

        • Format:
        switch(expression){
        	case Value 1:
        					Statement body 1;
        					break;
        	case Value 2:
        					Statement body 2;
        					break;
        	......
        	default:
        					Statement body n;
        					[break;]
        }
        
        • Format Description:
          • Expression: the values are byte, short, int and char. After JDK5, it can be enumeration, and after JDK7, it can be String
          • case: followed by the value to be compared with the expression
          • break: it means to interrupt and end. It is used to end the switch statement
          • default: indicates that the content is executed when all conditions do not match, which is similar to else of if statement
        • Execution process
          • First, evaluate the value of the expression
          • Compare with the value after case in turn. If there is a corresponding value, execute the corresponding statement. In the process of execution, it ends when a break is encountered.
          • If the values and expressions behind all case s do not match, the statement body in default will be executed, and the program will end
        • Code example:
        import java.util.Scanner;
        
        public class SwitchDeno {
            public static void main(String[] args) {
                System.out.println("start");
                Scanner sc = new Scanner(System.in);
                System.out.println("Please enter a week (number 1-7): ");
                int i = sc.nextInt();
                switch(i){
                    case 1:
                        System.out.println("Monday");
                        break;
                    case 2:
                        System.out.println("Tuesday");
                        break;
                    case 3:
                        System.out.println("Wednesday");
                        break;
                    case 4:
                        System.out.println("Thursday");
                        break;
                    case 5:
                        System.out.println("Friday");
                        break;
                    case 6:
                        System.out.println("Saturday");
                        break;
                    case 7:
                        System.out.println("Sunday");
                        break;
                    default:
                        System.out.println("error");
                        break;
                }
            } 
        }
        
        • matters needing attention:
          • In the switch statement, if break is not written behind the case controlled statement body, penetration will occur. Without judging the value of the next case, run down until break is encountered and the operation ends.

1.2 circular statements

  • Circular statement
    • for loop statement

      • features:
        • Repeat sth
        • There are clear start and stop signs
      • form:
        • Initialization statement: used to mark the starting state of the cycle. In short, it is what the cycle looks like when it starts.
        • Condition judgment statement: it is used to indicate the condition of repeated execution of the loop. In short, it is whether it can be executed all the time.
        • Loop body statement: used to represent the content of repeated execution of a loop. In short, it is something that is executed repeatedly.
        • Conditional control statement: used to represent the contents of each change in the execution of the loop. In short, it controls whether the loop can be executed
      • Corresponding syntax of loop structure:
        • Initialization statement: this can be one or more statements to complete some initialization operations
        • Conditional judgment statement: here, an expression with the result value of boolean type is used, which can determine whether to execute the loop body
        • Loop body statement: here can be any statement, which will be executed repeatedly
        • Conditional control statement: here, a statement is usually used to change the value of a variable, so as to achieve the effect of loop control whether to continue or not.
      • Format:
      for(Initialization statement;Conditional judgment statement;Conditional control statement){
      		Loop body statement;
      }
      
      Dead loop format: (always execute loop body statement)
      for(;;){
      	Loop body statement
      }
      
      • Execution process
        • Execute initialization statement
        • Execute the conditional judgment statement, and the result is true or false
          • true, continue execution
          • false, end execution
        • Execute loop body statement
        • Execute conditional control statements
        • Go back to step two and continue
      • Code example
      public class ForDemo1 {
          public static void main(String[] args) {
              //Output HelloWorld multiple times on the console
              for(int i = 1;i < 6;i++){
                  System.out.println("HelloWorld");
              }
          }
      }
      
    • while loop statement

      • Format:
      Basic format:
      while(Conditional judgment statement){
      	Loop body statement;
      }
      
      Full format:
      Initialization statement;
      while(Conditional judgment statement){
      	Loop body statement;
      	Conditional control statement;
      }
      
      Dead loop format:
      while(true){
      	Loop body statement;
      	Conditional control statement;
      }
      
      • Execution process
        • Execute initialization statement
        • Execute the conditional judgment statement to see whether the result is true or false
          • If false, the loop ends
          • If true, continue
        • Execute loop body statement
        • Execute conditional control statements
        • Go back to step two and continue
      • Code example:
      public class WhileDemo1 {
          public static void main(String[] args) {
              //Output HelloWorld 5 times on the console
              //for loop implementation
              for(int i = 0 ; i <= 5; i++){
                  System.out.println("helloworld");
              }
              //while loop implementation
              int i = 1;
              while(i<=5){
                  System.out.println("HelloWorLD");
                  i++;
              }
          }
      }
      
    • do... while loop statement

      • Format:
      Basic format:
      do{
      	Loop body statement;
      }while(Conditional judgment statement);
      
      Full format:
      Initialization statement;
      do{
      	Loop body statement;
      	Conditional control statement;
      }while(Conditional judgment statement);
      
      Dead loop format:
      Initialization statement;
      do{
      	Loop body statement;
      	Conditional control statement;
      }while(true);
      
      • Execution process
        • Execute initialization statement
        • Execute loop body statement
        • Execute conditional control statements
        • Execute the conditional judgment statement to see whether the result is true or false
          • true, continue execution
          • false, the loop ends
        • Go back to step two
      • Code example:
    • The difference between the above three cycles

      • Three differences
        • The for loop and the while loop first determine whether they are valid, and then decide to execute the loop body
        • The do... while loop is executed once to determine whether it is valid
      • for and while
        • The self increasing variable controlled by the conditional control statement belongs to the for loop structure, and can no longer be accessed after the for loop ends
        • The self increasing variable controlled by the conditional control statement does not belong to the starting syntax structure for the while loop. Its variable can still be used after the loop ends
    • Jump control statement

      • summary:


    - continue Used in a loop. Based on condition control, skip the execution in a loop and continue the next execution.
    - break Used in a loop to terminate the execution of the contents of the loop body based on condition control, that is, to end the current whole loop
    - Code example:

    ```java
    public class ControlDemo {
        public static void main(String[] args) {
            for(int i = 1; i<= 5;i++){
                if(i%2 == 0){
                    // continue;//1  3  5
                    break;//1
                }
                System.out.println(i);
            }
            
        }
    }
    ```

- loop nesting 
    - Statement order:
        - Sequential statements, ending with a semicolon, indicate the end of a sentence
        - Branch statement
            - A pair of braces indicates if Overall structure, overall description, a complete if sentence
            - A pair of braces indicates switch Overall structure, overall description, a complete switch sentence
        - Circular statement
            - A pair of braces indicates for Overall structure, overall description, a complete for sentence
            - A pair of braces indicates while Overall structure, overall description, a complete while sentence
            - do...while End with a semicolon and describe a complete picture as a whole do...while sentence

1.3 Random

  • Random action and use steps

    • Function to generate a random number
    • Use steps
      • import java.util.Random;
      • Create object Random r = new Random();
      • Obtain the random number int number = r.nextInt(10)// Get data range, [0,10)
    • Code example:
    import java.util.Random;
    
    public class RandomDemo {
        public static void main(String[] args) {
            Random r = new Random();
            for(int i = 0; i <10 ; i++){
                int unmber = r.nextInt(10);
                System.out.println(unmber);
            }
            //Gets a random number between 1 and 100
            int x = r.nextInt(100) + 1;
            System.out.println(x);
    
        }
    }
    
    • Number guessing game:
    import java.util.Random;
    import java.util.Scanner;
    
    public class test4 {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            Random r = new Random();
            System.out.println("Please enter 1-100 Any number between:");
            int j = r.nextInt(100) + 1;
            while (true) {
                int i = sc.nextInt();
                if(i > j ){
                    System.out.println("Big");
                }else if(i < j){
                    System.out.println("Small");
                }else {
                    System.out.println("Yes");
                    break;
                }
            }
    
        }
    }
    

Posted by cjmling on Mon, 04 Oct 2021 17:09:02 -0700