Some 'switch - case' not reading value correctly

Hello, somehow if I do:
switch (j)
case 2:

case 5:

the cases are not corresponding to the value of integer variable 'j'. Can someone please help me figure out why this behavior. Explanation and complete code below. Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <iostream>
/*
 Goal: to read array elements (x,y)
 and print them on the following
 format. This is a sudoku
 puzzle and it will always
 be 9x9 size:

 4 2 3|7 5 1|9 6 8
 7 5 9|6 8 3|1 2 4
 1 6 8|2 4 9|3 5 7
------+-----+------
 9 4 5|3 6 2|8 7 1
 8 7 2|9 1 5|4 3 6
 3 1 6|4 7 8|2 9 5
------+-----+------
 5 3 7|1 9 4|6 8 2
 6 9 1|8 2 7|5 4 3
 2 8 4|5 3 6|7 1 9

*/

using namespace std;
int main() {
    //Build a test array able to hold 9x9 elements
    int testarray[10][10] = {0,0};
    for (int i=0;i<10;i++) {
        for (int j=0;j<10;j++) {
            switch (j) {
                case 2:
                    cout << testarray[i][j - 2] << " " << testarray[i][j - 1] << " " << testarray[i][j] << "|";
                case 5:
                    cout << "* Now j must be 5. j=" << j << endl; //<== Error. Somehow if j=2, it is getting this case 5
                    cout << testarray[i][j - 2] << " " << testarray[i][j - 1] << " " << testarray[i][j] << "|";
                case 9:
                    cout << "* Now j must be 9. j=" << j << endl; //<== Error. Somehow if j=5, it is getting this case 9
                    cout << testarray[i][j - 2] << " " << testarray[i][j - 1] << " " << testarray[i][j] << " " << endl;
            }
        }
    }

    return 0;
}

https://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm

When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
Last edited on
Thank you for the prompt and accurate reply. It works perfectly after the break.
inadequate...
all must
case 9:{

//...

}
break;
@abdulbadii

No, that's not true. You don't need a separate code block enclosed by braces for each case. It's fine to do:

1
2
3
4
5
6
7
8
9
case 1:
   // Some stuff
   break;
case 2:
   // More stuff
   break;
default:
   // Default stuff
   break;

without additional braces.
Last edited on
abdulbadii wrote:
inadequate...
all must
case 9:{
MikeyBoy wrote:
You don't need a separate code block enclosed by braces for each case.

You're both right... and you're both wrong :)

If the code within a case instantiates an object that requires a constructor then you need to put the code inside braces. Otherwise constructor/destructor mayem ensues:
1
2
3
4
5
6
7
8
9
10
11
12
{
    ....
    switch (i) {
    case 1:
        MyClass instance;
        myClass.doSomething();
        break;
    case 2:
        myClass.doSomething();   // wrong. myClass hasn't been constructed
        break;
    }
}   // wrong. compiler destroys instance, which might not have been constructed 
Oh, yeah, construction of objects within a switch-case statement is a specific issue that you have to be careful of - and any good compiler should give warnings/errors as appropriate. But that's not at all the same thing as abdulbadii's misinformation.

(And adding braces wouldn't do anything to help with the problem in your case 2, except maybe produce clearer compiler errors.)

There's certainly nothing in the OP's code which would require adding braces in the case sections.
Last edited on
Topic archived. No new replies allowed.