A, switch statements use Precautions
switch statement format is as follows
switch (switch表达式){
case 值1: 语句1;break;
case 值2: 语句2;break;
...
case 值N: 语句N;break;
default: 默认情况下执行的语句
}
Note the following issues:
- switch expression must be able to calculate a char, byte, short, int, or a String value, float, and double, and other types are not allowed;
- Value 1, value 2 and the like are constants;
- By default, default is optional;
- Fall behavior: If you forget to use the break, once a match which case, it will begin at the match from the case until it encounters a break statement (here not break statement) or the switch statement ends. The following code, the first 1 day to 5 show WEEKDAY, 0 and 6 show weekends.
switch(day){
case 1:
case 2:
case 3:
case 4:
case 5:System.out.println("Weekday");break;
case 0:
case 6:System.out.println("weekends");
}