JAVA process control statement

Posted by Warptweet on Fri, 28 Jan 2022 11:11:53 +0100

JAVA process control statement

Development tools and key technologies: idea java

Author: Xiao's Xiao ran

Written on: June 17, 2021

Process control statement: it can control the execution process of the program.

Classification:

Sequential structure

Select structure

Cyclic structure

Sequential structure:

From top to bottom, execute in sequence.

//Code snippet

class
ShunXuJieGouDemo {

    public static void main(String[] args) {

        System.out.println("The program begins");

        

        System.out.println("Java");

        

        System.out.println("The program is over");

    }

}

Select structure:

if statement

switch Statements

if statement:

Format 1

Format 2

Format 3

Format of if statement:

If (comparison expression){

Sentence body;

}

Execution process:

        First calculate the value of the comparison expression to see if its return value is true still false. 

        If it is true,Execute the body of the statement;

        If it is false,The body of the statement is not executed;

if Precautions for statement:

    A:Whether the comparison expression is simple or complex, the result must be boolean type

    B:if If the statement body controlled by the statement is a statement, the braces can be omitted;

      If there are multiple statements, they cannot be omitted. It is recommended never to omit.

    C:Generally speaking: there is no semicolon if there is a left brace, and there is no left brace if there is a semicolon

if statement format 1:

    if(Comparison expression) {

        Statement body;

    }

Execution process:

    First, calculate the value of the comparison expression to see if its return value is true still false. 

    If it is true,Execute the body of the statement;

    If it is false,The body of the statement is not executed;

    

Note: make sure that the result of the comparison expression is consistent with the result you want.

if statement format 2:

    if(Comparison expression) {

        Statement body 1;

    }else {

        Statement body 2;

    }

Execution process:

    First, calculate the value of the comparison expression to see if its return value is true still false. 

    If it is true,Execute statement body 1;

    If it is false,Execute statement body 2;

    

be careful: else There is no comparison expression, only if In the back.

Because the second format of the if statement just completed the effect that the ternary operator can complete.

So we thought they could do the same thing.

But are there no differences between them? Definitely not.

difference:

Ternary operators can be implemented by if statements. Otherwise, it does not hold.

When can the if statement implementation not be improved with ternary?

Not when the operation controlled by the if statement is an output statement.

Why? Because the ternary operator is an operator, there should be a result after the operator is operated, not an output.

Format 3 of if statement:

    if(Compare expression 1) {

        Statement body 1;

    }else if(Compare expression 2) {

        Statement body 2;

    }else if(Compare expression 3) {

        Statement body 3;

    }

    ...

    else {

        Statement body n+1;

    }

    

Execution process:

    First, calculate the comparison expression 1 to see if its return value is true still false,

    If it is true,Execute statement body 1, if End of statement.

    If it is false,Then calculate the comparison expression 2 to see if its return value is true still false,

    

    If it is true,Execute statement body 2, if End of statement.

    If it is false,Then calculate the comparison expression 3 to see if its return value is true still false,

    ...

    

    If it's all false,Execute the body of the statement n+1. 

switch statement format:

    switch(expression) {

        case Value 1:

            Statement body 1;

            break;

        case Value 2:

            Statement body 2;

            break;

        ...

        default:

            Statement body n+1;

            break;

    }

Interpretation of format:

    switch:Indicates that this is switch Select structure

    expression:The value of this place is limited

        byte,short,int,char

        JDK5 It can be enumeration in the future

        JDK7 It can be a string later

    case:This is followed by the value to compare with the expression

    Statement body:Code to execute

    break:It means to interrupt and end. It can be controlled switch End of statement.

    default:When all the values do not match the expression, it is executed default Control statement. In fact, it is equivalent to if Declarative else. 



switch Precautions for statement:

    A:case The following can only be constants, not variables, and multiple case The following values cannot have the same value

    B:default Can you omit it?

        It can be omitted, but it is not recommended, because its function is to give a prompt for incorrect situations.

        exceptional case:

            case You can fix the value.

            A,B,C,D

    C:break Can you omit it?

        It can be omitted, but the result may not be what we want.

        There will be a phenomenon: case pierce through.

        Finally, we suggest not to omit

    D:default Must it be at the end?

        No, it can be anywhere. But the suggestion is at the end.

    E:switch End condition of statement

        a:encounter break It's over

        b:The execution ends at the end

