Java process control

Posted by ayzee01 on Wed, 12 Jan 2022 21:50:03 +0100

Java process control

Sequential structure

1. The basic structure of Java is a sequential structure. Unless otherwise specified, it will be executed sentence by sentence in order

2. Sequential structure is the simplest algorithm structure

3. Between statements and between boxes, it is carried out from top to bottom, which is composed of several processing steps executed in turn. It is a basic algorithm structure that any algorithm is inseparable from

Select structure

if single selection structure
if(Boolean expression){
  //If the Boolean expression is true, the statement is executed
}
if double selection structure
if(Boolean expression){
  //If the Boolean expression is true, the value is true
}else{
  //If the value of Boolean expression is false
}
if multiple selection structure (judgment)
if(Boolean expression 1){
  //If the value of Boolean expression 1 is true, execute the code
}else if(Boolean expression 2){
  //If the value of Boolean expression 2 is true, execute the code
}else if(Boolean expression 3){
  //If the value of the Boolean expression is true, execute the code
}
.........................
  else{
    
  }

The if statement can only have one else statement at most, and the else statement is after all else if statements

Nested if structure
if(Boolean expression 1){
  ///If the value of Boolean expression 1 is true, execute the code
  if(Boolean expression (2){
    ///If the value of Boolean expression 2 is true, execute the code
  }
}
switch multiple selection structure (matching specific values)

1.switch case statement determines whether a variable is equal to a value in a series of values. Each value is called a branch

2. The variable types in the switch statement can be:

#byte, short, int or char

#switch supports String type

#At the same time, the case tag must be a string constant or literal

switch(expression){
  case value: //sentence
       break;//Optionally, do not write break, which will execute all the following and penetrate the case.
  case value: //sentence
       break;
  //There can be any number of case statements
  default:    //Optional
       sentence
}

Code learning:

package com.zou.Struct;

import java.util.Scanner;

public class Demo26 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        System.out.println("Please enter grade:");
        char grade = scanner.next().charAt(0);
        /*Enter the statement char type, where the reason for 0 is: in the Scanner source code,
        String The type actually calls the Next() method, and the input character actually
        Or enter a string and take the first character of the string. If you want to take the second one
        Or the third character is 1, 2. Push it down
        */
        switch (grade){
            case 'A':
                System.out.println("excellent");
                break;
            case 'B':
                System.out.println("good");
                break;
            case 'C':
                System.out.println("fail,");
                break;
            default:
                System.out.println("Unknown input");
        }//case penetration, if no break statement is added, the following will be executed.
        scanner.close();
    }

}

Cyclic structure

while Loop
while(Boolean expression){
  //Cyclic content
}

1. As long as the Boolean expression is true, the loop will continue to execute.

2. In most cases, the loop will stop. We need a method to invalidate the expression to end the loop

package com.zou.Struct;

public class Demo27 {
    public static void main(String[] args) {
        //Output 1-100
        int i=0;
        while (i<100){
            i++;
            System.out.println(i);
        }
    }
}
do... whlie loop

1. For the whlie statement, if the condition is not met, it cannot enter the loop. But sometimes we need to perform at least once even if the conditions are not met.

do{
  //Code statement
}while(Boolean expression);

The difference between while and do while

1.while judge before execute, do... while execute before Judge

2.do... while can always ensure that the loop body is executed at least once!

package com.zou.Struct;

public class Demo28 {
    public static void main(String[] args) {
        int a = 0;
        while (a < 0) {
            System.out.println(a);
            a++;
        }
        System.out.println("===================");
        do {
            System.out.println(a);
            a++;
        }while (a<0);

    }
}
for loop

1. The for loop makes some loop structures simpler

2.for loop statement is a general structure that supports iteration. It is the most effective and flexible loop structure

3. The execution times of the for loop are determined before execution. The syntax format is as follows:

for(initialization; Boolean expression; to update){
  //Code statement
}
/*The initialization step is performed first. You can declare a type, but you can initialize one or more loop control variables or empty statements.

code:

package com.zou.Struct;

public class Demo30 {
    public static void main(String[] args) {
        //Calculates the sum of odd and even numbers between 0 and 100
        int addSum = 0;//Odd number
        int evenSum = 0;//even numbers
        for (int i = 0; i < 100; i++) {
          if (i%2!=0)  {
              addSum+=i;
          }else {
              evenSum+=i;
          }
        }
        System.out.println(addSum);
        System.out.println(evenSum);
    }
}

99 multiplication table

package com.zou.Struct;

public class Demo31 {
    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+"="+(i*j)+"\t");
            }
            System.out.println();
        }
    }
}
Enhanced for loop (to be supplemented later)

==================================================

break continue statement

1.break in the main part of any loop statement, you can use break to control the loop flow. Break is used to forcibly exit the loop. Do not execute the remaining statements in the loop.

2.continue statement in the body of a loop statement is used to terminate a loop process, that is, skip the statements that have not been executed in the loop, and then judge whether to execute the loop next time.

Topics: Java Back-end