Basic Java Syntax--Process Control

I. Classification

Sequential structure: program execution from top to bottom

Branch structure: if-else if-else, switch-case

Loop structure: while loop, for loop, do-while loop, enhanced for loop

2. Specific description

1. Branch structure

1.1if Branch Structure

 

 

 

 

 

Description

1.else structure is optional;

2.if-else structures can be nested;

3. If there is only one execution statement after the if-else structure, {} can be omitted.However, omitting is not recommended and readability is poor

Code 1:

public class test {
    public static void main(String[] args) {
        int age = 18;
        if (age >= 18) {
            System.out.println("You're an adult");//You're an adult
        }
        System.out.println("Congratulations");//Congratulations
    }
}

Code 2:

public class test {
    public static void main(String[] args) {
        int age = 23;
        if (age >= 18) {
            System.out.println("You're an adult");//You're an adult
        }else{
            System.out.println("There are many restrictions on being under age");
        }

        System.out.println("end");//end
    }
}

Code 3:

public class test {
    public static void main(String[] args) {
        int age = 30;
        if (age < 18) {
            System.out.println("Under age");
        } else if (age < 30) {
            System.out.println("Young adults");
        } else if (age < 50) {
            System.out.println("Adult");//Adult
        }else{
            System.out.println("old age");
        }
    }
}

1.2switch-case structure

 

 

Description:

1. Match the constants in each case in turn according to the values in the switch expression.Once the match is successful, it enters the corresponding case structure and invokes its execution statement.When the execution statement is invoked, execution statements in other case structures continue to be executed down until the break keyword or the end of this switch-case structure is encountered.
(2) break, which can be used in the switch-case structure, means that once this keyword is executed, the switch-case structure will be jumped out
(3) The expression in the switch structure can only be one of the six data types: byte, short, char, int, enumeration type (JDK5.0 new), String type (JDK7.0 new)
(4) Constants can only be declared after case.Scope cannot be declared.
The break keyword is optional.
Default: The else. default structure equivalent to the if-else structure is optional and flexible in location.
If multiple cases in the switch-case structure have the same execution statement, you can consider merging them.
break is optional in switch-case

public class test {
    public static void main(String[] args) {
        String season = "summer";
        switch (season){
            case "spring":
                System.out.println("In the warm spring, flowers are coming out with a rush");
                break;
            case "summer":
                System.out.println("Dog Days of Summer");//Dog Days of Summer
                break;
            case "autumn":
                System.out.println("Fresh autumn weather");
                break;
            case "winter":
                System.out.println("A world of ice and snow");
                break;
            default:
                System.out.println("Incorrect season input");
                break;
        }
    }
}

Scenarios for using branch structures:

For determined constants, which do not involve scope, the switch-case structure is preferred because it is slightly more efficient to execute.

2. Cycle structure

Four elements of a circular structure:

(1) Initialization conditions;

(2) Circular condition--boolean type;

(3) Circulating body;

(4) iteration conditions;

2.1for loop

 

Description:

(1) The loop condition part is an expression of boolean type, generally ending the loop when the loop condition part returns false;

(2) The initialization conditions section can declare multiple variables of the same type;

(3) The iteration part may also declare multiple

Code:

public class test {
    public static void main(String[] args) {
        //Traverse 1-100 And print its sum
        int sum = 0;
        for (int i = 1;i <= 100;i++){
            if (i % 2 == 0) {
                System.out.println(i);
                sum += i;
            }
        }
        System.out.println(sum);//2550
    }
}

2.2 while loop

 

Description:

(1) Don't forget the iteration section, otherwise you will fall into an infinite loop;

(2) while and for loops can be converted to each other;

Code:

public class test {
    public static void main(String[] args) {
        //Traverse 1-100 Between odd numbers and print their sum
        int sum = 0;
        int i = 1;
        while (i <= 100) {
            if (i % 2 != 0) {
                System.out.println(i);
                sum += i;
            }
            i++;
        }
        System.out.println(sum);//2500
    }
}

2.3do-while cycle

 

Description:

(1) The do-while structure executes the loop body at least once;

(2) In the development, while and for are used more, do-while is rarely used;

Code:

public class test {
    public static void main(String[] args) {
        //Traverse 1-100 Between odd numbers and print their sum
        int sum = 0;
        int i = 1;
        do {
            if (i % 2 != 0) {
                System.out.println(i);
                sum += i;
            }
            i++;
        }while (i <= 100);
        System.out.println(sum);//2500
    }
}

3. Infinite loop structure

while(true) or for(;;)

4. Determine if a loop has been executed?

(1) Loop condition returns false;

(2) to break;

5. Loop Nesting

Remember one point: outer loop controls the number of rows, and inner loop controls the number of columns.

public class test {
    public static void main(String[] args) {
        //multiplication table
        for (int i = 1;i <= 9;i++){
            for (int j = 1;j <= i;j++){
                System.out.print(j + "*" + i + "=" + (j * i) + "\t");
            }
            System.out.println();
        }
    }
}

6.break keyword

Can be used in switch-case structure, loop structure.Indicates the end of the current loop, and the execution statement cannot be declared after the keyword.

Code:

public class test {
    public static void main(String[] args) {
        for (int i = 0;i < 10;i++){
            if (i == 3) {
                break;
            }
            System.out.println("i = " + i);
        }
        System.out.println("Game over");//This statement can be executed because it has already been issued for Circulated
    }
}

Tagged break:

public class test {
    public static void main(String[] args) {
        label:for (int i = 0;i < 10;i++){
            for (int j = 0;j < 10;j++){
                if (j == 4) {
                    break label;
                }
                System.out.println("i * j = " + (i * j));
            }
        }
        System.out.println("Game over");//This statement can be executed because it has already been issued for Circulated
    }
}

7.continue keyword

Use in the loop structure to end the current loop.Execution statement cannot be declared after keyword.

Code:

public class test {
    public static void main(String[] args) {
        for (int i = 0;i < 100;i++){
            if (i % 10 == 0) {
                continue;
            }
            System.out.println(i);
        }
    }
}

 

 

 

Author: The Beauty of Java

Date: 2020-03-28

Tags: Java Spring

Posted on Fri, 27 Mar 2020 11:28:19 -0700 by amin1982