samedi 24 janvier 2015

How does switch statement compares values? [duplicate]


This question already has an answer here:




Given the following sequence



switch(1) {
case 1:
cout << "first \n";
case 2:
cout << "second \n";
default:
cout << "Not first nor the second";
}


the output is



first
second
Not first nor the second


I'm expecting the output to be



first


so, how do values are compared? I know I didn't use the break statement, but isn't that just to save cpu time? How come the second case executes since there are two different integer values? What am I missing?


I'm using gcc 4.9.2 with -std=c++11 flag.


Aucun commentaire:

Enregistrer un commentaire