给出一个简单的switch语句
switch (int)
{
case 1 :
{
printf("1\n");
break;
}
case 2 :
{
printf("2\n");
}
case 3 :
{
printf("3\n");
}
}
Run Code Online (Sandbox Code Playgroud)
在案例2中没有break语句意味着在案例3的代码中将继续执行.这不是偶然的; 它是这样设计的.为什么做出这个决定?这提供了什么好处与块的自动中断语义?理由是什么?
void Send(int * to, const int* from, const int count)
{
int n = (count+7) / 8;
switch(count%8)
{
case 0: do { *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while (--n>0);
}
}
Run Code Online (Sandbox Code Playgroud)