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 explains switch and default. Loop and fraction statements are covered in a future article.

2. The break in the case is at the end

In the body of a switch case statement, break must be placed last.

Let’s look at a case in point:

Wrong examples:

The reason why this is an error is because the statement under break never executes and makes no sense.

3. Without break

Let’s start with an example:

Running results:

Let’s get rid of the break in the first case:

Running results:

As you can see, it prints “1 + 1 = 2” and “1 + 1 = 3”. Why doesn’t it print “1 + 1 = 4”?

Let’s look at the execution of the program.

First, calculate the value of the conditional expression:

The value is calculated, and the value is 2, and the first case is matched:

If the match is successful, execute the statement in case, print “1 + 1 = 2”; After printing, the body of the switch statement does not end because there is a case match and the break closing flag has not been encountered. The match has already been successful, so we do not need to verify the case match. We simply execute the body of the case statement:

“1 + 1 = 2” and “1 + 1 = 3” are printed, and then proceed to the end of the switch statement when a break occurs, so only “1 + 1 = 2” and “1 + 1 = 3” are printed.

No break is added for application scenarios

For example, there are four seasons in a year. January, February and March correspond to spring, April, May and June correspond to summer, July, August and September correspond to fall, and October, November and December correspond to winter. How to use switch statement?

As shown above, let’s write the code:

Running results:

It’s too much code, it’s not intuitive, it’s too much repetitive code. Is there a way to simplify? Like this one:

Ok, let’s change it:

Running results:

As you can see, the amount of code is suddenly reduced and the readability is improved. Take full advantage of the switch’s case matching success downward continuation feature, as well as the shorthand feature.

So, everybody, we’ve got a pretty good program here, but I want to ask, what if month=13? Which is the month? The questions will be answered in the next section.

4.default

In the previous section, we successfully completed the month and season mini-program, but at the end, we still have a question: what season is month=13?

Let’s start by reviewing the procedure in the previous section:

Then set month=13:

Then run the result:

It was empty. It would be nice to have a default result. If the user wanted to query for a month that doesn’t exist, I could show him the default result, such as “the month you want to query is not valid.”

Here we go. Here we go. Rewrite the program.

Running results:

Is perfect. Here’s how we did it:

Here is a keyword: default. When matched with the switch, execute the statement in default if there are no successful matches.

5. The default shorthand

The braces in the default statement can be omitted.

Rewrite the program at the end of the previous section:

Running results:

6. Default is the last option

What if we put default on the first line in the switch body?

Let’s try:

Running results:

If something goes wrong, how can there be spring in 13? We need to look at our procedures and see what went wrong.

First, we need to know if default is run first when it is placed on the first line.

To get the answer to this question, we change month to 12 and expect the result to show winter:

Running results:

Winter, to be exact. It also shows that even if the default statement is placed on the first line of the switch statement body, it will wait for all case matches to be matched.

In other words, the default statement is executed last.

The program displays “the month you want to query is illegal” and “spring.” After the program says “spring”, it doesn’t display anything else, that is, it doesn’t execute further. Why doesn’t it execute further? Because break, break ends the body of the switch.

Ok, why is “the month you want to query is invalid” followed by “spring”, because the program continues to execute without ending the switch, just add a break to default.

Let’s try adding break to the default statement:

Running results:

At this time, we look at the result, found only “you want to query the month is illegal”, no “spring”, that is, when the program matches the default, encounter a break statement, directly end the switch, did not continue to execute other case matches.

Advice:

Although the default statement can be placed on the first line in the switch statement body, it is not recommended.

At this point, the Java process control statement in the decision statement switch related content to explain the end, please continue to pay attention to more content.

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 23: Process Control Statements in the decision statement Switch part 1

The next chapter

“Full Stack 2019” Java Chapter 25: The loop statement while in process Control Statements

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!