Using a switch statement is more concise when you need to make an equivalent judgment on an option. For example: According to the rank of the exam, give the top 4 different prizes. First prize notebook one, second prize IPAD 21, Third prize mobile power one; the last one to reward u disk.
Grammar:
Execution procedure: When the value of the expression after switch is the same as the value after the case statement, it starts down from that position until the break statement is encountered or the switch statement block ends, and the code of the default block is executed if there is no matching case statement.
Such as:
Have to say a few little secrets:
1, switch the value of the expression after the parentheses must be integer or character type
2. The value after the case can be a constant value , such as 1, 2, or a constant expression , such as a. T, but not a variable or an expression with a variable, such as a * 2
3, case matching, execute the program code in the matching block, if not met break will continue to execute the contents of the next case block until the break statement or switch statement block end as
,
Operation Result:
4, the function of the same case statements can be combined, such as
5, the default block can appear in any position , you can also omit
Switch of the Java conditional statement