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