Flow control is a very important learning stage for any language, it provides the basic means of the overall operation process of the program. It can also be said that flow control is an important part of making the computer move.

Now let’s talk about what we need to know about process control.

Control statements:

“Sequential structure” represents the logic of “execute a first, then b”. For example, find a girlfriend first and then call her; Be betrothed before married;

“Conditional structure” stands for “if… , then…” The logic. For example, if your girlfriend calls, answer the phone quickly. If you see a red light, stop.

“Loop structure” stands for “if… , repeat…” The logic. For example, if you don’t get through to your girlfriend, try again. If you don’t find someone you like, keep looking.

Before we talk about Java control statements, you can watch the course to help yourself understand the Java control process more easily:

Java300 sets zero foundation for beginners video tutorial _Java300 sets zero foundation tutorial _Java beginner beginner video foundation consolidation tutorial _Java language entry to master JavaSE basic tutorial – only for beginners and written _ high qi Java300 sets /Java zero foundation /Java /Java entry to master /Java programming /JavaSE/Java foundation JavaSE basic system combat course _Java flow control and operator operation combat _ loop operation control /JavaSE course/data type/operator/scanner /JavaSE introductory combat tutorial Java combat tutorial _Java zero basic/online education /Java entry to master /Java programming /Java Zero basic Java beginner beginner

Before we know the selection structure of Java, we always start from the entry of the program and execute each statement in sequence until the end of the last statement. But in life, we often need to make conditional judgment, and decide whether to do a thing according to the judgment result, which requires the selection structure.

Selection structures are used to determine given conditions and then to control the flow of the program based on the results of those judgments. The main conditional structures are: if structure and switch structure. And if structure can be divided into if single branch structure, if-else double branch structure, if-else if-else multi-branch structure.

First: Choose the structure

1, if single branch structure

The if selection structure is a programming flow control composed of conditional Boolean expressions and statement series (code blocks).

Conditional expression 1 to conditional expression n: Required parameters. There can be multiple expressions, but the result must be of type Boolean (true, false).

Statement sequence: Can be one or more statements. When conditional expression 1 is true, statement sequence 1 is executed. When conditional expression 2 is true, statement sequence 2 is executed; And so on.

Form 1: if (expression) {block 1; }

The if statement evaluates the Boolean expression once. If true, the block in {} is executed; otherwise, the block is skipped.

Example:

int age = 20; If (age > 18) {String name = "Tom"; System.out.println(" my name "+" my name "+" my name "+" my name "+" my name "+" my name ") ); }Copy the code

2, if-else double branch structure

Form 2:

If (expression) {

Block 1;

} else {

The code block 2;

}

When the Boolean expression is true, block 1 is executed; otherwise, block 2 is executed. That’s the else part.

Example:

int age = 16; If (age > 18) {String name = "Tom"; System.out.println(" my name "+" my name "+" my name "+" my name "+" my name "+" my name ") ); } else {system.out.println (" I'm underage! ") ); }Copy the code

If -else if-else multi-branch structure

When Boolean expression 1 is true, block 1 is executed; Otherwise, Boolean expression 2 is determined, and block 2 is executed when Boolean expression 2 is true; Otherwise, continue to judge Boolean expression 3···; If all 1 to N Boolean expressions are false, the n+1 statement block is executed, which is the else part.

In the form of three:

If (expression) {block 1; } else if (expression) {block 2; } else {block 2; }Copy the code

Example:

If (age >= 0 && age <= 10) {system.out.println (" boy "); } else if (age <= 18) {system.out.println (" teen "); } else if (age <= 30) {system.out.println (" young "); } else if (age <= 50) {system.out.println (" middle age "); } else {system.out.println (" old "); }Copy the code

4, nested if selection structure

Execution process: When the outer condition is satisfied, then judge the inner condition

Note: One selection structure can be nested within another selection structure (as long as the nesting format is correct, any combination is supported)

If (outer expression) {if (inner expression) {inner block 1}else{inner block 2}}else{outer block}Copy the code

5, switch multiple selection structure

The SWtich statement consists of a control expression and multiple case tag blocks.

The switch statement is executed from the matching case label based on the value of the expression until the end of the break statement or the switch statement. If the value of the expression does not match any of the case values, the default statement is entered (if any exists).

Grammatical structure:

Switch (expression) {case value 1: statement sequence 1; [break]; Case value 2: statement sequence 2; [break]; ... ... ... ... ... [default: default statement;] }Copy the code

Note that you can use if-else if-else multi-branching or switch when the Boolean expression is equivalent, and only if-else if-else multi-branching when the Boolean expression is interval.

Example:

