《C语言编程学习课件 (18).pdf》由会员分享,可在线阅读,更多相关《C语言编程学习课件 (18).pdf(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Programming In CProgramming In C switch statementswitch statement Using the third form of if statement(if-else if)can realize multi-way selection structure,but due to too many levels,it will reduce the readability of the program and cause confusion.Using the switch multi-way selection statement prov
2、ided in C can implement the multi-way selection structure more easily and intuitively.Programming In C switch(expression E)case constant c1:statement 1 case constant c2:statement 2 .case constant cn:statement n default:statement n+1 Statement form When using it,note that the types of the expression
3、E and the value of the constant expression after the case should be the same integer or character.Float type is not allowed.Execution process The execution process of the statement is as follows:first,calculate the value of the expression,and then compare it with the value of the constant expression
4、 in each case in turn.If there is an equivalent,the execution starts from the case and proceeds down.starts from default.When the break statement is encountered during execution,jump out of the switch statement,otherwise the execution will continue to be in order,that is,the statements following oth
5、er cases will be executed,and it will not stop until it encounters the.Flow diagram Programming In C Programming In C#include stdio.h void main()int week;scanf(%d,&week);switch(week)case 0:printf(Sundayn);case 1:printf(Mondayn);case 2:printf(tuesdayn);case 3:printf(wednesdayn);case 4:printf(Thursday
6、n);case 5:printf(Fridayn);case 6:printf(Saturdayn);default :printf(errorn);For example,what day is it today?Execution result:4 Thursday Friday Saturday error Programming In C case constant c1:statement 1;break;case constant c2:statement 2;break;.case constant cn:statement n;break;default:statement n
7、+1;switch(expression E)Programming In C switch(week)case 0:printf(Sundayn);break;case 1:printf(Mondayn);break;case 2:printf(tuesdayn);break;case 3:printf(wednesdayn);break;case 4:printf(Thursdayn);break;case 5:printf(Fridayn);break;case 6:printf(Saturdayn);break;default :printf(errorn);Execution res
8、ult:4 Thursday Execution result:0 Sunday Execution result:9 error Programming In C switch(week)case 1:case 2:case 3:case 4:case 5:printf(Workdayn);break;case 0:case 6:printf(Weekendn);break;default :printf(errorn);Execution result:2 Workday Execution result:0 Weekend multiple cases share a set of ex
9、ecution statements Programming In C#include void main()float a,b;char op;printf(Please enter a,b and op:);scanf(%f%c%f,&a,&op,&b);switch(op)case +:printf(%.1f+%.1f=%.1fn,a,b,a+b);break;case -:printf(%.1f-%.1f=%.1fn,a,b,a-b);break;case *:printf(%.1f*%.1f=%.1fn,a,b,a*b);break;case /:if(b!=0.0)/*分母不能等于0*/printf(%.1f/%.1f=%.1fn,a,b,a/b);else printf(error!n);break;default:printf(error!n);break;Lets write a calculator that can perform addition,subtraction,multiplication,division,and remainder operations on two integers.Programming In CProgramming In C
限制150内