c++switch-statementcase

About the braces in a 'case' statement in 'switch'


Today, while I was trying to write code to just add and subtract the two 2*2 matrices, in which I used a switch statement, I got an error:

case bypass initialization of local variable in function main()

Code

#include <iostream.h>
#include <conio.h>
#include <string.h>

int
main()
{
    int mat1[2][2], mat2[2][2], mat3[2][2];

    cout << "Enter the elements in the first matrix";
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            cin >> mat1[i][j];
        }
    }

    cout << "\n\nEnter the elements of the second matrix";

    for (int k = 0; k < 2; k++) {
        for (int l = 0; l < 2; l++) {
            cin >> mat2[k][l];
        }
    }

    cout << "\n\nsaved......";

    int choice;
    cout << "\n\n\nFor adding these two matrices,press 1";
    cout << "\nFor subtracting these two matrices,press 2";
    cin >> choice;

    switch (choice) {
    case 1:

        cout << "The addition of the two matrices will yield";
        for (int a = 0; a <= 1; a++) {
            for (int b = 0; b <= 1; b++) {
                mat3[a][b] = mat1[a][b] + mat2[a][b];
            }
        }
        break;

    case 2:
        cout << "The subtraction of the two matrices will yield";
        for (int c = 0; c <= 1; c++) {
            for (int d = 0; d <= 1; d++) {
                mat3[c][d] = mat1[c][d] - mat2[c][d];
            }
        }
        break;
    }
    getch();
    return 0;
}

I also found that I can take the rid of this error by placing the code of case(s), into braces, NOW,

  1. my confusion is about the error...
  2. & the requirement of braces in case....

(I know I haven't used the new coding conventions, like <iostream>, std namespace, etc., etc. as I have written it in the Turbo C++ compiler, so a to-the-point answer is humbly requested.)


Solution

  • A switch statement is just a bunch of labels and a goto done by the compiler depending on the value of the thing inside the switch test.

    When you have a local variable in a function, anywhere past the declaration of that variable you can use it. For instance:

    int a;
    // can use a now
    

    However, in a switch statement, if you have a local variable:

    case a:
        int a;
        break;
    case b:
        // we can use variable 'a' here because these cases are just labels
        // used in a goto, i.e. the cases do *not* create a new scope
    

    So when you have a variable in a case, the variable exists in cases below it but the variable won't exist because the code that initialized it got skipped by the case statement. It's hard for me to explain, maybe someone else can do a better job.

    The braces fix this problem because they make the variable local, so that it doesn't exist in subsequent cases. It only gets created if that particular case gets entered, and if you forget a break and control falls through to the next case, the ending } ends the scope and causes the variable to be destroyed so it's not accessible from the next case, and the initialization can't be skipped.

    So just remember that all the cases share scope. That might help you understand this.