COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 1 Iterative Statements Concepts Covered: Iterative Statements (counter-based) Iterative Statements (sentinel-based) Serially Arranged Nested Part 1 Module3
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 2 Iterative Statements Concept: Iterative Statements (counter-based)
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 3 Counter-based Iteration Counter = Counter +1 Some more work To do after the loop Is Counter == 10 ? Counter = 1 Do some work YES TRUE NO FALSE LOOP
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 4 Counter-based Iteration Late Test Counter = Counter +1 Some more work To do after the loop Is Counter == 10 ? Counter = 1 Do some work YES TRUE NO FALSE LOOP
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 5 Counter-based Iteration Early Test Counter = Counter +1 Some more work To do after the loop Is Counter == 10 ? Counter = 1 Do some work YES TRUE NO FALSE LOOP
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 6 Counter-based Iteration middle Test Counter = Counter +1 Some more work To do after the loop Is Counter == 10 ? Counter = 1 Do some work LOOP
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 7 Iterative Statements Concept: Iterative Statements (sentinel-based)
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 8 Sentinel-based Iteration Display: “one more?” Some more work To do after the loop RESPONSE is “no” Do some work Read: RESPONSE LOOP
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 9 Sentinel-based Iteration Display: “one more?” Some more work To do after the loop RESPONSE is “no” Do some work Read: RESPONSE Late Test LOOP
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 10 Iteration (both kind) Some more work To do after the loop RESPONSE is “no” Do some work Early Test Display: “one more?” Read: RESPONSE Display: “one more?” Read: RESPONSE LOOP
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 11 Iteration (both kind) Display: “one more?” Some more work To do after the loop RESPONSE is “no” Do some work Read: RESPONSE Middle Test LOOP
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 12 Iterative Statements Concept: Serially Arranged Iterative Statements
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 13 Two separate loops one after the other Display: “one more?” Some more work to do after the loop RESPONSE is “no” Do some work Read: RESPONSE Display: “one more?” RESPONSE is “no” Do more some work Read: RESPONSE LOOP
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 14 Iterative Statements Concept: Nested Iterative Statements
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 15 Two nested loops one inside another Display: “one more?” Some more work to do after the loop RESPONSE is “no” Inner Loop Read: RESPONSE Display: “one more?” RESPONSE is “no” Do more some work Read: RESPONSE LOOP
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 16 “Real Life” Application of Nested Loops
COP 2510 Programming ConceptsAlessio Gaspar BSAS Industrial Operations 17 Design Choices Counter vs. Sentinel Early Test [0:N] iterations Late Test [1:N] iterations Conditional Infinite loops (e.g. counter based) Summary on using loops