Switch statement Suitable for matching specific values.
Switch (expression) {
Case value 1:
Sentence body 1
Break
Case value 2:
Sentence body 2
Break
Case value 3:
Sentence body 3
Break
Default:
Sentence body n
Break
}.
1, Explain the structure of the switch statement
1. Switch represents the use of switch statements
2. Expression:
To match the value of an expression with a specific value (the content to be determined)
The formula composed of operands and operators.
3. Case:
Using a case to list specific values.
4. Sentence body:
If the value of the expression matches the specific value after the case (equal), execute the statement body below the corresponding case.
5. break:
After executing the body of the statement, end the switch statement and no longer execute the body downwards.
6. default:
Usually used to handle erroneous data or situations where specific values cannot be listed using a case.
2, The execution process of switch statements
1. Calculate the value of an expression
2. Use the result of the expression to match the values after all cases
(1) If it matches, execute the statement body in the corresponding case
(2) If the value of the expression does not match the values after all cases, execute the body of the statement in default.
3. If a break or} is encountered, the switch statement ends
3, Notes on switch statements
1. Case
The value after the case must be a literal value (constant) and cannot be a variable; And the values after multiple cases cannot be equal, resulting in duplicate case labels being reported.
2. Break
Can it be omitted
(1) Not writing a break will result in case penetration:
After executing the body of the statement in the current case,
Continue to execute the body of statements in other cases sequentially,
Until a break or} is encountered, the SWTCH structure will end
(2) Using case penetration to reduce code redundancy
When multiple cases have the same statement body
Just add the body of the statement in the last case.
3. Default
Can it be omitted
Can be omitted, but generally not omitted.
Does default have to be placed last
The default position can be placed arbitrarily, but its execution order will not change
It must be when the value of the expression does not match the values after all cases
Only then will the statement body in the default be executed
Will not change due to the default placement order.
[Case].
import java.util.Scanner;
class SwitchDemo1{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("please enter the month :");
int month = sc.nextInt();
switch(month){
case 12:
case 1:
case 2:
System.out.println(month+"the month is winter");
break;
default:
System.out.println("the month you entered is incorrect !");
break;
case 3:
case 4:
case 5:
System.out.println(month+"the moon is spring");
break;
case 6:
case 7:
case 8:
System.out.println(month+"the month is summer");
break;
case 9:
case 10:
case 11:
System.out.println(month+"the moon is autumn");
break;
}
System.out.println("hello");
}
}