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

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 5: Loops and Files.
1 Chapter 7 Additional Control Structures. 2 Knowledge Goals Understand the role of the switch statement Understand the purpose of the break statement.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Control Structures II (Repetition)
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
Chapter 4 Program Control Statements
1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.
Control Structures – Selection Chapter 4 2 Chapter Topics  Control Structures  Relational Operators  Logical (Boolean) Operators  Logical Expressions.
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
1 Additional Control Structures. 2 Chapter 9 Topics  Switch Statement for Multi-way Branching  Do-While Statement for Looping  For Statement for Looping.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
1 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX do { Statement } while.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
Control Structures II (Repetition). Objectives In this chapter you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Chapter 2 Flow of Control. Learning Objectives Boolean Expressions – Building, Evaluating & Precedence Rules Branching Mechanisms – if-else – switch –
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Control Structures Repetition or Iteration or Looping Part II.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
1 do-while Statement 2 Do-While Statement Is a looping control structure in which the loop condition is tested after each iteration of the loop. SYNTAX.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
1 Looping Chapter 6 2 Getting Looped in C++ Using flags to control a while statement Trapping for valid input Ending a loop with End Of File condition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Control Structures II (Repetition)
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Control Structures – Selection
Control Structures - Repetition
Additional Control Structures
Chapter 7 Additional Control Structures
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
CSC215 Lecture Control Flow.
Presentation transcript:

1 1 Additional Control Structures Chapter 9

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

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

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...

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 (); }

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 (); }

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

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

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

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

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?

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?

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.

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 }

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

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

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

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

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

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

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

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