Loop Types and Construction Logical types and construction

  • Slides: 18
Download presentation
Loop Types and Construction Logical types and construction hints Copyright © 1997 -2015 Curt

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

Types • The focus is on why we are using the loop, rather than

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

Counting loops • Characterized by counting up or down by a value which is

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 Copyright © 1997 -2015 Curt Hill

Counting Loop Examples • Read N values – Ask the user how many values

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 Copyright © 1997 -2015 Curt Hill

Sentinel • Process data (usually in a file) until a sentinel value is encountered

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. Copyright © 1997 -2015 Curt Hill

Infinite • A loop that never terminates – Stops when program is cancelled or

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 Copyright © 1997 -2015 Curt Hill

Boolean • A boolean test exits the loop • All loops can be reduced

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 Copyright © 1997 -2015 Curt Hill

Loop Construction • Loop construction is inside out compared to most other programming •

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 Copyright © 1997 -2015 Curt Hill

Curt’s Loop Construction Tips • • • There are five steps to loop coding:

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 Copyright © 1997 -2015 Curt Hill

What is the Business Part? • What you would do in sequence if you

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 Copyright © 1997 -2015 Curt Hill

Code the Business Part • The first step is to code that part of

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 Copyright © 1997 -2015 Curt Hill

Wrap the loop around it • Once you have the business part you need

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? Copyright © 1997 -2015 Curt Hill

Wrapping Again • Once the loop statement type is chosen: • Finish up the

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 Copyright © 1997 -2015 Curt Hill

Post Loop Cleanup • Is there anything that needs to be done after the

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 Copyright © 1997 -2015 Curt Hill

Loop Initialization • Only after the loop is complete and the post loop cleanup

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 Copyright © 1997 -2015 Curt Hill

Empty Steps? • Some of the pieces of a loop may have no code

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 Copyright © 1997 -2015 Curt Hill

Testing • • • Test the loop manually three times Once the first time

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 Copyright © 1997 -2015 Curt Hill

Loop Errors • Off by one – One too many or one too few

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 Copyright © 1997 -2015 Curt Hill