The switch statement is a multi-branch selection statement. It is often used to select the statement to be executed based on the expression value.
Switch (expression ){
Case value_1:
Statement (s );
Break;
Case value_2:
Statement (s );
Break;
.
.
.
Case value_n:
Statement (s );
Break;
Default:
Statement (s );
}
For example, in a program, 0 ~ 3. Conditional output: Use the numerical code in English to view the instance.
Instance
Public class MainClass {
Public static void main (String [] args ){
Int I = 1;
Switch (I ){
Case 1:
System. out. println ("One .");
Break;
Case 2:
System. out. println ("Two .");
Break;
Case 3:
System. out. println ("Three .");
Break;
Default:
System. out. println ("You did not enter a valid value .");
}
}
}
Instance 2
4.3.2. The switch Statement: a demo
Public class MainClass {
Public static void main (String [] args ){
Int choice = 2;
Switch (choice ){
Case 1:
System. out. println ("Choice 1 selected ");
Break;
Case 2:
System. out. println ("Choice 2 selected ");
Break;
Case 3:
System. out. println ("Choice 3 selected ");
Break;
Default:
System. out. println ("Default ");
Break;
}
}
}