The format of the switch statement is as follows: (its function is to select a piece of code to execute)
Copy the code code as follows:
switch(integer selection factor) {
case integer value 1: statement; break;
case integer value 2: statement; break;
case integer value 3: statement; break;
case integer value 4: statement; break;
case integer value 5: statement; break;
...
default: statement;
}
But there are a few points to note:
1. The parameter type of case in switch can only be int type, but byte, short, and char types can also be used because byte, short, and shar can be automatically promoted (automatic type conversion) to int, so in the final analysis, they are still of int type. Let me explain here that Java has 8 data types: byte, short, char, int, long, float, double, and boolean. Boolean cannot be converted to any type of data. Small types can be automatically converted into large data types. , large data types must be converted into small ones, and forced conversion is required.
2.The case can be followed by an expression.
3.break is used to jump out of the entire switch statement. If not, the next branch will be executed.
4. Good programmers will make good use of defaults.