switch
case
一致するラベルにジャンプするだけです。それを行った後、それ以上case
のラベルは無視されます。また、暗黙的なものがないことにも注意してくださいbreak
。省略した場合、後続のコードが順番に実行されます。
そう、
for (int i=0; i<3; i++) // statements (1,2,3)
{
switch(i) // statement 4
{
case 0: cout<<"ZERO"; // statement 5
case 1: cout<<"ONE"; continue; // statements 6; 7
case 2: cout<<"TWO"; break; // statements 8; 9
}
cout<<endl; // statement 10
}
ほどく
i = 0; // statement 1
// begin first iteration with i=0
if (i<3) => true // statement 2
switch (i) => goto case 0 // statement 4
case 0: cout<<"ZERO" // statement 5
cout<<"ONE"; // statement 6
continue; // statement 7
=> jump to next iteration of loop
i++; // statement 3
if (i<3) => true // statement 2
// second iteration, i=1
switch (i) => goto case 1 // statement 4
case 1: cout<<"ONE"; // statement 6
continue; // statement 7
=> jump to next iteration of loop
i++; // statement 3
if (i<3) => true // statement 2
// second iteration, i=2
switch (i) => goto case 2 // statement 4
case 2: cout<<"TWO"; // statement 8
break; // statement 9
=> jump to end of switch
cout << endl; // statement 10