Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITP © Ron Poet Lecture 7 1 Repetition. ITP © Ron Poet Lecture 7 2 Easing Repetitive Tasks  Many computing task are repetitive.  Checking all known foods.

Similar presentations


Presentation on theme: "ITP © Ron Poet Lecture 7 1 Repetition. ITP © Ron Poet Lecture 7 2 Easing Repetitive Tasks  Many computing task are repetitive.  Checking all known foods."— Presentation transcript:

1 ITP © Ron Poet Lecture 7 1 Repetition

2 ITP © Ron Poet Lecture 7 2 Easing Repetitive Tasks  Many computing task are repetitive.  Checking all known foods when calculating the cook time.  Checking all numbers to find the maximum.  The type of calculation is the same in each case.  It is just the data that is different.

3 ITP © Ron Poet Lecture 7 3 Inflation Checker  Inflation gradually makes everything more expensive.  How many years of inflation at 2% would it take before prices double?  What about 3%, 4%,...

4 ITP © Ron Poet Lecture 7 4 Simple Program Design  Get inflation rate  Start with prices at 1.0  Start with year count at 0  Loop while prices < 2.0  Increase prices by inflation.  Increase year count by 1.  Print out year count

5 ITP © Ron Poet Lecture 7 5 Loop Design  The word loop denotes repetition.  Part of our program code is in a loop  It is repeated over and over again.  The inside of the loop is indented.  Note that the two variables, prices and year count are initialise before the loop starts.  Note the loop termination condition.

6 ITP © Ron Poet Lecture 7 6 Inflation Formula  The inflation rate is expressed as a percentage.  Convert to a decimal by dividing by 100.  Percent means each 100.  newPrice = oldPrice + inflation * oldPrice.  Just use the one variable for old and new price.  We don’t need to remember all the prices.  price = price + inflation * price;

7 ITP © Ron Poet Lecture 7 7 while Loop  The are several ways of writing loops in Java.  A while loop is the most fundamental. while (test) statement;  If the test is true then do the statement and then go back to the while.  If the test is false then the loop is finished.  The statement can be a compound statement.

8 ITP © Ron Poet Lecture 7 8 The Program // get inflation rate con.print(“Enter inflation rate: ); double infPerc = con.readDouble(); double inflation = infPerc / 100.0; // initialise loop variable int yearCount = 0; double prices = 1.0;

9 ITP © Ron Poet Lecture 7 9 The Program // loop while prices < 2.0 while (prices < 2.0) { price = price + inflation * price; yearCount = yearCount + 1; } // print out year count con.println(“Prices double in “ + yearCount + “years”);

10 ITP © Ron Poet Lecture 7 10 Running The Program

11 ITP © Ron Poet Lecture 7 11 Infinite Loops  Most loops must stop eventually.  An infinite loop is usually a programming error.  The following code is WRONG because real numbers are not usually exact. while (prices != 2.0)  The loop will not stop by itself.

12 ITP © Ron Poet Lecture 7 12 Stopping an Infinite Loop in Windows  Press CTL-ALT-DELETE and choose Task Manager.  Select the program you want to stop.  Eg Inflation.  Click End Task to stop it.


Download ppt "ITP © Ron Poet Lecture 7 1 Repetition. ITP © Ron Poet Lecture 7 2 Easing Repetitive Tasks  Many computing task are repetitive.  Checking all known foods."

Similar presentations


Ads by Google