[Road to Final Review] JAVA (3) A

This issue is about loop architecture, if-else, for loop, while loop, etc. Please appreciate it carefully

1. Process control statement

two order structure

Three-branch structure if-else

Four switch-case selection structure

Penetration of five cases

Six if-else statement and switch-case statement comparison

Seven summary


foreword

Thank you for your company all the time. Our first two chapters have been updated. This is the third part of my update, which is about flow control statements. Then my next part, which is the future plan, will update the array for object etc...


提示:以下是本篇文章正文内容,下面案例可供参考

1. Process control statement

        The flow control statement is a statement used to control the execution order of each statement. The statement can be combined into a small logic module that can complete a certain function. It is a small part of the area. Three flow structures are specified in our program: sequential structure , branch structure, loop structure.

two order structure

        The sequential structure is executed line by line from top to bottom, each step, the modification of a variable in the previous line will have an impact on the next line

public class tast{
    public static void main(String[] args){
        int x = 1;
        int y = 2;
        System.out.println("x = " + x);
        System.out.println("y = " + y);
            x++;
            y = 2 * x + y;
            x = x * 10;
        System.out.println("x = " + x);
        System.out.println("y = " + y);
}
}

Java defines variables using forward references. After the previous relationship, the variables cannot be defined until the usage cannot be completed.

Three-branch structure if-else

The format of the if-else statement: if (conditional expression) {

                                        statement block;

                                                }

The conditional expression here is of boolean type. In the execution process, first judge whether the result of the conditional expression is true or false, if it is true, execute the statement, if it is false, end the execution statement.

The format of the double-branch conditional judgment of the if-else statement: if (conditional expression) {

                                                                                   Statement 1;

                                                                         }else{

                                                                                   statement 2;

                                                                              }

If-else statement multi-branch condition judgment:

                                                                       if(condition_expression) {

                                                                                   Statement 1;

                                                                         }else if(condition_expression2){

                                                                                   statement 2;

                                                                                ...........

                                                                        }else if(condition_expression n){

                                                                                   statement n;   

                                                                                }else {

                                                                                   statement n+1;

                                                                                     }

Once the expression is true, the corresponding statement block will be executed. After the corresponding statement block is executed, it will jump out of the current structure. If 1 is true, it will exit after execution and will not be executed later.

if...else nesting: In the if statement block, or in the else statement block, another conditional judgment is included, and the result is a nested structure. If it is nested in the if statement, when only If the external if condition is satisfied, the internal condition will be judged. If it is nested in the else statement, only when the external if condition is not satisfied, the internal condition will be judged after entering else

Four switch-case selection structure

switch-case statement structure:

                switvh(expression) {

                                case constant value 1:

                                        statement block 1;

                                case constant value 2:

                                        statement block 2;

                        [default :

                                statement block n+1;

                                break;

                        ]

                                        }

If a break is encountered, jump out of the current switch-case statement, the case is penetrating, until the break keyword is encountered or all the execution statements of the case and default are executed, and the current structure is jumped out

The value in the switch expression must be the following: byte, short, int, char, enumeration, String

The value in the case clause must be a constant, and cannot be a variable name or an uncertain expression value or range. In the same switch statement, the constant values ​​of all the case clauses are different from each other.

The break statement is used to make the program jump out of the switch statement block after executing a case branch. If there is no break, the program will execute sequentially to the end of the switch

Penetration of five cases

In the switch statement, if break is not written later, there will be a penetration phenomenon. Once it succeeds, it will run backward until it encounters a break or the end of the switch statement.

Six if-else statement and switch-case statement comparison

in conclusion:

  • Anything that can use a switch-case statement can be transformed into an if-case structure, and vice versa, it is not true
  • If you can use both switch-case statement and if-case statement, generally use if-case statement, because if-case statement is more efficient
  • The advantage of the if-case statement: its statement condition is a Boolean value, if the conditional expression is true, it will enter the branch, which can be used for range judgment or equivalent judgment, making the judgment range wider , and the condition of switch is a constant value, it can only judge whether the result of a variable or expression is equal to a constant value, only relatively narrow
  • Advantages of the switch statement: When the condition is to judge whether a variable or expression is equal to a fixed constant value, both if and switch can be used, and more people are accustomed to using switch because of high efficiency. When the condition is judged by an interval range time, only the if statement can be used
  • Using switch can use penetration, execute multiple branches at the same time, but if....else has no penetration

When the keyboard enters an integer to determine whether it is a positive number, a negative number or zero, only the if-case statement can be used

Seven summary

These are all the sequence structures. In fact, these two are generally speaking. We will update the loop structure tomorrow, and there will be more of them. Overall, it feels good. I hope everyone will pay more attention, so that we can become better together. Come on everyone, I hope you will continue to pay attention, JAVA (3) Part B should be updated soon, so stay tuned

Guess you like

Origin blog.csdn.net/m0_69520030/article/details/130571783