Three basic statements of Java

Catalogue

(1) Judgment statement

(1) if statement

(1) Concept

(2) Format

(3) Code test

(2) If else statement

(1) Concept

(2) Format

(3) Code test

(3) If else else if statement

(1) Concept

(2) Format

(3) Code test

(2) Select statement

(1) switch

(1) Concept

(2) Format

(3) Code test

(3) Circular statement

(1) for loop

(1) Concept

(2) Format

(3) Code test

(2) while loop

(1) Concept

(2) Format

(3) Code test

(3) do...while loop

(1) Concept

(2) Format

(3) Code test

(4) Expand

(1) The difference between for and while

(2) Jump out statement

(1)break

(2)continue

(3) Dead cycle

(4) for nested loop

(1) Judgment statement

(1) if statement

(1) Concept

The if statement belongs to a branch structure, and the corresponding execution statement or condition will be selected according to the judgment of the expression.

(2) Format

(3) Code test

package cn.tedu.exercise;
​
import java.util.Scanner;
​
public class biji {
    public static void main(String[] args) {
        System.out.println("Please enter an integer:");
        int number=new Scanner(System.in).nextInt();
        if(number%2==0){
            System.out.println(number+"Can be divided by 2");
        }
    }
}

(2) If else statement

(1) Concept

In the multi branch structure, there are corresponding execution results for the satisfied judgment results, and there are also corresponding statements for the unsatisfied judgment statements.

(2) Format

(3) Code test

package cn.tedu.exercise;
​
import java.util.Scanner;
​
public class biji {
    public static void main(String[] args) {
        System.out.println("Please enter an integer:");
        int number=new Scanner(System.in).nextInt();
        if(number%2==0){
            System.out.println(number+"Can be divided by 2");
        }else {
            System.out.println(number+"Cannot be divided by 2");
        }
    }
}

(3) If else else if statement

(1) Concept

Nested loops. There are many judgment conditions (more than two).

(2) Format

(3) Code test

package cn.tedu.basic;
​
import java.util.Scanner;
​
public class TestScore {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Please enter your grade:");
        Double score=new Scanner(System.in).nextDouble();
        if(score<0||score>100) {
            System.out.println("The input data is incorrect!");
        }else
        {
            if(score>=90){
                System.out.println("excellent");
            }else if(score>=80) {
                System.out.println("good");
            }else if(score>=70) {
                System.out.println("secondary");
            }else if(score>=60) {
                System.out.println("pass");
            }else {
                System.out.println("fail,");
            }
            /*
             * In order to enhance the robustness of the program, we can judge the score
             * If it is a score within a reasonable range, judge that the segment is unreasonable and end the procedure
             */
        }
    }
}

(2) Select statement

(1) switch

(1) Concept

The switch case statement is used to judge whether a variable is equal to a value in a series of values. Each value is called a branch. When a case is established, it penetrates all cases backward from this case, including default, until the program ends or encounters a break.

(2) Format

(3) Code test

package cn.tedu.basic;
​
public class TestSwitch2 {
​
    public static void main(String[] args) {
        String s="5";
        switch(s) {
            case "1" :System.out.println("Eat pickled vegetables");break;
            case "2" :System.out.println("Eat dry pot");break;
            case "3" :System.out.println("Eat barbecue");break;
            case "4" :System.out.println("Eat spicy hot");break;
            case "5" :System.out.println("Eat hot pot");break;
            case "6" :System.out.println("Eat crayfish");break;
            case "7" :System.out.println("having dinner");break;
            default:System.out.println("impossible");
        }
    }
}

(3) Circular statement

(1) for loop

(1) Concept

Loop structure refers to a program structure that needs to execute a function repeatedly in the program. It determines whether to continue to perform a function or exit the loop according to the conditions in the loop body. According to the judgment conditions, the loop structure can be subdivided into the loop structure of judgment before execution and the loop structure of execution before judgment.

(2) Format

(3) Code test

package cn.tedu.basic;
​
public class TestFor {
​
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for(int i=1;i<=100;i++) {
            System.out.println("It's not without that condition. It's over!");
        }
        for(int i=1;i<=10;i++) {
            System.out.println(i);
        }
    }
}

