Review of If selection Structure

Source: Internet
Author: User

 

? What is the If selection structure?
If the selected structure is determined based on conditions and then processed
Syntax:
If (condition) {// The result must be a Boolean value to determine the true and false. The result is "true" and the code under if is executed quickly, the result is "false" and the code block under the IF structure is not executed.
// Code block
}
? Logical operators
Operator expression description
& Condition 1 & condition 2. If both conditions are true, the result is true.
| Condition 1 | condition 2 returns true if either of the two conditions is true.
!! If the condition is true, the result is false.
If the condition is false, the result is true.

3. Generate a random number (0 ~ 9) The method is as follows:
Int random = (INT) (math. Random () * 10 );
// Int in parentheses indicates that this number is forcibly converted to the integer type.
// * 10 indicates that the random number range is 0-9;
// Note: You can get 0 to get less than 10
Random random = new random ();
Int ran = random. nextint (899) + 100;

4. The If selection structure in Java, in addition to the first form, also includes the following form
If-else select structure: it can handle simple condition branches
For example, John scored 10 points for the test. If 60 points are correct and the judgment condition is false, the code block in else will be executed.
Syntax:
If (condition ){
// Code block 1
} Else {
// Code block 2
}


Multi-If selection structure: it can handle condition branches of segments
In case of interval judgment, when a single if structure cannot solve the judgment, multiple if structures are used when multiple if structures are difficult to select.
For example, evaluate the final test scores of Students
Score> = 80: Good
Score> = 60: Medium
Score <60: Poor
In this case, we need to use three
Syntax:
If (score> = 80 ){
// Code block 1
} Else if (score> = 60 ){
// Code block 2
} Else {
// Code block 3
}


Note:
The judgment order of multiple if selection structures is very important. If the current if condition is met, the next if statement is not executed, in the future, students should control the execution sequence when selecting multiple if-based structures.

Nested If selection structure: it can handle complicated condition branches
When a judgment result still needs to be judged again before processing, the nested if structure can be used to process complex judgment conditions.
For example, Michael is admitted to the university, but according to his actual score, he can go to Tsinghua University or Peking University.
Syntax:
If (condition 1) {// whether the student is admitted to the University
If (condition 2) {// can you go to Peking University?
Code Block 1
} Else {// otherwise, Tsinghua University
Code Block 2
}
} Else {// not admitted to university
Code block 3
}


Chapter 4
Review of switch selection Structure
1. Switch selection Structure
Syntax:
Switch (expression ){
Case constant 1:
Statement;
Break;
Case constant 2:
Statement;
Break;
...

Default:
Statement;
Break;
}


Features:
1. It is more concise than multiple if selection conditions and reduces the amount of code.
2. The condition is equivalent judgment.
3. The condition type can only be char or Int.
4. The default block sequence can be changed, but pay attention to the execution sequence.
Generally, the default block is placed at the end, and can be omitted.
2. Comparison with multiple if selection Structures
Similarities:
Is used to process the structure of multiple Branch Conditions
Differences:
Switch selection Structure
Only the equivalence condition judgment can be processed, and the condition must be an integer or balanced variable.
Multi-If selection Structure
There is no limit on the switch selection structure, which is especially suitable when a variable is in a continuous interval.

3. Note!
Use of the break statement:
If break is not used in each case, it will continue until it encounters break.
So remember to add break at the end of each case.
When using if {} to select a structure, if there is only one statement in the curly brackets, You can omit the curly brackets.

 

4. Determine whether the characters entered by the user are valid
If (input. hasnextint () = true ){
} Else {
}
Increase Program robustness by determining whether the program is valid

Actual use of the selected Structure
Case 1

If select structure usage:
// Zhang San is going to take the university test. If 500 is admitted to the university
Int score = 600;
If (score> 500) {// determines whether the score is greater than 500 in the condition.
System. Out. println ("Congratulations on going to college ");
}}


Case 2
Use of the IF-else selection structure:
// Zhang San is going to take the university test. If 500 is admitted to the university
Int score = 400;
If (score> 500) {// determines whether the score is greater than 500 in the condition.
System. Out. println ("Congratulations on going to college ");
} Else {// execute the code in else when the result is false.
System. Out. println ("not admitted ");
}

 

Case 3
Use of multiple if selection structures:
// Zhang San is going to take the university test. If 500 is admitted to the university
// More than 400 are divided into junior colleges
// Otherwise, go to the construction site
Int score = 300;
If (score> 500) {// determines whether the score is greater than 500 in the condition.
System. Out. println ("Congratulations on going to college ");
} Elseif (score> 400) {// College Level
System. Out. println ("going to college ");
} Else {
System. Out. println ("work on site ");
}

Case 4
Use of nested If selection structure:
// Zhang San is going to take an exam at a university. If 500 is admitted to a university, he does not take an exam at a construction site.
// More than 550 go to Tsinghua University
// Otherwise, go to Fudan University
Int score = 560;
If (score> 500) {// determines whether the score is greater than 500 in the condition.
If (score> 550 ){
System. Out. println ("going to Tsinghua University ");
} Else {
System. Out. println ("go to Fudan University ");
}
} Else {
System. Out. println ("work on site ");
}

 

 


Use Case nested If selection structure:
// Zhang San wants to take an exam at a university. Assume that 500 of the students are admitted to the university,
// Score 600 to Tsinghua University
// 700 points for free at Harvard
// Did not go to work at the construction site
Int score = 600;
Int BL = score/100; // The rounded up number is the score range.
Switch (BL ){
Case 7:
System. Out. println ("Free Harvard ");
Break;
Case 6:
System. Out. println ("going to Tsinghua University ");
Break;
Case 5:
System. Out. println ("upper limit ");
Break;

Default: // other figures are cheating or not admitted to college.
System. Out. println ("go to the construction site to make a lot of money ");
Break;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.