Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 1997-2015 Curt Hill Loop Types and Construction Logical types and construction hints.

Similar presentations


Presentation on theme: "Copyright © 1997-2015 Curt Hill Loop Types and Construction Logical types and construction hints."— Presentation transcript:

1 Copyright © 1997-2015 Curt Hill Loop Types and Construction Logical types and construction hints

2 Copyright © 1997-2015 Curt Hill Types The focus is on why we are using the loop, rather than what we construct it from Counting Sentinel Infinite Boolean

3 Copyright © 1997-2015 Curt Hill Counting loops Characterized by counting up or down by a value which is usually one Usually deterministic –We know the starting and ending values before we get started The count is usually an integer, but may have a real value Usually implemented with a for

4 Copyright © 1997-2015 Curt Hill Counting Loop Examples Read N values –Ask the user how many values to enter –Then read in that many –This also works for reading a file Compute N values of sequence –Factorial –Finding prime numbers less than N –Raise n to the m power

5 Copyright © 1997-2015 Curt Hill Sentinel Process data (usually in a file) until a sentinel value is encountered A sentinel is an invalid value that signals an end of processing Usual technique is to have: –a sequence of data –a sentinel –another sequence of data –another sentinel –etc.

6 Copyright © 1997-2015 Curt Hill Infinite A loop that never terminates –Stops when program is cancelled or computer powered down In this class it is usually a mistake, but there are many correct infinite loops in working programs Most controllers have Operating systems also contain Also the loop may appear to be infinite but have another exit

7 Copyright © 1997-2015 Curt Hill Boolean A boolean test exits the loop All loops can be reduced to boolean controlled loop –Count, sentinel, infinite and all variations The boolean controlled loop is usually implemented with a while or do, but the for is also boolean controlled

8 Copyright © 1997-2015 Curt Hill Loop Construction Loop construction is inside out compared to most other programming We usually start at the top and work to the bottom With loops we do things differently Hence we need to discuss Loop Construction Tips

9 Copyright © 1997-2015 Curt Hill Curt’s Loop Construction Tips There are five steps to loop coding: Code the business part Wrap the loop around it Code post-loop cleanup Code initialization Test three times

10 Copyright © 1997-2015 Curt Hill What is the Business Part? What you would do in sequence if you did not have a loop If you knew you would run the loop three times you could just code the set of statements three times You would not need –Counting of times through –An actual loop header –Perhaps other things

11 Copyright © 1997-2015 Curt Hill Code the Business Part The first step is to code that part of the body of the loop that accomplishes only what you intend –You will add the rest later Example: sentinel loop –Do not worry about the sentinel, advancement or a loop –Just read in one piece of data and process it

12 Copyright © 1997-2015 Curt Hill Wrap the loop around it Once you have the business part you need to ask some questions –Is this a well known type: counting, sentinel, etc? –What will be my exit condition? –How will the loop advance? When you understand these then answer this: –What is the best type of loop for this?

13 Copyright © 1997-2015 Curt Hill Wrapping Again Once the loop statement type is chosen: Finish up the advancement issues This is often called the housekeeping of the loop: –Where should the value be incremented? –What data to read? The business part, loop statement and housekeeping amount to the bulk of the loop Now we look at the code before and after the loop

14 Copyright © 1997-2015 Curt Hill Post Loop Cleanup Is there anything that needs to be done after the loop terminates to get things ready for the rest of the program? Often a variable has to be adjusted one way or another Loops that have boolean operators in the exit often need an if to find out why the loop was exited

15 Copyright © 1997-2015 Curt Hill Loop Initialization Only after the loop is complete and the post loop cleanup is done do we worry about initialization Set the values of all needed variables, now that they are all known –Especially the variables used in the loop test –A for often sets one of these but not usually all

16 Copyright © 1997-2015 Curt Hill Empty Steps? Some of the pieces of a loop may have no code A for often does its own initialization and occasionally has no body Often no post-loop cleanup is needed

17 Copyright © 1997-2015 Curt Hill Testing Test the loop manually three times Once the first time through the loop Once the last time through the loop Once any other time (often 2 nd time) Why? –First time catches initialization problems –Last time catches off by one errors –The any other time test advancement

18 Copyright © 1997-2015 Curt Hill Loop Errors Off by one –One too many or one too few –Most common loop error –Test was < when it should be <= –Many other easy to make errors Infinite loop –Advancement problem –Not changing the variables of test –Conflicting changes

19 Copyright © 1997-2015 Curt Hill Example Fibonacci GCF by Euclid Primes less than a value Multiple sets of class scores, each terminated by a negative score – find the averages


Download ppt "Copyright © 1997-2015 Curt Hill Loop Types and Construction Logical types and construction hints."

Similar presentations


Ads by Google