C++程序设计课件:Chapter9 Additional Control Structures

上传人:鲁** 文档编号:568782260 上传时间:2024-07-26 格式:PPT 页数:60 大小:364KB
返回 下载 相关 举报
C++程序设计课件:Chapter9 Additional Control Structures_第1页
第1页 / 共60页
C++程序设计课件:Chapter9 Additional Control Structures_第2页
第2页 / 共60页
C++程序设计课件:Chapter9 Additional Control Structures_第3页
第3页 / 共60页
C++程序设计课件:Chapter9 Additional Control Structures_第4页
第4页 / 共60页
C++程序设计课件:Chapter9 Additional Control Structures_第5页
第5页 / 共60页
点击查看更多>>
资源描述

《C++程序设计课件:Chapter9 Additional Control Structures》由会员分享,可在线阅读,更多相关《C++程序设计课件:Chapter9 Additional Control Structures(60页珍藏版)》请在金锄头文库上搜索。

1、7/26/2024C+程序设计 教师:1Chapter 9. Additional Control Structures7/26/2024C+程序设计 教师:2Switch StatementIs a selection control structure for multi-way branching.SYNTAXswitch ( IntegralExpression )case Constant1 :Statement(s); / optionalcase Constant2 :Statement(s); / optional . . .default :/ optionalStateme

2、nt(s); / optional7/26/2024C+程序设计 教师:3float weightInPounds = 165.8 ;char weightUnit ;. . . / user enters letter for desired weightUnitswitch ( weightUnit )case P :case p :cout weightInPounds “ pounds “ endl ;break ;case O :case o :cout 16.0 * weightInPounds “ ounces “ endl ;break ;case K :case k :cou

3、t weightInPounds / 2.2 “ kilos “ endl ;break ;case G :case g :cout 454.0 * weightInPounds “ grams “ endl ;break ;default :cout “That unit is not handled! “ response ; / skips leading whitespaceif ( ( response != y ) & ( response != n ) ) cout “Please type y or n : “ ; while ( ( response != y ) & ( r

4、esponse != n ) ) ;Function Using Do-While7/26/2024C+程序设计 教师:8Do-While Loop vs. While LoopnPOST-TEST loop (exit-condition)nThe looping condition is tested after executing the loop body.nLoop body is always executed at least once.nPRE-TEST loop (entry-condition)nThe looping condition is tested before

5、executing the loop body.nLoop body may not be executed at all.7/26/2024C+程序设计 教师:9Do-While Loop p326When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the do-while statement.Statement ExpressionDOWHILEFALSETRUE7/26/2024C+程序设计 教师:1

6、0A Count-Controlled LoopSYNTAXfor ( initialization ; test expression ; update ) 0 or more statements to repeat7/26/2024C+程序设计 教师:11The for loop containsan initializationan expression to test for continuingan update to execute after each iteration of the body7/26/2024C+程序设计 教师:12Example of Repetition

7、int num;for ( num = 1 ; num = 3 ; num+ ) cout num “Potato” 0 ; count- ) cout count endl;cout “Done” endl;Count-controlled LoopOUTPUT: 4321Done7/26/2024C+程序设计 教师:27What is output?int count;for ( count = 0 ; count 10 ; count+ ) cout “*” ;7/26/2024C+程序设计 教师:28OUTPUT*7/26/2024C+程序设计 教师:29What output fro

8、m this loop?int count;for (count = 0; count 10; count+) ; cout “*” ;7/26/2024C+程序设计 教师:30nno output from the for loop! Why?nthe ; right after the ( ) means that the body statement is a null statementnin general, the Body of the for loop is whatever statement immediately follows the ( )nthat statemen

9、t can be a single statement, a block, or a null statement nactually, the code outputs one * after the loop completes its counting to 10 OUTPUT7/26/2024C+程序设计 教师:31Several Statements in Body Blockconst int MONTHS = 12 ;int count ;float bill ; float sum = 0.0 ;for (count = 1; count = MONTHS; count+ )

10、cout bill ;sum = sum + bill ;cout “Your total bill is : “ sum endl ;nBook P3267/26/2024C+程序设计 教师:32Break Statementnbreak statement can be used with Switch or any of the 3 looping structures nit causes an immediate exit from the Switch, While, Do-While, or For statement in which it appears nif the br

