8. Statement
(1) The flow control statements
Classification: sequential structure, selection structure, a cyclic structure
① sequential structure: from top to bottom, are sequentially performed
② Select structure:. IF statements and switch statements
if statement: Format 1: if (Comparative expression) { statement body; } Process: judgment comparison value of the expression, true body statement is executed, false not performed Format 2: if (Comparative Expression 1) { statement 1; } the else { statement 2; } process: determination value comparison expression 1, true statement is executed body 1, false statement 2 is executed format. 3: IF (Comparative expression 1) { statement body 1; } the else IF ( Comparative expression 2) { statement body 2; } ... the else { statement body n-; } procedure: determination value comparison expression 1, true statement 1 is executed, if the end of the statement, to false if the comparison value of the expression 2 , true statement 2 is executed, the end of the if statement, false is executed statement thereof n
NOTE: The difference between the if statement and the ternary operator:
First, three can be achieved, if the statement can be achieved, not vice versa
Secondly, ternary an operator, after the implementation of which would certainly produce a result, can not produce an output statement; if statement may be implemented to generate an output statement
switch statement format: switch (expression) { Case Value 1: Statement body 1; BREAK ; Case Value 2: ; statement body 2 BREAK ; ... default : statement n-body; BREAK ; } Process: Case of the listed value based on the order and comparing the value of the expression, if it is true, the statement is executed the body, it encounters a break or the right brace}, the switch end of the statement. If the values are not matched to the case of, default statement is executed corresponding n body
note:
. A switch expression can accept data types:
Basic types: byte, short, char, int
Reference types: enumeration (JDK1.5 above), String string (JDK1.7 above)
b. values only after case is constant, not variable, and the value of a switch case can no longer appear in the same sentence
. c Finally a break can be omitted, but is not recommended; other break must not be omitted, otherwise there will be case penetrate
d. default no matter what position it is finally implemented, it is on the final recommendation, easy to read
and if usage scenario switch:
switch is recommended when using a fixed value determination;
if Analyzing recommended when used interval or range
③. Loop structure
Category: for statement, while statement, do ... while statement
for statement format: for (initial expression; conditional expressions; after cyclic operation expression) { loop body; } Process: 1. perform the initial expression 2 of a condition expression true, proceed to false, the loop ends 3. Run loop statement 4. after execution cycle operation expression 5. Return to step 2 to continue to
give chestnut:
statistical sum of an even number within 1-100
int sUM = 0;
for (int I = 0; I <= 100; I + 2 =) {
SUM = I +;
}
another implementation:
for(int i=0; i<=100; i++){
if(i%2==0){
sum += i;
}
while statement basic format: while (judgment condition statement) { loop statement; } full format: initialization statement; while (judgment condition statement) { loop statement; controlling conditional statement; } Process: 1. Perform initialization statement
2. Analyzing execution conditional statements
true, proceed to
false, the loop ends
3. execution loop statement
4. A conditional statement execution control
5. Return to step 2 to continue
do ... the while statement basic format: do { loop statement; } the while (judgment condition statement); Full Format: initialization statement; do { loop statement; controlling conditional statement; } the while (judgment condition statement); flow: 1 perform initialization statement 2 . statement execution cycle body 3 . performs control conditional statement 4 . statement execution determination condition to true , proceed to false , the end of cycle 5. Go back to step 2 to continue
Note: Three loop differences
1.do ... while loop will execute at least one loop; while for and while loops must first determine whether the conditions established, and then decide whether to execute the loop body statement
After 2.for statement is executed, the initial definition of variables will be released, it can not be used; and after the while statement is executed, initialization of variables can continue to use
Two simple endless loop manner: . ①the while(to true) { loop statement; } ②.For(;;) { loop statement; }
Nested loop:
Output * ** *** **** shape for ( int I =. 1; I <=. 4; I ++ ) { for ( int J =. 1; J <= I; J ++ ) { Sout ( "*" ); } Sout (); // wrap } ---------------------------- gorgeous dividing line ---------- ------------------- 9 * 9 multiplication table for ( int I =. 1; I <= 9; I ++ ) { for ( int J =. 1; J <= I ; J ++ ) { Sout (J + "*" + I + "=" + (I * J) + '\ T'); // \ T escape character, corresponding to the Tab key } Sout ();// wrap } Summary: row number of outer-loop control, the inner loop controls the number of columns of small expansion: escape character '\ T' : the Tab key '\ R & lt' : Enter '\ n-' : newline '\ "' : double quotes '\' ':apostrophe
(2) The control jump statements
Category: break, continue, return
①.break statement
Only use switch statements and loops in the role is out of the loop
②.continue statement
Can only use the loop statement , is to terminate this cycle, the next cycle continues
③.return statement
Function is returned, to the end of the process
Part II will begin on methods ,, BB ....
Ha ha ha ... code did not move to eat ,,