Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class More LoopsMore Loops Do While – a ‘trailing decision’ loopDo While.

Similar presentations


Presentation on theme: "March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class More LoopsMore Loops Do While – a ‘trailing decision’ loopDo While."— Presentation transcript:

1 March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class More LoopsMore Loops Do While – a ‘trailing decision’ loopDo While – a ‘trailing decision’ loop For loop – for doing a sequenceFor loop – for doing a sequence Accumulation, Sentinel valuesAccumulation, Sentinel values Tomorrow: lab using a count controlled loopTomorrow: lab using a count controlled loop

2 March 2005 2/18R. Smith - University of St Thomas - Minnesota Recap: Parts of a Program The “Envelope”The “Envelope” –Class definition, Main definition –We’ll just use them as “patterns” for now Variable declarationsVariable declarations –First thing in “Main” –First part of “setting up” your program to run –Some variables are initialized, not necessarily all of them –Each has a comment explaining its purpose Executable StatementsExecutable Statements –BLANK LINE separates them from variable declarations –These are things that “run” when your program runs –Clusters of 2-5 with a comment at the start of each cluster –Indentation of Ifs and Loops

3 March 2005 3/18R. Smith - University of St Thomas - Minnesota Parts of a loop Statements “Outside” The “Test condition”Statements “Outside” The “Test condition” –“Statements Inside” “More Statements Outside”“More Statements Outside”

4 March 2005 4/18R. Smith - University of St Thomas - Minnesota Do While A “Trailing Decision” LoopA “Trailing Decision” Loop do {do { –“statements to execute” while (condition-remains-true);while (condition-remains-true); Loop statements are performed onceLoop statements are performed once Then repeated as long as the condition remains trueThen repeated as long as the condition remains true

5 March 2005 5/18R. Smith - University of St Thomas - Minnesota Leading vs Trailing decision Error messages and input loopsError messages and input loops Read inputRead input If error, repeatIf error, repeat –Print error message –Prompt for another input RepeatRepeat –Prompt for input, giving rules –Read input Until input is validUntil input is valid

6 March 2005 6/18R. Smith - University of St Thomas - Minnesota The For Loop “Count controlled loop”“Count controlled loop” –Used to apply a set of operations a fixed number of times Traditionally applied to lists and arraysTraditionally applied to lists and arrays –(we haven’t studied those yet) for (initial-stmt; test-stmt; update-stmt) {for (initial-stmt; test-stmt; update-stmt) { –“statements to perform” } Usually the loop header has a variable that indicates which iteration you are onUsually the loop header has a variable that indicates which iteration you are on

7 March 2005 7/18R. Smith - University of St Thomas - Minnesota Accumulating Results We already did that in the Payroll exampleWe already did that in the Payroll example The idea: repeating an operation several times,The idea: repeating an operation several times, Accumulating partial results as you proceedAccumulating partial results as you proceed

8 March 2005 8/18R. Smith - University of St Thomas - Minnesota Sentinel Values Example: program that averages a bunch of positive numbersExample: program that averages a bunch of positive numbers Input: collects one number at a timeInput: collects one number at a time Once a negative number arrives, average the previously collected onesOnce a negative number arrives, average the previously collected ones

9 March 2005 9/18R. Smith - University of St Thomas - Minnesota Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "March 2005 1/18R. Smith - University of St Thomas - Minnesota QMCS 230: Today in Class More LoopsMore Loops Do While – a ‘trailing decision’ loopDo While."

Similar presentations


Ads by Google