The switch declaration allows you to select an option for running a set of statements, based on a code return value expression.
Switch (expression ){
Case value_1:
Statement (s );
Break;
Case value_2:
Statement (s );
Break;
.
.
.
Case value_n:
Statement (s );
Break;
Default:
Statement (s );
}
Instance test
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 .");
}
}
Method two characters
Public class mainclass {
Public static void main (string [] args ){
Char yesno = 'n ';
Switch (yesno ){
Case 'n ':
System. out. println ("no selected ");
Break;
Case 'Y ':
System. out. println ("yes selected ");
Break;
}
}
}
Method 3
Public class main {
Public static void main (string [] args ){
Int I = 0;
Switch (I ){
Case 0:
System. out. println ("I is 0 ");
Case 1:
System. out. println ("I is 1 ");
Case 2:
System. out. println ("I is 2 ");
Default:
System. out. println ("free flowing switch example! ");
}
}
}
/*
I is 0
I is 1
I is 2
Free flowing switch example!
*/