C语言程序设计第5章 选择控制结构.ppt
文本预览下载声明
Important Rule ! switch (expression) { case value1 : statement1; break; case value2 : statement2; break; …… default : statementX; break; } Must be int or char! 5.8用于多路选择的switch语句 注意! Example: switch (month) { case 1: printf(January\n); break; case 2: printf(February\n); break; case 3: printf(March\n); break; default: printf(Others\n); break; } printf(End); Assume month = 1, so … …this step will be executed. Later … …case is terminated here. Jump to … January _ January End _ 5.8用于多路选择的switch语句 Example: switch (month) { case 1: printf(January\n); break; case 2: printf(February\n); break; case 3: printf(March\n); break; default: printf(Others\n); break; } printf(End); …this step will be executed. Later … March _ March End _ Assume month = 3, so … …case is terminated here. Jump to … 5.8用于多路选择的switch语句 Example: switch (month) { case 1: printf(January\n); break; case 2: printf(February\n); break; case 3: printf(March\n); break; default: printf(Others\n); break; } printf(End); Now…what will happen if this break is taken out from the program? 5.8用于多路选择的switch语句 Example: switch (month) { case 1: printf(January\n); break; case 2: printf(February\n); case 3: printf(March\n); break; default: printf(Others\n); break; } printf(End); No more ! 5.8用于多路选择的switch语句 Example: switch (month) { case 1: printf(January\n); break; case 2: printf(February\n); case 3: printf(March\n); break; default: printf(Others\n); break; } printf(End); …this step will be executed. Later … February _ March _ Assume month = 2, so … …case is terminated here. Jump to … End _ …execution continues. Thus, this step is executed . So … 5.8用于多路选择的switch语句 Example: switch (month) { case
显示全部