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, if-then-else is explained in this chapter. Loop and fraction statements are covered in a future article.

2.if-else

Is there a decision statement that says if condition 1 is met, but condition 2 is not met? The answer is yes, so let’s talk about if-then-else.

If-then-else If an if condition is not met, the else block is executed.

Take a life example: if it’s sunny tomorrow, I’ll go shopping, otherwise I’ll go to the movies.

To simplify, use if-then-otherwise.

And just to program it a little bit more, say if-then-else.

Next, we use code to illustrate:

We change the variable name of “sunny tomorrow” to “weather”, true if sunny, false otherwise:

Let’s translate this program briefly:

Let’s run this program:

3. If – else shorthand

If -else shorthand, if statement just follows one of its own statements, and else statement just follows one of its own statements.

For example: if it’s sunny tomorrow, go to the movies, otherwise.

Code representation:

Short form:

The if-else shorthand is to get rid of the curly braces.

Let’s test if and else blocks with multiple statements:

Running results:

Alas, error, take a look at error:

Error:(11, 9) Java: ‘if’, but no ‘else’

What does “if, but no else” mean? There are both:

Note that, as we said earlier, if and else only follow their first statement, which looks like this:

So if and the first statement that follows it are the same thing, and else and the first statement that follows it are the same thing:

From the if-else abbreviation, we know:

This is the correct way to write it, which is that we do have an else after the if statement, but instead of an else statement, we have “system.out.println (” window shopping “);” :

Error:(11, 9) Java: ‘if’, but no ‘else’

Suggestion: Please do not omit the curly braces.

4. If – else nesting

First let’s look at the if-else nested form:

The ellipsis means you can nest more.

Note: Please try not to nest if-else.

Draw a red card and a blue card, draw the card first, then draw the numbers, only numbers 1 and 2, and the winning combination is as follows:

Red +1 corresponds to notebooks;

Red +2 corresponds to the mouse;

Blue +1 corresponds to a mobile phone;

Blue +2 corresponds to a keyboard.

So, rewrite it as if-otherwise:

Because there’s only red and blue, so you don’t get red, so you’re left with blue. Ok, let’s rewrite it to if-else form:

Let’s rewrite the inside as well:

Change the inside to if-else form, because you drew either 1 or 2, so when you didn’t draw 1, there was only 2 left:

Finally, our code shows:

Running results:

You can change the color and number of the card to see different results.

5.if-else if

If (= if), else if (= if), else if (= if)

If the condition is not satisfied, we go down to else if if the condition is not satisfied, if it is satisfied we execute the else if statement; If no, the command is not executed.

Take an example, lucky draw, there are red, blue cards, red notebook, blue keyboard.

Change to if otherwise – if form:

Change to if-else-if form:

Code representation:

Running results:

6. If the else – if shorthand

If else-if short form:

Similarly, if just follows the first statement after it; Else if just follows the first statement after it;

Take an example, lucky draw, there are red, blue cards, red notebook, blue keyboard.

Change to if otherwise – if form:

Change to if-else-if form:

Code representation:

Short form:

Running results:

Suggestion: Don’t omit curly braces in your development!

7. If the else – if nested

If else-if nested form:

The ellipsis indicates that many layers can be nested.

Note: it is best not to nest a layer, if not nesting!

If else else nesting example: again, draw a red card and a blue card, draw the card first, draw the numbers, numbers 1 and 2, the winning combination:

Red +1 corresponds to notebooks;

Red +2 corresponds to the mouse;

Blue +1 corresponds to a mobile phone;

Blue +2 corresponds to a keyboard.

Use the “if else-if” form:

Code representation:

Running results:

8.if else-if else

If else-if else

When condition 1 is met, statement block 1 is executed. When condition 2 is met, statement block 2 is executed. Otherwise, block 3 is executed.

To take an example in daily life, an exam score of 90 or above is A, an exam score of 80 or above is B, A test score of 70 or above is C, and A test score of less than 70 is D:

If statement form:

If else-if form:

We use the if else-if else form again:

Code representation:

Running results:

9. If else-if else

If else-if

Modify the original program of the above fraction example with the if else-if else abbreviation.

First, let’s list the source of the score example:

If else-if else:

Running results:

Suggestion: Don’t omit curly braces in your development!

10. If else-if else nesting

If else-if else nested form:

Doesn’t everyone seem to feel very confused, not to mention multiple layers of nesting.

Strongly recommended: If else-if else nesting is not allowed in development!! Very bad for team development. This kind of code is very bad to write.

If else-if else nesting example is not necessary to enumerate, it is not readable.

11. List common if statement forms

  • If statement
  • If the else statement
  • If the else – if statements
  • If else-if else statement

The if-else decision statement in the control flow statement in Java is now finished. 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 21: Decision statement if in Process Control Statements

The next chapter

“Full Stack 2019” Java Chapter 23: Process Control Statements in the decision statement Switch part 1

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!