Why are different case bodies not automatically in their own scope? For example, if I were to do this:
switch(condition) {
case CONDITION_ONE:
int account = 27373;
case CONDITION_TWO:
// account var not needed here
case CONDITION_THREE:
// account var not needed here
case CONDITION_FOUR:
int account = 90384;
}
the compiler would complain about local variable redefinitions. I understand I could do this:
switch(condition) {
case CONDITION_ONE: {
int account = 27373;
}
case CONDITION_TWO: {
// account var not needed here
}
case CONDITION_THREE: {
// account var not needed here
}
case CONDITION_FOUR: {
int account = 90384;
}
}
to put a block around each set of statements to be executed to put each account
variable in its own scope. But why doesn't the language do this for me?
Why would you ever want to declare a local variable in CONDITION_ONE
's body and then use it in CONDITION_TWO
's? This seems like a TERRIBLE idea which should be explicitly banned, not implicitly permitted.