If switch do and while and for
When do these statements work?
1), when judging the fixed number of values, you can use if, you can also use switch. However, it is recommended to use switch for relatively high efficiency.
Switch (variable) {
Case value: The statement to be executed; „
Default: the statement to execute;
}
How it works: compare the values of the variables in parentheses with the values after the case, and the values after the case are the same
Executes the statement following the case, and if there is no same, execute the statement after the default;
Details:
1): Break can be omitted, if omitted, has been executed until a break is encountered;
2): The variable in parentheses after switch should be one of the byte,char,short,int four types;
3): Default can be written anywhere in the switch structure, if the default statement is placed in the first row, regardless of whether expression matches value in case, the program starts execution from default until the first break occurs. 2),
If the data range is judged and the Boolean type of the result of the operation is obtained, the IF is used.
3), the loop structure is used when certain statements need to be executed many times. While and for can be interchanged.
The difference is that if you need to define a variable control loop number. It is recommended to use for. Because the For loop is complete, the variable is freed in memory.
Java language Control Flow