Introduction to three Java process structures

Sequential structure

  • 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 from top to bottom. It is executed by several in turn

Select structure

if single selection structure

  • We often need to judge whether something is feasible, and then we can execute it. Such a process is represented by if statement in the program
package com.Flonx.Practice;

import java.util.Scanner;

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

        System.out.println("Please enter the content: ");

        String s = scanner.nextLine();
        if (s.equals("Hello")){
            System.out.println(s);
        }
        System.out.println("End");

        scanner.close();
    }
}

if double selection structure

package com.Flonx.Practice;

import java.util.Scanner;

public class Practice{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter your grade:");
        int score = scanner.nextInt();

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


        scanner.close();
    }
}

if multiple selection structure

package com.Flonx.Practice;

import java.util.Scanner;

public class Practice{
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter the score: ");
        int score  = scanner.nextInt();
        // Once one else if statement is detected as true, the other else if and else statements will skip execution
        if (score == 100){
            System.out.println("Congratulations, full marks");
        }else if (score < 100 && score >= 90){
            System.out.println("A");
        }else if (score < 90 && score > 80){
            System.out.println("B");
        }else{
            System.out.println("C");
        }
    }
}

Nested if structure

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

switch multiple selection structure

  • Another implementation method of multi selection structure is switch case statement
  • The switch case statement determines whether a variable is equal to a value in a series of values. Each value is called a branch
  • The variable types can be byte, short, int, case, string (starting with Java 7), and the case label must be a string constant or literal

Cyclic structure

while Loop

  • while is the most basic loop
  • As long as the Boolean expression is true, the loop will continue to execute
  • In most cases, we will stop the loop. We need a way to invalidate the expression to end the loop
  • In a few cases, the loop needs to be executed all the time, such as the request listening of the server
  • If the loop condition is always true, it will cause infinite loop [dead loop]. We should try our best to avoid dead loop in normal business programming, which will affect the program performance or cause the program to get stuck

do... while loop

  • For the while 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
  • The do... While loop is similar to the while loop. The difference is that the do... While loop is similar. The difference is that the do... While loop will be executed at least once
  • The difference between while and do... While loops:
    • while is to judge before execution, do... while is to judge after execution
    • do... while always ensures that the loop will be executed at least once, which is their main difference

for loop

  • Although many loop structures can be represented by while or do... While, Java provides another statement - for loop, which makes some loop structures simpler
  • for loop statement is a general structure that supports iteration. It is the most effective and flexible loop structure
  • The number of times the for loop is executed is determined before execution

Enhanced for loop (Java 5)

  • Java 5 introduces an enhanced for loop that is primarily used for arrays or collections

  • Syntax format:

    for (Declaration statement: expression){
        //Code sentence
    }
    
  • Declaration statement: declare a new local variable. The type of the variable must match the type of the array element. Its scope is limited to the circular statement block, and its value is equal to the value of the array element at this time

  • Expression: an expression is the name of the array to be accessed, or a method whose return value is an array

int[] numbers = {10, 20, 30, 40, 50};

//Traversing the elements of an array 
for (int x: numbers){
    System.out.println(x);
}

break continue

  • Break in the main part of any loop statement, you can use break to control the flow of the loop. Break is used to forcibly exit the loop without executing the remaining statements in the loop. (break statement is also used in switch statement)

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

  • About goto keyword

    • Goto keyword appeared in programming for a long time. Although goto is still a reserved word of Java, it has not been officially used in the language; Java does not have goto. However, we can still see the shadow of goto in the two keywords break and continue -- the labeled break and continue
    • "Label" refers to the identifier followed by a colon, for example: label;
    • For Java, the only place where the label is used is before the loop statement, and the only reason for setting the label before the loop is that we want to nest another loop in it. Because the break and continue keywords usually only interrupt the current loop, but if they are used with the label, they will interrupt to the place where the label exists
    int count = 0;
    outer: for (int i = 101; i < 150; i++){
        for (int j = 2; j < i/2; j++){
            if (i % j == 0){
                continue outer;
            }
        }
        System.out.println(i + " ");
    }
    

Keywords: Java Programming JavaSE

Added by londonjustin on Sat, 19 Feb 2022 14:45:13 +0200