11、eak is inside nested structures, control exits only the innermost structure containing it 7/26/2024C+程序设计 教师:33Continue Statementnis valid only within loops nterminates the current loop iteration, but not the entire loop nin a For or While, continue causes the rest of the body statement to be skippe

12、d-in a For statement, the update is done nin a Do-While, the exit condition is tested, and if true, the next loop iteration is begun 7/26/2024C+程序设计 教师:34Imagine using . . .na character, a length, and a width to draw a box, for example, nusing the values &, 4, and 6 would display&7/26/2024C+程序设计 教师:

13、35Write prototype for void functioncalled DrawBox ( ) with 3 parameters. The first is type char, the other 2 are type int.void DrawBox( char, int , int );7/26/2024C+程序设计 教师:36void DrawBox(char what, int down, int across) / 3 function parameters int row, col; / 2 local variablesfor ( row = 0; row dow

14、n; row+ )for (col = 0; col across; col+ )cout what;cout endl;return;7/26/2024C+程序设计 教师:37#include void DrawBox (char, int, int); / prototypeint main ( ) char letter = &; DrawBox(letter, 4, 2*3); / arguments DrawBox(V, 9, 3); / appear in call return 0;THE DRIVER PROGRAM7/26/2024C+程序设计 教师:38Quick Chec

15、kv p3397/26/2024C+程序设计 教师:39TRUE/FALSE A For loop is a posttest loop, whereas a While loop is a pretest loop. A)TrueB)False7/26/2024C+程序设计 教师:40TRUE/FALSEvFALSE7/26/2024C+程序设计 教师:41TRUE/FALSE The body of a Do-While loop will always execute at least once, whereas the body of a For loop may never exec

16、ute. A)TrueB)False7/26/2024C+程序设计 教师:42TRUE/FALSEvTRUE7/26/2024C+程序设计 教师:43TRUE/FALSE The code segment cout response; while (response != Y & response != N) cout response; is equivalent to the following code segment. do cout response; while (response != Y & response != N); A)TrueB)False 7/26/2024C+程序

17、设计 教师:44TRUE/FALSEvTRUE7/26/2024C+程序设计 教师:45CHOICE What is the output of the following code fragment if the input value is G? cin inputChar; switch (inputChar) case A : cout 1; break; case Q : cout 2; break; case G : case M : cout 3; break; default : cout num; switch (num) case 3 : alpha+; case 4 :

18、alpha = alpha + 2; case 8 : alpha = alpha + 3; default : alpha = alpha + 4; cout alpha endl; A)10 B)14 C)12 D)19 7/26/2024C+程序设计 教师:50CHOICEvD7/26/2024C+程序设计 教师:51CHOICE To produce the output 2 4 6 8 10, what is the loop condition for the following loop? n = 0; do n = n + 2; cout n ; while ( ? ); A)

19、n = 10 B)n 10 C)n = 2 7/26/2024C+程序设计 教师:52CHOICEv B7/26/2024C+程序设计 教师:53CHOICE What is the output of the following code fragment? (beta is of type int.) beta = 5; do switch (beta) case 1 : cout R; break; case 2 : case 4 : cout O; break; case 5 : cout 1); cout X; A)X B)ROOLX C)LOOX D)LOORX 7/26/2024

20、C+程序设计 教师:54CHOICEv C7/26/2024C+程序设计 教师:55CHOICE How many iterations does the following For loop execute? (count is of type int.) for (count = 0; count = 20; count+) DoSomething(); A)18 B)19 C)20 D)21 E)infinitely many 7/26/2024C+程序设计 教师:56CHOICEv D7/26/2024C+程序设计 教师:57CHOICE What is the loop postco

21、ndition (an assertion that is true immediately after the loop exit) for the following loop? for (i = 1; i = 30; i+) cout i endl; / Assert: ? A)/ Assert: 1, 2, ., 29 have been output & i = 29 B)/ Assert: 1, 2, ., 29 have been output & i = 30 C)/ Assert: 1, 2, ., 30 have been output & i = 30 D)/ Assert: 1, 2, ., 30 have been output & i = 31 E)/ Assert: 1, 2, ., 31 have been output & i = 31 7/26/2024C+程序设计 教师:58CHOICEvD7/26/2024C+程序设计 教师:59上机课作业预习nChapter 9n27.Book P342-343 2,12n28.Programming Problems Book P345 5,6(两个题写一个答案即可)7/26/2024C+程序设计 教师:60课后在线平台评测WWW.ECNUCPP.COM

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 高等教育 > 研究生课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号