ImageVerifierCode 换一换
你正在下载:

cs202ch2.docx

[预览]
格式:DOCX , 页数:16 ,大小:25.54KB ,
资源ID:8010122      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/8010122.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(cs202ch2.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

cs202ch2.docx

1、cs202ch2COURSE : C Language (CS102)UNIT : 2.1 CONTROL STRUCTURE SEQUENCE* Instructions are executed one after another without branching.* E.g. # include main() int a, b ; printf ( Enter the first integer ) ; scanf ( %d , &a); printf ( Enter the second integer n ); scanf ( %d , &b); printf ( Sum is %

2、d n , a+b ); return 0; SELECTION STRUCTUREA selection structure is used to choose among different actions. Single Selection ( IF Selection Structure ) * It performs an action only when a condition is true. * Format : if (condition) statement Problem : print the word Passed if the grade is greater th

3、an 60 Coding : if (grade 60) printf ( Passed n ); (print only if grade 60) Warning : if (grade60); - if (grade60) ; printf( “ Passed n” ); printf( “ Passed n” ); (print with any grade)Double Selection (IF/ ELSE Selection Structure)* It performs an action when a condition is true, perform a different

4、 action when the condition is false * Format : if (condition) statement-1 else statement-2 Problem : print the word Passed if the grade is greater than 60 print the word Failed if the grade is below or equal 60 Coding : if (grade 60) printf ( Passed n ); else printf( Failed n ); Multiple Selection N

5、ested IF/ELSE Selection Structure * Format : if (condition-1) statement-1 else if (condition-2) statement-2 . * Problem(1) : print A for the marks greater than or equal to 80. print B for the mark greater than or equal to 70. print C for the mark greater than or equal to 60. And F for other marks *

6、Coding : if (grade = 80) /80-100 printf ( A n ); else if (grade = 70 ) /70-79 printf ( B n ); else if (grade = 60 ) /60-69 printf ( C n ); else /59 or below printf ( F n ); *if selection structure expects only one statement in its body, to include several statements in the body, enclose the set of s

7、tatements using * E.g. if (grade 60) printf( Failed n ); printf( You must work hard n ); else if . * Problem (2): Convert an integer into the corresponding day of a week. * Coding : if (day = 1) printf ( Today is Monday n ); else if (day = 2 ) printf ( Today is Tuesdayn ); else if (day = 3) printf (

8、 Today is Wednesday n ); else if (day = 4 ) printf ( Today is Thursday n ); else if (day = 5) printf ( Today is Friday n ); else if (day = 6 ) printf (Today is Saturdayn); else printf(“Today is Sundayn”)Switch structure ( multiple selection) Problem : Convert an integer into the corresponding day of

9、 a week. Coding : main() int day ; printf(Enter an integer between 1 to 7. Enter -1 to quit : ); scanf(%d , &day) ; switch (day) case 1 : printf(Today is Mondaynn) ; break ; case 2 : printf(Today is Tuesdaynn) ; break ; case 3 : printf(Today is Wednesdaynn) ; break ; case 4 : printf(Today is Thursda

10、ynn) ; break ; case 5 : printf(Today is Fridaynn) ; break ; case 6 : printf(Today is Saturdaynn) ; break ; case 7 : printf(Today is Sundaynn) ; break ; default : printf(Incorrect Number Entered!) ; break ; return 0; Enter an integer between 1 to 7. Enter -1 to quit : 1 Today is Monday Enter an integ

11、er between 1 to 7 . Enter -1 to quit : 6 Today is Saturday Enter an integer between 1 to 7 . Enter -1 to quit : 9 Incorrect Number Entered! Enter an integer between 1 to 7 . Enter -1 to quit : -1 Break * The switch structure is exited immediately with the break statement. * If break is not used, eve

12、n a match is found, the statements for all the remaining cases will still be executed. * break can /cannot be used in default statement (last statement) Default * If no match occurs, the default case is executed * place the default clause at the end of the switch structure. Case * Each case can have

13、 one or more action and braces are not required around multiple action in a case of a switch Switch *switch structure can only be used for testing a constant integral expression, that is, any combination of character constants and integer constants that evaluates to a constant integer value. switch(

14、day) E.g. case 1 : . case 2 : . E.g. case a : . case b : . E.g. case abc : . WRONG USE! case 123 : . REPETITION (looping) STRUCTURE* An action will be repeated while some condition remains true.* The action will stop only if the condition become false. WHILE Repetition Structure * Format : while (co

15、ndition) statement * Problem(1): sum all the integer from 1 to 10 * Coding : main() int sum, number; sum=0; number = 1 ; while (number = 10) sum = sum+number ; number+; printf(“Sum is %d ”, sum); Sum is 55 * There will be an infinite loop if the condition always true. * Must be an action that eventu

