Control statements

The presence of control statements makes our program logical/organized, and we can use control statements to implement a “business”.

How many types of control statements are there?

Three categories:

  • Select statement
  • Looping statements
  • Goto statement

Select statements can also be called branch statements

  • If statement
  • A switch statement

Loop: The main loop executes a specific block of code repeatedly

  • The for loop
  • The while loop
  • do.. while.. cycle

Goto statement

  • break
  • continue
  • Return (I don’t need to learn this for now, but I’ll use it later when WE talk about methods)

Select statement/branch statement if

Four ways to write it.

Syntax mechanism:

If (Boolean expression){} if(Boolean expression){}else{}
If (Boolean expression){}else if(Boolean expression){}else if(Boolean expression){}else if(Boolean expression){}else if(Boolean expression){}else if(Boolean expression){}
If (Boolean expression){}else if(Boolean expression){}else{} if(Boolean expression){}else{}else{}
Nested if statements: if(Boolean expression){// Prerequisite if(Boolean expression){}else{}}else{}

Implementation principle:

For an if statement, as long as one branch executes, the entire if statement ends.

The branch executes only when the result of the Boolean expression is true.

There is only one Java statement in the branch, and the braces can be omitted.

Something with an else is guaranteed to have a branch.

Select statement switch

Complete grammatical structure:

Switch (value){// Value can be String, int, (byte,short,char can automatically convert int); break;
Case value 2: Java statement; break;
Case value 3: Java statement; break;
Default: Java statement; }

About loop Statements

  • The for loop
  • The while loop
  • do.. The while loop

What is a loop statement and why do you use it?

Because in the real world, a lot of things are done over and over.

In the corresponding program, if a piece of code needs to be executed repeatedly, in order to reduce the amount of code, we use a loop statement.

The for loop

For loop syntax mechanism:

For (initialize the expression; Conditional expression; Update expression){body of loop; } for(int i = 0; i < 10; i++){ System.out.println(i); }

The for loop executes as follows:

1. Execute the initialization expression first and only once. 3. If true, the body of the loop is executed. 4. After the body of the loop ends, execute the update expression. 5, continue to determine the condition, if the condition is true, continue the loop. 6, until the condition is false, the loop ends.

The while loop

While (Boolean expression){body of loop; }

Execution times: 0 to N times.

do.. The while loop

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

Execution times: 1 to N times

About the turn statement:

  • break;
  • continue;
  • return; We’ll learn more later in the return statement when we talk about methods. Never mind for now.)

break;

By default, it terminates the loop closest to it.

Of course, you can also terminate the specified loop by means of an identifier.

for(int i = 0; i < 10; i++){ if(i == 5){ break; } code1; code2; code3; code4; . }

continue;

Terminate the current “this time” loop and jump directly into the next loop to continue execution.

for(int i = 0; i < 10; i++){ if(i == 5){ continue; } code1; code2; code3; code4; . }

The last

Recommended to you a more detailed Java zero basic tutorial, the following is I have seen feel pretty good, worth watching the collection. To share it with you, click here

https://www.bilibili.com/vide…

If it helped you, thank you for your support