(2) while loop

(1) Concept

The same as the for() loop, the while loop judges first and then executes.

(2) Format

(3) Code test

package cn.tedu.basic1;
​
import java.util.Random;
import java.util.Scanner;
/*
 * This class is used to practice the while structure
 * Requirement: generate a random number for users to guess
 */
public class TestWhile {
    public static void main(String[] args) {
        int random=new Random().nextInt(100);
        //Parameter 100 is user-defined, and the generated random number range is an integer within [0100]
        while(true) {//Dead cycle
            //Note: the outlet must be set for the dead cycle
            System.out.println("Please enter the integer you want to enter:");
            int input =new Scanner(System.in).nextInt();
            if(input==random) {
                System.out.println("congratulations! You guessed right!");break;
            }else if (input>random) {
                System.out.println("Guess big!");
            }else if (input<random) {
                System.out.println("Guess small!");
            }
        }
    }
}

(3) do...while loop

(1) Concept

Same as for loop, but different from while, do While executes first and then judges.

(2) Format

(3) Code test

package cn.tedu.basic1;
​
import java.util.Scanner;
​
public class TestDowhile {
​
    public static void main(String[] args) {
        int random=new Scanner(System.in).nextInt(100);
        do {
            System.out.println("Guess what");
            int input=new Scanner(System.in).nextInt();
            if (input>random) {
                System.out.println("Guess big!");
            }else if (input<random) {
                System.out.println("Guess small!");
            }else if(input==random) {
                System.out.println("congratulations! You guessed right!");break;}
        }while(true);
    }
}

(4) Expand

(1) The difference between for and while

① After the for loop ends, it can no longer be used, but it can be used after the while loop ends, because after the for loop ends, the variable will disappear from memory, which can improve the efficiency of memory use.

② for is recommended when the number of cycles is known, and while is recommended when the cycle is unknown.

(2) Jump out statement

(1)break

① Overview: directly jump out of the loop, do not execute the statements in the loop statement, but execute the statements outside the loop body.

② Format:

switch(i){
    case (..):...break;
    case (..):...break;
    default:.......break;
        }

③ Code test:

package cn.tedu.basic;
​
public class TestSwitch {
    
    public static void main(String[] args) {
        int a =7;
        switch(a) {
            case 1:System.out.println(1);break;
            case 2:System.out.println(2);break;
            case 3:System.out.println(3);break;
            case 4:System.out.println(4);break;
            case 5:System.out.println(5);break;
            default:System.out.println(0);
        }
    }
}

(2)continue

① Overview: jump out of this cycle, do not execute this cycle statement, but continue to the next cycle.

② Format:

  int a =7;
       switch(a) {
           case 1:;break;
           case 2:;break;
           case 3:;continue;
           case 4:;break;
           case 5:;break;
           default:;
      }

③ Code test:

package cn.tedu.basic1;
​
import java.util.Scanner;
​
public class TestBreakAndContinue {
​
    public static void main(String[] args) {
        for(int i=1;i<=100;i++) {
            System.out.println("Please enter the integer you want to guess:");
            int input =new Scanner(System.in).nextInt();
            if(input!=88) 
                continue;
            if(input==88) {
                System.out.println("congratulations! You guessed right!");
                break;
            }
        }
    }
}

(3) Dead cycle

① while judgment statement. When the loop condition is true or 1, the loop is an endless loop.

② For loop statement, when the format is: for(;;) When, it can be regarded as an dead cycle.

(4) for nested loop

① The outer loop plus the inner loop, the outer loop is executed once, and the inner loop is executed n times.

② Format:

for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
        
    }
}

③ Code test:

package cn.tedu.basic;
​
public class TestForDemo {
​
    public static void main(String[] args) {
        /*
         * The outer loop controls the number of rows, and the inner loop controls the number of columns in a row
         */
        for(int i=1;i<=9;i++) {
            for(int j=1;j<=i;j++) {
                System.out.print(j+"x"+i+"="+i*j+"\t");
            }
            System.out.println();
        }
    }
}

Added by CookieDoh on Thu, 30 Dec 2021 14:04:14 +0200