Switch Case Statement
The switch statement evaluates an expression, matches the expression’s value to a case clause and executes the statements associated with that case.
Following is the syntax.
switch(variable_expression) {
case constant_expr1: {
// statements;
}
break;
case constant_expr2: {
//statements;
}
break;
default: {
//statements;
}
break;
}
The value of the variable_expression is tested against all cases in the switch. If the variable matches one of the cases, the corresponding code block is executed. If no case expression matches the value of the variable_expression, the code within the default block is associated.
Example
void main() {
int gfg = 2;
switch (gfg) {
case 1:
{
print("jeet number 1");
}
break;
case 2:
{
print("jeet number 2");
}
break;
case 3:
{
print("jeet number 3");
}
break;
default:
{
print("This is default case");
}
break;
}
}