The difficulty

primary

Learning time

30 minutes

Suits the crowd

Zero basis

Development of language

Java

The development environment

  • JDK v11
  • IntelliJ IDEA v2018.3

1. What are process control statements?

Statements in the source file are usually executed from top to bottom in the order in which they appear. However, flow control statements allow your program to conditionally execute specific blocks of code by breaking up the execution flow using decisions, loops, and branches. There are three types of decision statements (if-then, if-then-else, switch), loop statements (for, while, do-while), and branch statements (break, continue, return).

The first sentence:

Statements in the source file are usually executed from top to bottom in the order in which they appear.

What is a source file?

In Java, a source file is a Java type file.

What is a statement?

A statement that ends with a semicolon. There are four statements above.

What is “execute sequentially from top to bottom”?

Let’s look at some sample code:

The output of this program is:

The output is executed in the same order as the corresponding output statement, from top to bottom.

Then read on:

Process control statements, however, allow your program to conditionally execute specific blocks of code by breaking up the execution process using decisions, loops, and branches.

Among them, the execution sequence of our program can be controlled, that is to say, the original output is “ABCD”, so I can let it only output “AD”, how to do it, it needs to cooperate with the flow control statement. There are three types of process control statements:

Decision statements: if-then, if-then-else, switch

Loop statements: for, while, do-while

Branch statements: break, continue, return

Next, this chapter covers the loop statement for.

2. While, do – the while

We learned while, do-while earlier.

While statement summary:

  • A while loop consists of a loop condition and a loop body
  • The value of a condition expression in a while loop must be of Boolean type only
  • A while loop can be terminated by breaking out of the loop
  • The while loop can end the method by returning its value
  • The while loop can be skipped by continuing
  • A while loop can be infinite by always returning true from the loop condition expression

Summary of the do-while statement:

  • A do-while loop consists of a loop condition and a loop body
  • A do-while loop executes the body of the loop once, regardless of whether the loop condition is true
  • The value of a condition expression in a do-while loop must be of Boolean type only
  • A do-while loop can be terminated by breaking out of the loop
  • A do-while loop can end the loop by terminating the method by returning its value
  • The do-while loop can be skipped by continuing
  • A do-while loop can be infinite by always returning true from the loop condition expression

3.for

For is also called for loop. The for loop consists of four parts. They are initialization expressions, conditional expressions, stepping expressions, and loop bodies.

For loop format:

Let’s do an example to demonstrate for.

Requirements:

Show output “Happy birthday” 10 times + times.

Ideas:

  1. This is done through the for loop.
  2. Initialize the count variable in the expression that defines the number of records.
  3. The conditional expression is count<10.
  4. The step expression is the summation of count.

Implementation:

Results:

3. Initialize expressions that define multiple variables of the same type separated by commas

Features:

The initialization expression in the for loop can define not only one variable, but also many variables of the same type separated by commas.

Validation:

Initialize two variables at once.

Implementation:

Results:

4. The initialization expression may not be written

Features:

The initialization expression inside the for loop can be omitted. But the semicolon has to stay.

Validation:

  1. Do not write the initialization expression inside the for loop.
  2. The count initialization is performed in the main method.

Implementation:

Results:

5. Initialize expressions can write other things

Features:

For loop initialization expressions can write things that are unrelated to initialization.

Validation:

  1. Write a piece of code in the for loop initialization expression that displays the output.

Implementation:

Results:

6. The initialization expression is executed only once

Features:

The for loop initializes the expression only once.

Validation:

  1. Write a piece of code in the for loop initialization expression that displays the output. If the output is displayed only once, the initialization expression is executed only once. If the output is displayed more than once, the initialization expression is not executed only once.

Implementation:

Results:

Conclusion:

From the execution result, the output statement in the initialization expression is executed only once.

7. The conditional expression is executed at the end of the loop body each time

Features:

The for loop condition expression is executed each time after the end of the loop body.

Validation:

  1. In the body of the for loop, set count to a number greater than 10. If the loop ends, the for condition expression is executed at the end of the body; otherwise, the condition expression is executed only once.

Implementation:

Results:

Conclusion:

Judging by the results of the execution,

The first time, count=0; Count >3

