if (conditional expression) {
statements thereof;
}
-
First, determine the conditional expressions see the result is true or false
-
If the statement is true on the implementation of the body
-
If false statement is not executed somatic
if (relational expression) {
statement body. 1;
} {the else
sentence 2;
}
if (judgment condition 1) {
execute statement 1;
} the else if (judgment condition 2) {
execute statement 2;
}
...
} the else if (n-determination condition) {
execute statement n-;
} {the else
executing statements n + 1;
}
If the statement in the block, or else statement is a block,
Execution features:
(1) If the nested if statements in the block only if the condition is satisfied when the external, to judge the condition inside only
(2) if it is nested within the else block only when the external conditions are not met if, after entering else, only to judge the condition inside
if()
else // When the statement is executed only one article, we can omit the curly braces
switch (expression) {
case 1 constant value:
a block of statements 1;
{break;}
case constant value 2:
a block of statements 2;
{break;}
. . .
[Default:
statement block n + 1;
{break;}
}
(1) Entry
① When a constant value and the value of the latter case switch (expression) match, the process proceeds from the Case;
② constant value does not match all the values following the case when the switch (expression), finding default branch to enter; no matter where default
(2) Once the "entry" into the Switch, execution continues downward until it encounters the "export" may occur through
(3) exports
② interrupt exports: encountered a break, etc.
(1) the value of the type of switch (expression), can only be: four basic data types (byte, short, int, char), two kinds of reference data types (enumeration after JDK1.5, after JDK1.7 String )
(2) the rear case must be a constant value, and can not be repeated
for (initialization statement ①; ② loop conditional statement; iteration statement ④) {
loop statement ③
}
for (;;) {
loop statement block; // if there is no statement in the loop out of the loop body, it is an endless loop
}
(2) The cycling conditions must be a boolean
(3) If the loop condition statement ② omitted, then it defaults to cycling conditions are satisfied
Implementation process:
-
Step 1: Execute initialization statement ①, to complete the loop variable initialization;
-
The second step: the implementation of conditional statements cycle ②, look at the value of the loop condition statement is true, or false;
-
If true, the implementation of the third step;
-
-
-
The third step: execution loop statement ③
-
Step four: Perform iterative statements ④, re-assignment for the loop variable
-
Step 5: The new value of the loop variable re-start from the second step and then execute it again
while (loop condition statement ①) {
loop statement ②;
}
while (to true) {
loop statement; // if this time is not circulating loop statement out, but also endless loop
}
while (loop condition) and cycling conditions must be a boolean
Implementation process:
-
Step 1: Execute cycle conditional statements ①, see the value of circulating conditional statement is true, or false;
-
If true, the second step;
-
If it is false, loop aborted cycle is not executed.
-
-
-
The third step: after the implementation of the loop statement, then start again from the first step to perform again
2, while the extended loop format:
Initialization statement ①;
the while (loop condition statement ②) {
loop statement ③;
iteration statement ④;
}
Implementation process:
-
Step 1: Execute initialization statement ①, to complete the loop variable initialization;
-
The second step: the implementation of conditional statements cycle ②, look at the value of the loop condition statement is true, or false;
-
If true, the implementation of the third step;
-
If it is false, loop aborted cycle is not executed.
-
-
The third step: execution loop statement ③
-
Step four: Perform iterative statements ④, re-assignment for the loop variable
-
Step 5: The new value of the loop variable re-start from the second step and then execute it again
do {
loop statement ①;
} the while (loop condition statement ②);
(2) do {} while (); Finally there is a semicolon
(3) do ... while loop sentence structure that will execute at least once, and for this and while is not the same
-
The first step: the implementation of the loop statement ①;
-
The second step: the implementation of conditional statements cycle ②, look at the value of the loop condition statement is true, or false;
-
If true, the implementation of the third step;
-
If it is false, loop terminates, the cycle is not executed.
-
-
The third step: conditional statements after the implementation cycle, and then start again from the first step to perform again
2, do ... while loop Extended format:
Initialization statement ①
do {
loop statement ②;
iteration statement ③;
} the while (loop condition statement ④);
-
Step 1: Execute initialization statement ①, to complete the loop variable initialization;
-
The second step: the implementation of the loop statement ②;
-
The third step: the implementation of the iteration statement ③, reassigned for the loop variable;
-
Step four: Perform loop conditional statements ④, look at the value of the loop condition statement is true, or false;
-
If it is true, according to the new value of the loop variable, and then start again from the second step to perform again;
-
If it is false, loop aborted cycle is not executed.
-
-
do ... while loop executes at least once a loop statement
-
for while loops and conditional statements first loop is established, and then decide whether to execute the loop body is executed at least zero loop statement
-
-
From the perspective of the life cycle of the loop variable analysis
-
for loop the loop variable () declared in for, at the end of the loop, can not be accessed;
-
while and do ... while loop the loop variable declared as outside, so while and do ... while the end may continue to be used;
-
-
how to choose
-
Traversing significant demand cycles (range), selected for loop
-
Traversal no demand significant number of cycles (range), the cycle while loop
-
If the loop body block of statements executed at least once, consider do ... while loop
-
In essence: between the three cycles can be converted to each other, can realize the function of circulation
-
-
Three loop structure has four elements:
-
Initialization expression (1) of the loop variable
-
(2) The cycling conditions
-
Modified iterative expressions (3) loop variable
-
(4) loop statement block
for (initialization statement ①; loop condition statement ②; iteration statement ⑦) {
for (initialization statement ③; ④ conditional statement cycle; iteration statement ⑥) {
loop statement ⑤;
}
}
for (;;) // When only one block of statements, or if, when the for statement {} may be omitted, also known as compound statement. But not recommended
Statement block;