规则:
进入switch--如果有匹配的case直接跳转到匹配的那一行往下一直执行到break为止。(忽略case)
如果没有匹配项直接跳转到default执行到break为止。
如果没有default,默认最后一行有default: break;
测试程序:
#include<iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; ++i) {
cout << "i=" << i << "====" << endl;
switch (i) {
case 1:cout << 1 << endl; break;
case 2:cout << 2 << endl;
case 3:cout << 3 << endl;
default:cout << "default" << endl;
case 4:cout << 4 << endl; break;
}
}
system("pause");
}
结果: