In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
We learn big data technology from scratch, from the foundation of java, to Linux technology, then to the Hadoop, Spark, Storm technology of big data technology, and finally to the construction of big data enterprise platform, step by step, from point to face! I hope Technology Bull can come to guide the study.
In the previous section, you learned about ternary operator / keyboard entry, and this section learns flow control statements.
Before we begin, let's take a look at the exercises in the previous section.
1. Exercises
(1) the keyboard enters three data and obtains the maximum value of the three data. The procedure is as follows:
The execution result of the program is as follows:
(2) enter two data on the keyboard and compare whether the two data are equal. The program code is as follows:
The execution result of the program is as follows:
Second, process control statement
Through our previous study, we should be able to feel that in the process of program execution, the execution order of each statement has a direct impact on the results of the program, that is to say, the flow of the program has a direct impact on the results of the program.
Therefore, we must be clear about the execution process of each statement. Moreover, most of the time we have to control the order of the execution of statements to achieve the functions we want to complete.
Let's talk about the classification of process control statements (there are three main types):
Sequential structure
Select structure
Cyclic structure
The concept of process control statement: you can control the execution flow of the program
1. Sequential structure
Rule: from top to bottom, execute in turn
For example:
Program execution result:
2. Select the structure (also known as the branch structure)
There are two kinds of statements for the selection structure, which are as follows:
If statement
Switch statement
If statement
If statement (in three formats)
Format 1: suitable for individual judgment
Format 2: suitable for making two judgments
Format 3: suitable for making multiple judgments
① if statement format 1
If (comparison expression) {
Sentence style
}
Execute process rules:
First calculate the value of the comparison expression to see whether the return value is true or false.
If it is true, execute the statement aspect
If it is false, the statement aspect is not executed.
Program code example:
Program execution result:
It can be seen that in the if statement, x equals 10 is true, and the sentence body in curly braces can be executed normally.
X equals 20 is false. The statement body in curly braces does not execute, but does not affect the execution of the following statement "over".
Considerations for if statements:
Whether the comparison expression is simple or complex, the result must be of type boolean
If the body of an if statement is a statement, curly braces can be omitted
If it is more than one statement, it cannot be omitted. It is recommended that you never omit it.
Generally speaking, there is no semicolon if there is a left brace, and there is no open brace with a semicolon
The following program code shows that the expressions in the if statement are Boolean
② if statement format 2
If (comparison expression) {
Sentence aspect 1
} else {
Sentence style 2
}
Perform the process:
First of all, calculate the value of the comparison expression to see whether the return value is true or false.
If the form true, execute statement aspect 1
If the form false, execute statement aspect 2
Note: there is no comparative expression after else, only expressions will be added after if.
An example of the program code is as follows:
Program execution result:
Exercise:
Let's do two exercises for the if else sentence:
1. Get the larger values of the two data
2. Judge whether a data is odd or even, and output
The program code is as follows, and I will implement both questions through the following code:
Execution result:
It can be seen that the if statement and the ternary operator can achieve the same effect, but there are also some differences that need to be noted:
Difference:
Ternary operators can be implemented by if statements, and vice versa.
When can the implementation of if statements not be improved with ternary operators?
Not when the operation controlled by the if statement is an output statement.
Because the ternary operator is an operator, there must be a result return, not an output statement.
Case study:
Program execution result:
If you will judge the parity example and use ternary operation to improve it, you can refer to the following program code:
Program execution result:
It can be seen that all the ternary operators we mentioned earlier can be implemented by if statements, and vice versa.
Format 3 of the ③ if statement
If (comparative expression 1) {
Sentence aspect 1
} else if (comparison expression 2) {
Sentence style 2
} else if (comparison expression 3) {
Sentence aspect 3
}
...
Else {
Sentence style nasty 1
}
Perform the process:
First evaluate expression 1 to see whether the return value is true or false
If it is true, execute the body of the statement 1, which ends with the if statement.
If it is false, then evaluate the comparison expression 2 to see if the return value is true or false
If it is true, execute the statement body 2pm if statement to end
If it is false, then calculate the comparison expression 3 to see whether the return value is true or false.
...
If it is false, then execute the body of the statement natively 1.
For example: enter a score on the keyboard, judge and output the grade of the score.
90-100 excellent
80-90. Good.
70-80 good
60-70 pass
0-60 failed
The program code is as follows:
Program execution result:
Note:
When writing a basic test of a program, be sure to consider the following questions:
Correct data
Error data
Boundary data
So for the above data, we can make an improvement, first judge the wrong data first, this will be better, as follows:
Extended exercises (completed by yourself):
1. Get the larger values of the two data
2. Judge whether a data is odd or even, and output
Let's finish this section first, and in the next section we will learn Switch statements.
Previous articles:
Start with big data-Java Foundation from scratch (1)
Learning big data-Java basic Grammar from scratch (2)
Learn big data-Java operator from scratch (3)
Learn big data-Java Basics-Ternary operator / Keyboard entry from scratch (4)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.