The switch case statement is used to determine whether the expression following the case is consistent with the switch expression, executes the case statement if it matches, and then break out. While default is executed without a case (it does not break out of the box), Defaul is equivalent to the "third case" and is not used in the switch cases statement.
Public classSwitchdemo { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub intX=1; inty=2; //declaring character variables CharA= ' s '; Switch(a) {//determines whether a character contains a ' + ' character Case+: {System.out.println ("X+y=" + (x+y)); //Exit Switch Break; } Case‘-‘: {System.out.println ("X-y=" + (X-y)); Break; } Case‘*‘: {System.out.println ("X*y=" + (x*y)); Break; } Case‘/‘: {System.out.println ("X/y=" + (x/y)); Break; } default: {System.out.println ("Unknown Operation"); } } }}
Run
X+y=3
The switch statement for Java