C语言 | Switch的错误用法
1.示例(问题)
void FlowWndPlay_DownKey3Ctrl(void)
{
switch (g_PlbData.State)
{
case PLB_ST_PLAY_MOV:
case PLB_ST_FWD_MOV:
case PLB_ST_BWD_MOV:
{
UxStatic_SetData(&UIFlowWndPlay_VKEY3CCtrl, STATIC_VALUE, ICON_IC_BAR_PLAY);
break;
}
case PLB_ST_PAUSE_MOV:
{
UxStatic_SetData(&UIFlowWndPlay_VKEY3CCtrl, STATIC_VALUE, ICON_IC_BAR_RIGHT_OFF);
break;
}
case PLB_ST_FULL:
{
UxStatic_SetData(&UIFlowWndPlay_VKEY3CCtrl, STATIC_VALUE, ICON_IC_BAR_RIGHT);
break;
}
UxCtrl_SetShow(&UIFlowWndPlay_VKEY3CCtrl, TRUE);
}
}
实际运行时,当case 里面匹配执行后;UxCtrl_SetShow(&UIFlowWndPlay_VKEY3CCtrl, TRUE) 这一句将不会运行,这就脱离了我们的实际目的。
改写如下:
void FlowWndPlay_UpKey2Ctrl(void)
{
UINT32 uiPBFileFmt;
PB_GetParam(PBPRMID_CURR_FILEFMT, &uiPBFileFmt);
switch (g_PlbData.State)
{
case PLB_ST_PLAY_MOV:
case PLB_ST_FWD_MOV:
case PLB_ST_BWD_MOV:
{
UxStatic_SetData(&UIFlowWndPlay_VKEY2CCtrl, STATIC_VALUE, ICON_IC_BAR_PLAY_FAST);
break;
}
case PLB_ST_PAUSE_MOV:
{
UxStatic_SetData(&UIFlowWndPlay_VKEY2CCtrl, STATIC_VALUE, ICON_IC_BAR_LEFT_OFF);
break;
}
case PLB_ST_FULL:
{
UxStatic_SetData(&UIFlowWndPlay_VKEY2CCtrl, STATIC_VALUE, ICON_IC_BAR_LEFT);
break;
}
}
if (uiPBFileFmt & PBFMT_JPG){
UxStatic_SetData(&UIFlowWndPlay_VKEY2CCtrl, STATIC_VALUE, ICON_IC_BAR_MENU);
}
UxCtrl_SetShow(&UIFlowWndPlay_VKEY2CCtrl, TRUE);
}
2.实验
测试1:
#include <stdio.h>
int main(int argc, char* argv[])
{
int a = 2;
switch(a)
{
case 1:
printf("1\n");
break;
case 2:
printf("2\n");
break;
printf("3\n");
}
return 0;
}
运行:
[lanfeiye@fly-vm cStudy]$ ./testSwitch
2
测试2:
#include <stdio.h>
int main(int argc, char* argv[])
{
int a = 3;
switch(a)
{
case 1:
printf("1\n");
break;
case 2:
printf("2\n");
break;
printf("3\n");
}
printf("4\n");
return 0;
}
运行:
[lanfeiye@fly-vm cStudy]$ make testSwitch
gcc -o testSwitch testSwitch.c -g -Wall
[lanfeiye@fly-vm cStudy]$ ./testSwitch
4
|