Sequential structure and if usage

Posted by Blade280891 on Sat, 11 Dec 2021 15:01:28 +0100

Sequential structure

Daily Quotes

Tears do not mean whose failure, smile does not mean whose success—— Inspirational quotes

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

  • Sequential structure is the simplest algorithm structure.

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

  • package com.zhuanhun.struct;
    
    public class SunXuDemo01 {
        public static void main(String[] args) {
    
            //Execute in order (sequential structure)
            System.out.println("hello01");
            System.out.println("hello02");
            System.out.println("hello03");
            System.out.println("hello04");
            System.out.println("hello05");
        }
    }
    
    

Select structure

  • if radio structure
  • if double choice structure
  • if multiple selection structure
  • Nested if structure
  • switch multiple selection structure

if single selection structure

  • We often need to judge whether something is feasible before we execute it. Such a process is represented by an if statement in the program.

  • Syntax:

    if(Boolean expression){
        //The statement that will be executed if the Boolean expression is true
    }
    
package com.zhuanhun.struct;

import java.util.Scanner;

public class IfDemo01 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);

        System.out.println("Please enter:");
        String s=scanner.nextLine();

        //equals: to judge whether strings are equal, use "= =" to judge strings less
        if(s.equals("Hello")){
            System.out.println(s);
        }
        System.out.println("end");
        scanner.close();
    }
}

if double selection structure

  • Now there is a demand. The company wants to buy a software. If it succeeds, it will pay 1 million yuan. If it fails, it will find someone to develop it by itself. Such a requirement cannot be met with an if. We need to have two judgments and a double selection structure, so we have an if - else structure.

  • Syntax:

    if(Boolean expression){
        //If the Boolean expression has a value of true
    }else{
        //If the value of the Boolean expression is false
    }
    
package com.zhuanhun.struct;

import java.util.Scanner;

public class IfDemo02 {
    public static void main(String[] args) {
        //If the test score is greater than 6o, you will pass, otherwise you will fail
        Scanner scanner=new Scanner(System.in);

        System.out.println("Please enter your grade:");
        double score=scanner.nextDouble();

        //score
        if(score>60){
            System.out.println("pass");
        }else{
            System.out.println("fail,");
        }

        scanner.close();
    }
}

if multiple selection structure

  • Now it is found that code double selection does not meet the requirements. In the real situation, there may be ABCD and interval multi-level judgment, such as 90-100 is A, 80-90 is B... Etc. in life, we often have more than two choices, so we need A multi-choice structure to deal with such problems.

  • Syntax:

    if(Boolean expression 1){
        //If the Boolean expression 1 value is true, execute the code
    }else if(Boolean expression 2){
          //If the Boolean expression 2 value is true, execute the code
    }else if(Boolean expression 3){
          //If the Boolean expression 3 value is true, execute the code
    }else{
        //If the above Boolean expression is not true, execute the code
    }
    
package com.zhuanhun.struct;

import java.util.Scanner;

public class IfDemo03 {
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
    /*
    if There is one else statement after all else if statements.
    if Statement can have several else if statements, which must follow else.
    Once one else if statement is detected as true, the other else if and else statements will skip execution.
    */
        System.out.println("Please enter your grade:");
        double score=scanner.nextDouble();

        if(score==100){
            System.out.println("Out of 100");
        }else if(score>=90&&score<100){
            System.out.println("excellent");
        }else if(score>=80&&score<90){
            System.out.println("good");
        }else if(score>=70&&score<80){
            System.out.println("in");
        }else if(score>=60&&score<70){
            System.out.println("commonly");
        }else if(score>=0&&score<60){
            System.out.println("difference");
        }else {
            System.out.println("Illegal score input!!");
        }
        scanner.close();
    }
}

Nested if structure

  • It is legal to use nested if... Else statements. That is, you can use an if or else if statement in another if or else if statement. You can nest else if... Else like an IF statement.

  • Syntax:

    if(Boolean expression 1){
        Expression 1 is true Execute code
        if(Boolean expression 2){
             Expression 1 is true Execute code
        }
    }
    

Daily News

Titanium media T-EDGE

Topics: Java Back-end