Like if statements, a switch statement can also be nested. There can be a switch as part of the statement sequence of another switch as shown below. For instance, the following code fragment is perfectly all right in java.
switch(a)
{
case 1: switch(b)
{
case 0: statement 1;
case 1: statement 2;
}
break;
case 2: may be another switch or statement.
}
A switch statement only evaluates the expression once, while if statement will evaluate it repeatedly until it finds a match. Thus, a switch is an efficient choice in such a situation.
There is another thing that we would like you to keep in mind that helps in better programming. And for that: Always put a break statement after the last case statement in a switch.
Although it is not necessary to put a break after the last statement in a switch, since control will leave the statement anyway, yet it should be done to avoid forgetting the break when you add another case statement at the end of the switch.
When there is not any break statement after any case, then the next will execute in a sequence and all the time all the cases will executed. For example:
switch (i++)
{
case 1:
case 2:statement 1;
case 3:statement 2;
case 4:statement 3;
default: statement 4;
}
When case 1 is matched, all the statement till the end of switch are executed as there is no break statement. That’s why programmer must use break statement at the end of each case in switch statement.
switch(a)
{
case 1: switch(b)
{
case 0: statement 1;
case 1: statement 2;
}
break;
case 2: may be another switch or statement.
}
Important things about Switch
Following important things you must know about the switch statement:- A switch statement cannot work for non-equality comparisons.
- The case labels of switch statements must be literals or constants.
- No two case labels in the same switch can have identical values. But, in case of nested switch statements the case constants of the inner and outer switch can contain common values.
- A switch statement works with data types byte, short, int and char only.
- The switch statement is more efficient than if in a situation that supports the nature of switch operation.
A switch statement only evaluates the expression once, while if statement will evaluate it repeatedly until it finds a match. Thus, a switch is an efficient choice in such a situation.
There is another thing that we would like you to keep in mind that helps in better programming. And for that: Always put a break statement after the last case statement in a switch.
Although it is not necessary to put a break after the last statement in a switch, since control will leave the statement anyway, yet it should be done to avoid forgetting the break when you add another case statement at the end of the switch.
When there is not any break statement after any case, then the next will execute in a sequence and all the time all the cases will executed. For example:
switch (i++)
{
case 1:
case 2:statement 1;
case 3:statement 2;
case 4:statement 3;
default: statement 4;
}
When case 1 is matched, all the statement till the end of switch are executed as there is no break statement. That’s why programmer must use break statement at the end of each case in switch statement.
No comments:
Post a Comment