[第4章控制语句2.pptVIP

  • 2
  • 0
  • 约1.4万字
  • 约 45页
  • 2017-01-12 发布于北京
  • 举报
[第4章控制语句2

* * * * * * * * * * * * * * * switch多分支选择结构 switch语句(开关语句) 在一个表达式可能的一组固定数量的值中进行选择,并采取不同的动作。 选择结构 switch(表达式) { case 常量表达式_1: 语句1; [break;] case 常量表达式_2: 语句2; [break;] …… case 常量表达式_n: 语句n; [break;] [default: 语句n+1; [break;]] } 条件表达式:整型/字符型表达式(或枚举表达式) 常量表达式:整型/字符型常量表达式 Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. * switch语句 举例 根据考试成绩的等级打印相应的分数段。 选择结构 #include stdio.h int main() { char grade; grade=getchar(); switch(grade) { case ‘A’: printf(“85~100\n”); case ‘B’: printf(“70~84\n”); case ‘C’: printf(“60~69\n”); case ‘D’: printf(“60\n”); default: printf(“error\n”); } return 0; } B 70~84 60~69 60 error ? Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. * switch语句 举例 结果分析 流程图 选择结构 print “60~69” print “70~84” print “85~100” print “60” print “error” ‘A’ ‘B’ ‘C’ ‘D’ default grade Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. * switch语句 举例 正确的流程图 选择结构 print “60~69” print “70~84” print “85~100” print “60” print “error” ‘A’ ‘B’ ‘C’ ‘D’ default grade Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. * void main() { float grade; grade=getchar(); switch(grade) { case ‘A’: printf(“85~100\n”); break; case ‘B’: printf(“70~84\n”); break; case ‘C’: printf(“60~69\n”); break; case ‘D’: printf(“60\n”); break; default: printf(“error\n”); } } switch语句中的break 举例 得到正确结果 选择结构 B 70~84 break语句 跳转至switch语句之后的第一条语句 OK Evaluation only. Created with Aspose.Slides for .NET 3.5 Client Profile 5.2.0.0. Copyright 2004-2011 Aspose Pty Ltd. * switch语句 举例 完成一个简单的计算器程序,能实现两个浮点数的加、减、乘、除运算。(例4.6) 选择结构 print a*b print a-b print a+b print a/b print “error” ‘+’ ‘-’ ‘*’ ‘/’ default operator Evaluation only. Created with Aspose.Slides for .NET

文档评论(0)

1亿VIP精品文档

相关文档