The process control statements in Java are crucial to Java applications. Of course, they are also true for other languages. Next we will introduce some frequently used ones that are not mentioned. You are welcome to add them.
1. Conditional operators (ternary expressions)
int a = 10;int b = 9;int c = a < b ? 6 : 9;System.out.println(c); //9
In the preceding example, the value of C depends on whether the result of comparison between A and B is true or false. Ternary expression, "?" The result on the left is a Boolean value. In the preceding example, if the result of a <B is true, the value of C is 6, and the result of a <B is 9, in the above example, A <B is false, so the value of C is 9.
2. Usage of IF... else
If .... else does not need to be introduced too much, but I used to like to judge true, resulting in many nested if... else, in fact, you can try to judge false, so the nesting is not very deep.
If (! True) {// todo} // if it is incorrect, the previous Code will not be executed quickly.3. Switch usage
A switch statement is a SELECT statement that determines the statement to be executed based on the value of the variable.
int i = 1;switch (i) {case 0:System.out.println("0 invoked");break;case 1:System.out.println("1 invoked");break;case 2:System.out.println("2 invoked");break;default:System.out.println("default invoked");}
// The output result is 1 invoked.
Among them, the type of variable I is generally byte, short, Int, Char, enumeration type can also be, jdk1.7 can use string, personally not used to use string. "0" in "case 0" is a constant. If the value of variable I matches it, the code of the Branch is executed. The break jumps out of the code block. If break is not written, it runs down until the break or switch is executed. If the variable and constant do not match, the default code is executed. Therefore, the above example will execute "case
1 "code.
4. Break usage
for(int i = 0; i < 10; i++) {if(3 == i) {break;}System.out.print(i + " "); //0 1 2 }
In the above example, when break is executed, the entire loop is exceeded and the code after the loop is executed.
5. Continue usage
for(int i = 0; i < 10; i++) {if(3 == i) {continue;}System.out.print(i + " "); //0 1 2 4 5 6 7 8 9 }
In the above example, when continue is executed, the current loop exists and the next loop continues.
The for loop statement is used in the above two examples.
6. usage of the WHILE LOOP
While (condition) {// code to be executed}
Here, condition is a Boolean expression, so be sure to jump out of the loop conditions, otherwise it will cause an endless loop.
7. Do... usage of the WHILE LOOP
Do {// code to be executed} while (conditon );
Nothing to introduce, while and do... difference of while: If the Boolean expression is judged to be false for the first time, the while loop will not be executed once. Do .. while will be executed once; if the Boolean expression is true for the first time, the two are equivalent.
In fact, in development, it is basically a combination of these process control statements, which will be gradually accessed in the future and will be more impressive at that time.