Loop statements: for loop, while loop, do... While loop.

for Loop format:

    for(Initialization statement;Judgment conditional statement;Control condition statement) {

        Loop body statement;

    }

    

    Execution process:

        A:Execute initialization statement

        B:Execute judgment condition statement,The return value is true still false

            If it is true,Just continue

            If it is false,Just end the cycle

        C:Execute loop body statement;

        D:Execute control condition statement

        E:go back to B continue.

        

matters needing attention:

    A:Whether the conditional statement is simple or complex, the result is boolean Type.

    B:If a loop statement is a statement, braces can be omitted; If there are multiple statements, braces cannot be omitted. It is recommended never to omit.

    C:Generally speaking: there is no semicolon if there is a left brace, and there is no left brace if there is a semicolon

example:

class ForDemo {

    public static void main(String[] args) {

        //The most primitive approach

        System.out.println("HelloWorld");

        System.out.println("HelloWorld");

        System.out.println("HelloWorld");

        System.out.println("HelloWorld");

        System.out.println("HelloWorld");

        System.out.println("HelloWorld");

        System.out.println("HelloWorld");

        System.out.println("HelloWorld");

        System.out.println("HelloWorld");

        System.out.println("HelloWorld");

        System.out.println("----------");

        

        //This practice is not good, and the repetition of the code is too high.

        //So, we use the cycle to improve

        for(int x=1;x<=10;x++) {

            System.out.println("HelloWorld");

        }

    }

}
  

Basic format of while loop:

    while(Judgment conditional statement) {


      Loop body statement;

    }

    

    Extended format:

    

    Initialization statement;

   while(Judgment conditional statement) {

         Loop body statement;

         Control condition statement;

    }

    

    Through this format, we can see the actual and for The cycle is similar.

    

    for(Initialization statement;Judgment conditional statement;Control condition statement) {

        Loop body statement;

    }

//while statement version

 int x=0;

    while(x<10) {

        System.out.println("HelloWorld");

        x++;

    }

 

 

What is the difference between a while loop and a for loop?

Use difference: if you want to continue to use the variable that controls the condition after the loop ends, use the while loop, otherwise use the for loop. I don't know how to use the for loop.

Because variables disappear from memory as soon as possible, it can improve the efficiency of memory use.

In fact, there is another understanding of the scene:

If it is a range, it is very clear to use the for loop.

If it is not clear how many times to do it, a while loop is more appropriate.

Basic format of do... while loop:

    do {

        Loop body statement;

    }while(Judgment conditional statement);

    

    Extended format;

    Initialization statement;

    do {

        Loop body statement;

        Control condition statement;

    }while(Judgment conditional statement);
class DoWhileDemo {

    public static void main(String[] args) {

        //Output HelloWorld 10 times.

        int x = 0;

        do {

            System.out.println("HelloWorld");

            x++;

        }while(x<10);

        

        System.out.println("--------------");

        

        //Summation 1-100

        int sum = 0;

        int a = 1;

        do {

            sum += a;

            a++;

        }while(a<=100);

        

        System.out.println(sum);

    }

}
Differences between circular statements:

    do...while The loop executes the loop body at least once.

    and for,while The loop must first judge whether the condition is true, and then decide whether to execute the loop body statement.

    

So, what kind of loop do we usually use?

    Priority for,Second, consider while,Final consideration do...while

Note the dead cycle:

    A:Be sure to pay attention to the variable controlled by the control condition statement. Don't lose it, otherwise it will be easy to loop.

    B:Two simplest dead loop formats

        while(true){...}

        for(;;){...}

Loop nesting: the loop body of a loop statement is itself a loop statement. (external circulation or internal circulation)

Control jump statement:

Break: break

Continue: continue
Return: return

Break: break

Usage scenario:

A: In the switch statement

B: In a loop statement.

(if judgment is added to the loop statement)

Note: leaving the above two scenes is meaningless.

How to use it?

A: Jump out of single layer cycle

B: Jump out of multi-layer loop

To achieve this effect, we must know one thing. Tagged statement.

Format:

Tag names: statements

Topics: Java