16、ally cause the condition to become false *Probem(2): Determine the class average of a test taken by 5 student . * Coding : main() int student_counter, total_mark, mark ; double average ; total_mark = 0 ; student_counter = 1; while (student_counter = 5) printf (Please enter the mark : ); scanf ( %d ,

17、 &mark ) ; total_mark = total_mark + mark ; student_counter = student_counter +1 ; average = total_mark / 5 ; printf ( The class average is %lf n , average) ; return 0 ; * OUTPUT : Please enter the mark : 50 Please enter the mark : 10 Please enter the mark : 90 Please enter the mark : 93 Please ente

18、r the mark : 50 The class average is 58.000000 * Actual average should be (50+50+10+90+93)/5 58.6 Why %f prints 58.000000 instead of 58.6 ? NOTE : average = total_mark /10 ; (float) (int) (int) Integer division yields an integer result Significant digits are cut. *Problem(2) Revised Program * Coding

19、 : main() int student_counter, mark ; double average, total_mark ; total_mark = 0 ; student_counter = 0 ; printf( Enter the mark , or enter -1 to end : ) ; scanf( %d , &mark ) ; while (mark != -1) total_mark = total_mark + mark ; student_counter = student_counter +1 ; printf (Pls enter the next mark

20、,or enter -1 to end:); scanf ( %d , &mark ); if (student_counter != 0) average = total_mark /student_counter ; printf ( The class average is %.3lf n , average) ; else printf( No marks were entered n ); return 0 ; * OUTPUT Enter the mark , or enter -1 to end : 50 Please enter the next mark , or enter

21、 -1 to end : 10 Please enter the next mark , or enter -1 to end : 90 Please enter the next mark , or enter -1 to end : 93 Please enter the next mark , or enter -1 to end : 50 Please enter the next mark , or enter -1 to end : -1 The class average is 58.600 * OUTPUT Enter the mark , or enter -1 to end

22、 : -1 No marks were entered * Format string %.3f .3 - a floating-point with 3 decimal digits to the right of decimal point. eg. printf( %.2f , 9.348) ; 9.35 eg. printf( %.1f , 9.348) ; 9.3 eg. printf( %.0f , 9.348) ; 9 The default precision is (%.6f = %f ) * Precision are only used in printf, not sc

23、anf . eg. scanf(“%.5lf n” , &a) wrong ! scanf(“%lf ” , &a) * Do not compare floating-point value for equality.DO/ WHILE Structure It test the loop-continuation condition after the loop body is performed, so loop body will be executed at least once. * Format : do statements ; while (condition); * Pro

24、blem : Find the no.of times the statements has been executed in the loop . * Coding Using Do/While Loop : main() int loop_count, number ; loop_count = 0 ; number =0 ; do loop_count+; number-; while (number 0) ; printf ( Loop_count is %d n , loop_count) ; printf ( Number is %d n , number) ; return 0

25、; Loop_count is 1 Number is -1 * Coding Using While Loop : main() int loop_count, number ; loop_count = 0 ; number =0; while (number 0) loop_count+ ; number-; printf ( Loop_count is %d n , loop_count) ; printf ( Number is %d n , number) ; return 0 ; Loop_count is 0 Number is 0 FOR Structure * Proble

26、m : Print 1 to 10 1 * Coding (1): main() 2 int counter ; 3 4 for (counter=1; counter=10; counter+) 5 6 printf( %d , counter) ; : printf(n) ; : return 0 ; * Coding(2) : main() int counter=1 ; for ( ; counter 11 ; counter+) printf( %d , counter) ; printf(“n”); return 0 ; * 1 2 3 4 5 6 . * for (counter

27、=1 ; counter=10 ; counter+) Initialization, repetition condition, increment are all put in the header. - Initialization occurs only once, - Increment occurs after the body statement is performed. 1 2 4 for (counter=1; counter=10; counter+) printf( %d , counter) ; 3 printf(n) ; 1 2 3 4 * The conditio

28、n (counter = 10 ) or (counter 11 ) is OK But (counter 10 ) will run only 9 time and caused off-by-one error. * For Loop While Loop main() main() int count ; int count ; for (count=1;count =10;count+) count =1 ; printf( %d , count) ; while (count = 10 ) printf(“n”); return 0 ; printf( %d , count) ; count+; printf(“n”); return 0 ; * Initialization, condition, and

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1