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.
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.