Selection Structures - SWITCH Statement - Gateway 选择结构开关语句网关



《Selection Structures - SWITCH Statement - Gateway 选择结构开关语句网关》由会员分享,可在线阅读,更多相关《Selection Structures - SWITCH Statement - Gateway 选择结构开关语句网关(19页珍藏版)》请在装配图网上搜索。
1、Click Here to Add Title,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,Lect 9P.,*,Engineering H192 -Computer Programming,Gateway Engineering Education Coalition,Switch Case Structures,Lecture 9,1,Switch Multiple Selection Structure,A multiple selection structure i
2、s useful when an algorithm contains a series of decisions in which a variable or expression is tested separately for one of several possible integral values.,Each integral value represents a different action to be taken in the algorithm.,C provides the,switch,multiple selection structure to implemen
3、t this type of decision making.,2,Switch-Case Structures,The switch-case syntax is:,switch(,integer expression test value,),case,case _1_fixed_value,:,action(s),;,case,case_2_fixed_value,:,action(s),;,default:,action(s),;,Note use of colon!,3,Switch-Case Structures,The,switch,is the controlling expr
4、ession,Can only be used with constant integer expressions.,Remember,a single character is a small positive integer.,The expression appears in,(),The,case,is a label,The label must be followed by a,:,Braces,not required around statements,4,Switch-Case Structures,Unlike,if-else if-else,structures,when
5、 the value in a,case,matches the test value,all of the actions in the rest of the structure take place.,This is shown in the following program where the user enters a value that matches the first,case,and every action in the structure is executed.,5,A Sample Program to Illustrate Switch-Case,Problem
6、:,Write a program to ask the user to enter his/her letter grade and then respond with an appropriate message regarding his/her academic status.,6,A Sample Program to Illustrate Switch-Case,Algorithm:,1.Set up the environment,2.Prompt user to enter his/her letter grade,3.Get users response,4.If grade
7、 is a or A say“Good Job and go to 9,5.If grade is b or B say“Pretty good and go to 9,6.If grade is c or C say“Better get to work and go to 9,7 If grade is d or D say“You are in trouble and go to 9,8.Say“You are failing,9.Terminate program,7,A Sample Program to Illustrate Switch-Case,/*This program a
8、ssociates a letter grade with a,message appropriate to the score.*/,#include,void,main,(),char,grade,;,printf,(,Enter your current letter graden,);,grade=,getchar,();,8,A Sample Program to Illustrate Switch-Case,switch(,grade,),case,(,a,):,case,(,A,),:,printf,(,Good Job!n,);,case,(,b,),:,case,(,B,),
9、:,printf,(,Pretty good.n,),;,9,A Sample Program to Illustrate Switch-Case,case,(,c,),:,case,(,C,),:,printf,(,Better get to work.n,),;,case,(,d,),:,case,(,D,),:,printf,(,You are in trouble.n,),;,default,:,printf,(,You are failing!n,),;,/*End of switch-case structure*/,/*End of,main,program */,10,Swit
10、ch-Case StructuresResultant Output from Grade Program,/*The following results are produced when the user,enters an A as input to the program prompt.*/,Good Job!,Pretty good.,Better get to work.,You are in trouble.,You are failing!,11,Switch-Case Structures,break;,The problems with the previous progr
11、am can be corrected by use of the,break,statement.,It can be used in either a repetition structure or a selection structure to break out of(that is,to exit from)the structure.,The syntax is:,break;,The following program is the previous one with the addition of the,break,statements.,12,Fixed Program
12、using,Switch-Case Structures,#include,void main(),int,grade,;,printf,(,Enter your current letter graden,),;,while,(,(,grade=,getchar(),),!=,EOF),switch,(,grade,),case,(,a,),:,case,(,A,),:,printf,(,Good Job!n,),;,break,;,13,Fixed Program using,Switch-Case Structures,case,(,b,),:,case,(,B,),:,printf,(
13、,Pretty good.n,),;,break,;,case,(,c,),:,case,(,C,),:,printf,(,Better get to work.n,),;,break,;,case,(,d,),:,case,(,D,),:,printf,(,You are in trouble.n,),;,break,;,14,Fixed Program using,Switch-Case Structures,case,(,f,),:,case,(,F,):,printf,(,You are failing!n,),;,break,;,case,(,),:,case,(,n,):,brea
14、k,;,default,:,printf,(,Invalid grade.Try again.n,),;,/*End of switch/case*/,/*End of while loop */,/*End of main function*/,15,Comments on Last Example Program,Use of the,while,repetition structure-more discussion on repetition structures later this week.,Use of the end-of-file,or EOF,test.Note that
15、 EOF(a DEFINED constant)is a negative integral value,usually a-1 on most(but not all)systems.(EOF is actually defined in the header file.),Use of,int,s(instead of,char,s).Why?,From the keyboard,a generates an EOF signal on most UNIX systems.,16,Comments on Last Example Program,The statements:,case()
16、:case(n):,break;,were used to clear the keyboard input buffer.,Another way to clear it is with the statement:,fflush(stdin);,This statement can prove very useful in todays daily assignment.,17,Comments on Last Example Program,Example of use of,fflush,char,figure,;,float,size,;,printf,(,Enter figure type,),;,fflush(stdin);,scanf,(,%c,&figure),;,/or figure=getchar();,printf,(,Enter size of figure,);,fflush,(stdin);,scanf,(,%f,&size,);,18,Assignment E8,Use a switch-case structure to select from amo
- 温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。