[Thinking in Java] Chapter 4th-control execution process, Chapter 4th
4.1 if-else4.2 iteration 4.3 Foreach syntax 4.4 return4.5 break and continue4.6 switchDirectory
4.1 if-else
Else In if-else is optional and can be used in two forms:
if (Boolean-expression) statement
Or
if (Boolean-expression) statement1else statement2
Note that Boolean-expression cannot be a number. In fact, the conditional expressions in the while and for cannot be numbers, although we know that C and C ++ can use numbers to determine true or false
1 char test(int score) { 2 if (score >= 90) 3 return 'A'; 4 else if (score >= 80) 5 return 'B'; 6 else if (score >= 70) 7 return 'C'; 8 else if (score >= 60) 9 return 'D';10 else //if (score < 60)11 return 'E'; 12 }
In the above example, if the comments of the 10th rows are removed, the compiler will report an error. Although we can understand the intention of the designer, the compiler is not so intelligent. The Compiler thinks that the 10th rows will not return a value.
4.2 iterations (while, do-while,)
The reason for this is that while, do-while, and for statements are iterative statements because their statements will be executed repeatedly until the Boolean expression that controls the operation gets the "false" result.
The while LOOP format is as follows:
while (Boolean-expression) statement
The do-while loop format is as follows:
Do statementwhile (Boolean-expression); // do not forget the semicolon;
NOTE: The only difference between while and do-while is that the statements in do-while are executed at least once, even if the expression is calculated as false for the first time. In the while loop structure, if the condition is false for the first time, the statements in the condition are not executed at all.
The for loop format is as follows:
for (initialization; Boolean-expression; step) statement
Actually, it is equivalent to a while loop:
initializationwhile (Boolean-expression) { statement step}
The initialization expression (initialization), Boolean-expression, and step can be empty.
Comma operator (not a comma separator)
The only place in Java that uses the comma operator is the control expression of the for loop. In the control expression initialization and step-by-step Control Section, you can use a series of statements separated by commas, and those statements will run independently.
public class CommaOperator { public static void main(String[] args) { for (int i = 1, j = i + 10; i < 5; i++, j = i * 2) { println("i = " + i + "j = " + j); } }}/*i = 1 j = 11i = 2 j = 4i = 3 j = 6i = 4 j = 8*/
The int definition in the for statement overwrites I and j. In the initialization part, you can have any number of variable definitions of the same type. In a control expression, the ability to define multiple variables is only applicable to the for loop and cannot be used in any other selection statement.
As you can see, the statements are executed sequentially in both initialization and step. In addition, the initialization part can have any number of definitions of the same type.
4.3 Foreach syntax
Foreach is more concise and efficient when accessing arrays and containers. Here is an example of accessing arrays.
1 int[] a = new int[10];2 for (int i = 1; i <= 10; i++)3 a[i - 1] = i;4 for (int e : a)5 println(e);
4.4 return
The return keyword has two purposes: specify the value returned by a method (if it is not returned void), and it causes the current method to exit and return that value.
Rewrite the example in 4.1 if-else
char test(int score) { if (score >= 90) return 'A'; if (score >= 80) return 'B'; if (score >= 70) return 'C'; if (score >= 60) return 'D'; else //if (score < 60) return 'E'; }
In this way, you do not need to add else because the code after return will not continue to be executed.
Return can also be used in the method with the return value void. If the return value is not added, a default implicit return will be generated at the end of the method.
Void test (int e) {if (e = 1) return; // if e is equal to 1, the following e ++ will not execute e ++ ;}
4.5 break and continue
Break is used to forcibly exit the current loop without executing the remaining iteration. While continue stops executing the current iteration and then returns to the beginning of the loop to continue the next iteration.
For (int I = 0; I <100; I ++) {if (I = 74) break; // if it is 74, exit the loop if (I % 9! = 0) continue; // if it is not an integer multiple of 9, skip the following statement and continue the next iteration of println (I + "") ;}println (); int I = 0; while (true) {I ++; int j = I * 27; if (j = 1269) break; // if j is equal to 1269, exit loop if (I % 10! = 0) continue; // If I is not an integer multiple of 10, skip the following statement and continue the next iteration of println (I + "");}
Break and continue can also use tags like the goto keyword, but it is better to use them less. The format is as follows:
Label1: // tag. Do not write any code between label1 and outer-iteration. outer-iteration {// external iteration inner-iteration {// internal iteration //... break; // (1 )//... continue; // (2 )//... continue label1; // (3 )//... break label1; // (4 )}}
In (1), break interrupts the inner-iteration and returns to the outer-iteration );
In (2), continue skips the following statement and returns to the starting position of inner-iteration to continue the next internal iteration (inner-iteration );
In (3), continue label1 interrupts both inner-iteration and outer-iteration, and jumps directly to label1. Then it actually continues the iteration process, however, it starts with an external iteration (outer-iteration;
In (4), break label1 interrupts all iterations and returns to label1 division, but does not enter iteration.
The following example shows the break and continue with tags.
Int I = 0; outer: for (; true;) {inner: for (; I <10; I ++) {println ("I =" + I ); if (I = 2) {println ("continue"); continue;} if (I = 3) {println ("break"); I ++; break ;} if (I = 7) {println ("continue outer"); I ++; continue outer;} if (I = 8) {println ("break outer "); break outer ;}for (int k = 0; k <5; k ++) {if (k = 3) {println ("continue inner "); continue inner ;}}}/* output result
I = 0
Continue inner
I = 1
Continue inner
I = 2
Continue
I = 3
Break
I = 4
Continue inner
I = 5
Continue inner
I = 6
Continue inner
I = 7
Continue outer
I = 8
Break outer
*/
The same rule applies to the while iteration.
The key point to remember is that the only reason for using tags in Java is that there is loop nesting, and it is easy to use break and continue from multi-layer nesting.
4.6 switch
The switch is sometimes categorized as an option. Based on the value of the integer expression, the switch statement can be selected from a series of codes for execution. The format is as follows:
switch (integral-selector) { case integral-value1 : statement;break; case integral-value2 : statement;break; case integral-value3 : statement;break; // ... default: statement;}
Here, integral-selector (integer selection factor) is an expression that can generate numerical values. switch can compare the results of this expression with each integral-value (integer value. If a statement is found to be consistent, execute the corresponding statement (a single statement or multiple statements, which do not require parentheses ). If no match is found, the default statement is executed.
In the above definition, you will notice that each case ends with a break, so that the execution process can jump to the end of the switch topic. This is a traditional method for building switch statements, but break is optional. If the break is omitted, the subsequent case statement (if any) will be executed until a break is encountered. Note that the last default statement does not have a break, because the execution process has reached the jump destination of the break. Of course, you can place a break at the end of the default statement, although this is useless.
Note that the selection factor in the switch can only be an integer like int or char, and the string or floating point number cannot be used.
import java.util.*;public class VowelsAndConsonants { public static void main(String[] args) { Random random = new Random(1972); for (int i = 0; i < 5; i++) { int c = random.nextInt(26) + 'a'; println((char)c + ", " + c + ": "); switch (c) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : println("vowel"); break; case 'y' : println("Sometimes a vowel"); break; default: println("Consonant"); } } }}
From this example, we can see that the case statement can be stacked together to form multiple matching for a piece of code. Pay attention to setting the break statement at the end of a specific case. Otherwise, the control process will be moved down to process the subsequent case.