Syntax
Switch(variable){ case 0: . . . case n-1: default:}
Switch(variable)
{
case 0:
.
case n-1:
default:
}
This is a type of control statement in which we are a single variable is checked for multiple values and if neither value matches the code corresponding to default is executed.
import static java.lang.System.out; class Csharpcorner { public static void main(String[] args) { int i=0; switch(i) { case 0: case 1: case 2: case 3: case 4: case 5: case 6:out.print(i); break; default: out.print("No value"); } } }
import static java.lang.System.out;
class Csharpcorner
public static void main(String[] args)
int i=0;
switch(i)
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:out.print(i);
break;
default: out.print("No value");
For a detailed tutorial on Java Control and Loop Statements please visit https://www.c-sharpcorner.com/article/java-loops-and-control-statements/
switch statement is used to choose one of the blocks of code to get some output. Here in switch statement switch expression is evaluated once and compared with each “case” value.
Syntax:
`switch(expression){ case 1 value : // code goes here break;
case 2 value : // code goes here break;
case 3 value : // code goes here break; . . . .
default: // optional // default code goes here}`Advantages of switch statement
switch (variable/expression) {case value1: // statements break;case value2: // statements break; .. .. ... .. .. ...default: // statements}
switch (variable/expression) {
case value1:
// statements
case value2:
.. .. ...
Here the variable should be premitive datatype like byte,int short,char .floating point is not allowed .For more follow the linkhttps://www.programiz.com/java-programming/switch-statement
another linkhttps://www.javatpoint.com/java-switch