Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.

Similar presentations


Presentation on theme: "1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements."— Presentation transcript:

1

2 1 1 Additional Control Structures Chapter 9

3 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements

4 3 Branching Statements Recall the current branching capability provided by the if ( … ) statement n Only branches two ways n What if we desire multiway branching? ? stmt 1 stmt 2 FalseTrue

5 4 switch (choice) { case 1 : do_option_one(); break; case 2 : case 3 : do_2_3_a (); do_2_3_b (); break; default : do_something_else (); } Multiway Branching C++ provides the switch statement ? stmt 1 stmt n stmt 2...

6 5 Switch Statement n Value of the switch expression matched with one of the labels attached to a branch n The statement(s) with the match get executed switch (choice) { case 1 : do_option_one(); break; case 2 : case 3 : do_2_3_a (); do_2_3_b (); break; default : do_something_else (); }

7 6 Switch Statement n Switch expression => the expression in parentheses whose value determines which switch label is selected –cannot be floating point –usually is int or char Identifiers following case must be constants switch (choice) { case 1 : do_option_one(); break; case 2 : case 3 : do_2_3_a (); do_2_3_b (); break; default : do_something_else (); }

8 7 Switch Statement The break causes control to be shifted to first statement after the switch statement the default statement is executed if the value of the switch expression is NOT found among switch labels switch (choice) { case 1 : do_option_one(); break; case 2 : case 3 : do_2_3_a (); do_2_3_b (); break; default : do_something_else (); } // next statement switch (choice) { case 1 : do_option_one(); break; case 2 : case 3 : do_2_3_a (); do_2_3_b (); break; default : do_something_else (); } // next statement

9 8 A Different Looping Statement Recall that the while ( … ) statement always checked the condition BEFORE the loop n In certain situations we wish to check the condition at the END of the loop while (condition) statement 1 statement 2 true false

10 9 The Do-While Statement n Condition tested at end/bottom of loop n Guarantees loop body executes at least once true while (condition) statement 1 statement 2 false

11 10 The do-while Illustrated Loop Entry Loop Iteration Loop Exit Loop Trest Note: always at least one iteration

12 11 The Do-While Statement n Can be used for a counting loop What gets printed?? How do you change it to go 10 times?

13 12 The Do While Statement n This logic sometimes more suitable for some algorithms n Example : trap for valid input do { cout “; cin >> value; if (value 5) cout 5); What makes this easier than the while ( … ) loop for this task?

14 13 Do-While Loop vs. While Loop n POST-TEST loop (exit-condition) n The looping condition is tested after executing the loop body. n Loop body is always executed at least once. n PRE-TEST loop (entry-condition) n The looping condition is tested before executing the loop body. n Loop body may not be executed at all.

15 14 A Special Count Controlled Loop The for ( … ) statement –Initialization : initializes the LCV –Condition : usually a comparison, acts like a while ( …) –Incrementing statement : LCV is incremented (or decremented) for ( initialization ; test expression ; update ) { 0 or more statements to repeat } for ( initialization ; test expression ; update ) { 0 or more statements to repeat }

16 15 The for ( … ) Loop n Example -- what gets printed? x gets initialized value of x inspected x gets incremented How did it Happen?

17 16 The for ( … ) Loop n All three of the portions inside the parentheses can be multiple statements separated by commas n Any or all of the three portions inside the parenthesis may be missing –accomplish those tasks some other way –the two semicolons MUST be there

18 17 The break ; Statement We saw it in the switch statement n Causes immediate exit from innermost block –switch, while, do-while, for n Can be used to break out of purposely designed infinite loop –not a good idea … a lazy shortcut n Use only as last resort to avoid baffling combinations of multiple Boolean flags and nested ifs

19 18 The continue ; Statement n Valid only in loops n Terminates current loop iteration –NOT entire loop n Causes branch to bottom of loop –skips rest of loop statements n Loop then prepares for next iteration –for (…) would increment lcv –all loops would check condition

20 19 Guidelines for Choosing a Looping Statement n Simple count-controlled –use for (…) loop n Event controlled, body always executed at least once –use do-while n Event controlled and nothing known about first execution –use while, possibly for When in doubt => use while

21 20 Testing and Debugging n For do-while loops, make sure to try data sets to make loop go exactly one time n For data-dependant loop where expressions based on values other than constants –make sure to test for proper number of iterations n Make sure switch statements have every branch tested –including default

22 21 Testing and Debugging n Remember to use break at end of case alternatives in switch statements –otherwise next case is also executed! n Case labels in switch statement must be values or named constants -- no variables n Both switch expression & case constant cannot be floating point n Provide default for switch when possibility of case values not being matched

23 22 Testing and Debugging n Make sure all needed switch cases are present n Choose looping structure carefully for( ; ; ) loop heading must have two semicolons -- even if portions omitted n Break statement can exit only one level of nesting –innermost switch or loop where break is located


Download ppt "1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements."

Similar presentations


Ads by Google