Java foundation three
1 , selection structure
Selection structure, also known as the branch structure: selecting a specific structure of syntax rules, the specific code to be executed logical operation is judged, the logic operation result of two, is generated selection, executing different code according to different options.
java language offers two choices sentence structure
if;
switch;
Selection structure (if statement)
The if statement in three formats:
The first format:
if (relational expression) {
Statement body;
}
Implementation process: first determine the relationship between the expression to see the result is true or false; if the statement is true on the implementation of the body; if it is false statement is not executed body
The second format:
if (relational expression) {
Statement 1;
}else{
Statement 2;
}
Implementation process: first determine the relationship between the expression to see the result is true or false; if the statement is true on the implementation of 1; if false statement on the implementation of 2
The third form:
if (relational expression 1) {
Statement 1
} Else if (relational expression 2) {
Statement 2
} Else if (relational expression 3) {
Statement 3
}。。。
else {
Body statement n + 1
}
The implementation process: first determine the relationship between the expression 1 if the result is true, the statement is executed 1, the end of the statement. If the expression is false perform 2, if the result is true, the statement is executed 2, the end of the statement, if the execution is false expression 3, if the result is true, the statement is executed 3, the end of the statement, if it is false. . . . . . If not satisfied, execution else, the end of the statement.
Keyboard entry
// guide package
import java.util.Scanner;
// Create Object
Scanner sc = new Scanner(System.in);
Here you can add a prompt
// get input content
int n = sc.nextInt();
Selection structure (switch statement): can be break; interrupted.
switch (expression) {
case value 1:
Statement 1;
case value of 2:
Statement 2;
….
default:
Body statement n + 1;
}
The result of the expression, matching the value of 1, if the same, execute the statement 1, sentence 2, ... body statement n + 1; until the end. If the same value as 2, 2 execute statements, statements ... body, the body statement n + 1, until the end. If you do not match, perform default: statement n + 1 end.
switch (expression) {
case value 1:
Statement 1;
break;
case value of 2:
Statement body 2;
BREAK;
….
default:
Body statement n + 1;
}
If a match value of 1, execute the statement 1, statement 2 ... until it encounters a break, end of statement, if matched to a value of 2, the statement is executed 2, until it encounters a break, end of statement, if not match, perform default, the statement body n + 1, the end of the statement.
2 , the cyclic structure
When the loop may be a case where the loop condition repeatedly execute a section of code, the code is repeated is referred to as loop statement, when this loop is repeatedly performed, it is necessary at the right time cycle is determined to modify the conditions false, thus ending the cycle, otherwise the loop will always execute it, forming an endless loop.
For example: In the console output 10 "HelloWorld".
Loop consisting of
Initialization statement:
One or multiple statements, these statements do some initialization.
Judgment conditional statement:
This is a boolean expression that can decide whether to execute the loop body.
Loop statement:
This is part of the loop statement, that is what we want to do many times.
Control conditional statement:
This section after the end of the first loop, line. By cycling conditions used to control variables, such that the end of the cycle at the right time. Analyzing next cycle executed before the conditional execution.
3 , loop structure
for loop
for (initialization statement; Analyzing conditional statement; control conditional statement) {
Loop statement;
}
Implementation process:
A: perform the initialization statement
B: performing determination conditional statements, see the result is true or false
If the end of the false, loop
If true, continue.
C: execute the loop body statement
D: execution control conditional statements
E: B back to continue.
while loop
The basic format
while (judgment condition statement) {
Loop body;
}
Execution flow: A: determining whether the conditional statement is true,
If true, the loop body is executed, performs A turn
If false, the loop is terminated
Extended format
Initialization statement;
while (judgment condition statement) {
Loop body;
Control conditional statement;
}
for contrast and while
and while it can do for equivalent conversion.
Use difference: conditional statements control the variables controlled, at the end of the for loop, you can not be accessed, while the end of the while loop can continue to use, if you want to continue using it with a while, or recommended for. The reason is that for the end of the cycle, the variable will disappear from memory, can improve memory usage efficiency.
Scene difference:
The for loop operation with respect to a range suitable for Analyzing
for while loop determines the number of operations is not clear
do ... while loop
The basic format:
do{
Loop body;
} while (judgment condition statement) ;
Extended format:
Initialization statement;
do{
Loop body;
Control conditional statements
} While (loop condition statements);
Implementation process:
First iteration of the loop; execution determination condition, if the condition is true, execution cycle thereof, or the end of the cycle