The second time, count=1; Count >3

The third time, count=2; Count >3

The fourth time, count=3; Count >3

The fifth time, count=4; The count > 3 meet; The body of the if statement is executed, setting count to 11. If count<10 is not satisfied, return false, and the loop ends.

In summary, a conditional expression is executed each time the body of the loop completes execution.

8. Conditional expression omission is an infinite loop

Features:

For loop condition expression omitted is infinite loop.

Validation:

  1. Empty the for loop condition expression.

Implementation:

Results:

Conclusion:

From the execution result, the count variable keeps accumulating, the loop body keeps being executed, and there is no condition to end the loop, indicating that this is an infinite loop.

Step expressions can write multiple statements

Features:

A for loop stepping expression can write multiple statements.

Validation:

  1. The initialization expression declares two variables
  2. The stepping expression increments or decrements both variables

Implementation:

Results:

Conclusion:

From the running results, the x variable keeps increasing, the Y variable keeps decreasing, and finally the cycle ends normally. Note A stepping expression can write multiple statements simultaneously.

10. Step expressions can be omitted

Features:

Step expressions for loops can be omitted.

Validation:

  1. The initialization expression declares two variables
  2. Inside the body of the loop are operations that increase or decrease both variables simultaneously

Implementation:

Results:

Conclusion:

It can be seen from the running result that x and y increase and decrease in the loop body without affecting the normal execution of the loop. Note Step expressions can be omitted.

Step expressions are executed after the loop body

Features:

Step expressions for loops are executed after the body of the loop completes execution.

Validation:

  1. The initialization expression declares two variables
  2. Inside the body of the loop are operations that increase or decrease both variables simultaneously
  3. Display the output variables in a step expression

Implementation:

Results:

Conclusion:

From the running result, x and y are displayed once in the loop body, then x and y increase and decrease in the loop body, then X and y are displayed again in the step expression, the display output in the step expression is after the loop body, indicating that the step expression is executed after the loop body.

12. Omit all initializer expressions, conditional expressions, and step expressions

Features:

For omit all initializer, condition, and step expressions.

Validation:

  1. Empty initialization expressions, conditional expressions, and step expressions.
  2. Display the output “I am infinite loop” in the loop body.

Implementation:

Results:

Conclusion:

From the running result, it is indeed an infinite loop, so it also verifies that the initialization expression, condition expression, step expression is not written all omit is an infinite loop.

Conclusion:

  • The for loop consists of initialization expression, condition expression, step expression and loop body.
  • The for loop initializes the expression only once.
  • A for loop initialization expression can simultaneously declare multiple variables of the same type.
  • The for loop initialization expression can be omitted.
  • For loop initialization expressions can be written to non-assignment statements.
  • For loop conditional expressions can only accept Boolean types.
  • The first time a for loop conditional expression is executed after the initialization expression.
  • The for loop conditional expression is executed every time before the loop starts.
  • A for loop conditional expression can be omitted through a loop, which is an infinite loop.
  • A step expression for a loop is executed each time after the body of the loop.
  • A for loop can write non-assignment statements inside a stepper expression.
  • Step expressions for loops can be omitted.
  • For loop initializers, conditional expressions, and stepwise expressions can all be omitted, which is an infinite loop.
  • The full execution order of the for loop is that the initialization expression is executed first and only once, the condition expression is executed before the loop starts, the body of the loop is executed after the condition expression, and the step expression is executed after the body of the loop.

This is the end of the loop statement for flow control statement in Java. Please stay tuned for more information.

Attached: Flow control statement table

Answering questions

If you have questions or want to learn more about cutting-edge technology, please leave them in the comments below, and I’ll answer them for you.

The previous chapter

“Full Stack 2019” Java Chapter 26: Loop statement do-while in process control Statements

The next chapter

Full Stack 2019 Java Chapter 28: Array Details

A study group

Join a synchronous learning group for mutual communication and progress.

  • Method 1: Follow the headline number gorhaf, private message “Java study Group”.
  • Method 2: follow the public account gorhaf, reply “Java learning group”.

Full stack engineer learning program

Follow us and join the “Full stack Engineer Learning Program”.

Copyright statement

Original is not easy, shall not be reproduced without permission!