33

Why this is illegal:

switch(x) {

case 1:
   int a = 1;
   break;
case 2:
   int a = 2;
   break;
}

It looks like it could have been legal. What are some deeper reasons for that to be illegal?

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158

7 Answers7

45

Because the scope is delimited by { and } and you have the a variable twice.

You can avoid this compilation error by:

switch(x) {
    case 1: {
        int a = 1;
        break;
    }
    case 2: {
       int a = 2;
       break;
    }
}

Note that in your example the compiler fails to succeed, because if you remove the first break statement, а something that is called fall-through might happen:

All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.

In this case the statements that are going to be executed (because of the fall-through), are :

  1. int a = 1;
  2. int a = 1;
  3. break;

And as you can see, the a variable is duplicated, which is why the compiling fails in your example.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
43

if the break statements are not used, Then we know that the following cases will be executed. So if you are permitted to declare in both cases that will cause a conflict. For example

switch(x) {

case 1:
   int a = 1;  // i have removed the break;
case 2:
   int a = 2; // now what will happen here :)
   break;
}
stinepike
  • 54,068
  • 14
  • 92
  • 112
8

There's nothing related to a switch here. You simply can't declare the same variable twice in the same scope.

This is illegal :

int a = 1;
int a = 2;

Now, let's suppose you wanted, in fact, your case branchs to act as scopes. Then the problem would be that you wouldn't have been able to use the variable after the switch (outside of scope).

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
4

The scope of the variables in each case clause corresponds to the whole switch statement.

However, if you want to use the same variable, you can define your own block inside each case statement by using the curly braces { and }.

Harish Talanki
  • 866
  • 13
  • 27
2

The switch statement is a code block. The switch statement evalutates it's expression, then executes all statements that follow the matching case label.

In this case it evaluates x and this is compared with each constant until a match is found.

But actually it is like saying:

for(int i = 0; i < 4; i++) {
    int i = 1;
}

this will also not work, because i is already defined.

WonderWorld
  • 956
  • 1
  • 8
  • 18
0

You are defining a inside the local scope of the switch. As such, you assign a a value but you do not pass that value anywhere so it seems redundant.

If you were to declare a outside the switch and then call it within the switch it should work.

int a;

switch(x) {
case 1:
   a = 1;
   break;
case 2:
   b = 2;
   break;
}

Alternatively, in each case of the switch, you could manipulate your new int and then do something with it like so:

switch(x) {
case 1:
   int a = 1;
//do something with your new int here that affects a parameter outside the switch
   break;
case 2:
   int b = 1;
//do something with your new int here that affects a parameter outside the switch
   break;
}

Plus, it appears that defining a variable multiple times in a switch is illegal due to conflictions, as the variable a is defined within local scope to mean one thing in the first case of the switch. As such, you cannot define it to mean something else in the second case.

DeliciousFish
  • 48
  • 1
  • 8
0

Go with these facts: ->You cannot declare the same local variable twice in single scope. ->case does not create its own scope. ->switch, yes creates its own scope. ->Proof: If you do not break a case, all the cases will be executed unless they satisfy the condition. (unlike else if).

Srujan Barai
  • 2,295
  • 4
  • 29
  • 52