Select structure statement if, switch

Select structure statement

In real life, we often need to make some judgments or choices, code is no exception, in Java, there is a special statement called selection structure statement. The select structure statements are also divided into if statements and switch statements.

if statement

  • if single choice structure
if single selection structure:
	If (judgment condition){
        //Statement that will execute if the condition is true
    }

The judgment condition is a Boolean value. When the judgment condition is true, the execution statement in {} will be executed
package com.wang.struct;

import java.util.Scanner;

public class IfDemo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter:");
        String s = scanner.nextLine();

        //equals: determine whether the strings are equal
        if (s.equals("Hello")) {
            System.out.println(s);
        }

        System.out.println("End");
        scanner.close();
    }
}

  • if double selection structure
if double selection structure:
	If (judgment condition){
        //Statement that will execute if the condition is true
    } else {
        //Statement to be executed if the condition is not true (false)
    }

The judgment condition is a Boolean value. When the judgment condition is true, the execution statement in {} after if will be executed,
Otherwise, the execution statement of {} after else will be executed
package com.wang.struct;

import java.util.Scanner;

public class IfDemo2 {
    public static void main(String[] args) {
        //If the examination score is more than 60, it means passing, if it is less than 60, it means failing

        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter your score:");
        int score = scanner.nextInt();

        if (score > 60) {
            System.out.println("Pass the grade");
        } else {
            System.out.println("Fail in grade");
        }
        scanner.close();
    }
}
  • if multi selection structure
if multi selection structure:
	If (judgment condition 1){
        //If condition 1 is true, the statement 1 will be executed
    }Else if (judgment condition 2){
        //If condition 2 is true, the statement 2 will be executed
    }Else if (judgment condition 3){
        //If condition 3 is true, the statement 3 will be executed
    } else {
        //If none of the above conditions are true (false), the statement 4 will be executed
    }

The judging condition is a Boolean value. When the judging condition 1 is true, the executing statement 1 in {} after if will be executed,
When judgment condition 1 is false, judgment condition 2 will continue to be executed. If it is true, statement 2 will be executed, and so on
 If all the judging conditions are false, it means that all the conditions are not met, and the {} after else will be executed
 Execute statement 4
package com.wang.struct;

import java.util.Scanner;

public class IfDemo3 {
    public static void main(String[] args) {
        //Test score: ABCD grade

        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter your score:");
        int score = scanner.nextInt();

        if (score == 100) {
            System.out.println("Congratulations, full marks!");
        } else if (score < 100 && score >= 90) {
            System.out.println("A level");
        } else if (score < 90 && score >= 80) {
            System.out.println("B level");
        } else if (score < 80 && score >= 70) {
            System.out.println("C level");
        } else if (score < 70 && score >= 60) {
            System.out.println("D level");
        } else if (score < 60 && score >= 0) {
            System.out.println("Fail in grade");
        } else {
            System.out.println("Illegal achievements");
        }
        scanner.close();
    }
}
  • Nested if structure
Nested if structure:
	If (judgment condition 1){
        If (judgment condition 2){
            //Execution code
        }
    }

Nested if structure is to nest another if structure in one if structure

switch Statements

  • switch multi choice structure

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

switch(Control expression) {
    case value1:
        //Execute statement 1
        break;
    case value2:
        //Execute statement 2
        break;
    default:
        //Execution statement
        break;
}

Note: the result type of control expression can be: byte, short, int, char, enumeration;

Starting with Java se 7, String type is supported

package com.wang.struct;

public class SwitchDemo1 {
    public static void main(String[] args) {
        //Grade of achievement
        char grade = 'B';

        /*case It has the effect of penetration. If a case block ends without writing break,
        The code will continue to execute the next case block through, and the switch will match a specific value*/
        switch (grade) {
            case 'A':
                System.out.println("excellent");
                break;//Jump out of the loop, write or omit
            case 'B':
                System.out.println("good");
                break;
            case 'C':
                System.out.println("pass");
                break;
            case 'D':
                System.out.println("Efforts must be made");
                break;
            case 'E':
                System.out.println("Fail");
                break;
            default://Optional
                System.out.println("Unknown level");
                break;
        }
    }
}

java 7 new features, expression results can be strings!!!. Whether the character itself is a number or not can be verified by Decompilation

package com.wang.struct;

public class SwitchDemo2 {
    public static void main(String[] args) {
        //The new feature of JDK7, the result of expression can be string!!!
        String name = "Zhang Wei";

        switch (name) {
            case "Zhuge vigorously":
                System.out.println("I am Zhuge Dali!");
                break;
            case "Zhang Wei":
                System.out.println("I am Zhang Wei.");
                break;
            default:
                System.out.println("What kind of class!");
                break;
        }
    }
}
Published 9 original articles, won praise 2, visited 153
Private letter follow

Tags: Java less

Posted on Sun, 02 Feb 2020 08:16:20 -0500 by Merdok