Public class TestSwitch1{public static void main(String [] args){int score = (int)(math.random ()*81)+20; // Char grade; switch(score/10){ case10 : case9 :grade = 'A'; break; case8 :grade = 'B'; break; case7 :grade = 'C'; break; case6 :grade = 'D'; break; default :grade = 'E'; } system.out. println(" score: "+score+", grade: "+grade); }}Copy the code

Second: cyclic structure

Repeating an action many times simplifies your code by using loops

1, while loop structure

At the beginning of the loop, the Boolean expression is evaluated once, and if the condition is true, the body of the loop is executed. For each subsequent additional loop, it will be recalculated before it starts.

There should be statements that make the loop tend to end, otherwise there will be an infinite loop — a “dead” loop.

Grammatical structure:

While (Boolean expression) {body of loop; }

Example:

public class TestLoop3 { public static void main(String[] args) { int i = 1; int sum = 0; / / 1 + 2 + 3 +... + 100 =? while (i <= 100) { sum += i; Sum = sum+ I; i++; } System.out.println("sum= " + sum); }}Copy the code

2. Do-while loop structure

The do-while loop executes the body of the loop first, then determines the value of the Boolean expression. If the condition is true, the body is executed, and when the condition is false, the loop ends. The body of the do-while loop is executed at least once.

Grammatical structure:

Do {circular body; } while(Boolean expression);

Example:

public class TestLoop4 { public static void main(String[] args) { int i = 0; int sum = 0; do { sum += i; i++; } while (i <= 100); // here; System.out.println("sum= "+ sum); i = 1; sum = 0; do{ sum += i; i+=2; }while(i<100); System.out.println("sum= " + sum); }}Copy the code

3, for loop

The for loop is a generic construct that supports iteration and is the most efficient and flexible of the loop constructs. The for loop is initialized before the first iteration, that is, the initial expression is executed; The Boolean expression is then judged. If the result is true, the body of the loop is executed. Otherwise, the loop is terminated. Finally, at each iteration, some form of “stepping” is performed, that is, the iteration factor is performed.

  • The initialization section sets the initial value of the loop variable
  • The conditional judgment part is arbitrary Boolean expression
  • The iteration factor controls the increase or decrease of cyclic variables

The for loop executes the body of the loop first and then the step after the condition is determined.

Grammatical structure:

For (initial expression; Boolean expression; Iteration factor) {loop body; }

Example:

Public class TestLoop5 {public static void main(String[] args) {// Calculate int sum; int i; for (i = 1, sum = 0; i <= 100; i++) { sum += i; } System.out.println("1+2+3.... +100=" + sum); i = 1; sum =0 ; for (; ;) { if (i > 100) { break; } sum += i; i++; } System.out.println("1+2+3.... +100=" + sum); sum = 0; for (int j = 1; j <= 100; j++) { sum += j; } System.out.println("1+2+3.... +100=" + sum); }}Copy the code

Third: loop control statements

1, break statement

At the body of any loop, you can use a break to control the flow of the loop. Break is used to forcibly exit the loop without executing the remaining statements in the loop.

Example:

public class TestBreak{ public static void main(String [] args){ int sum = 0; int i; for(i=1; i<=100; i++){ sum +=i; System.out.println("i="+i+",sum="+sum); if(sum>=3000){ //break; return; } } System.out.println("i="+i+",sum="+sum); //10177 not 78 } }Copy the code

When I =77, sum>=3000, then break, which means that the next 23 times will not be executed.

Break statements can appear in loops and switch statements

The return statement terminates the current method without any relation to the loop. Each method will end with a return

2, the continue

The continue statement is used in the body of a loop to terminate the process by skipping the statements that have not yet been executed in the body of the loop and then deciding whether to execute the loop next time.

Example:

public class TestContinue { public static void main(String[] args) { for (int i = 1; i <= 100; i++) { /* if(i%5==0){ System.out.print(i+"\t"); } */ if (i % 5 ! = 0) { continue; } System.out.print(i + "\t"); }}}Copy the code

Continue is used to loop 100 times. When I =1,2,3,4, because I %5! =0, no subsequent statements are executed; When I =5, because I %5==0, execute the statement following the body of the loop

Learning goals

  1. Learn the difference and usage of if and switch statements

  2. Master the use of break in switch and the default statement

  3. Master the writing format and execution order of the three loops

  4. Learn the break and continue keywords, and understand the function of return

  5. Master the use of multiple cycles, master some common algorithms in process control

  6. Understand infinite loops and unreachable statements

  7. Master method declaration and use, as well as memory analysis when calling methods

  8. Master method overloading and usage scenarios


The above is the Process control of Java language, very basic and important knowledge, I hope I can like ~~~

Dry goods article:Dimple: Java learning circuit – Giant super full full super full super full super full