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.

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
CS0007: Introduction to Computer Programming
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Repeating Actions While and For Loops
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Control Structures II (Repetition)
CS 106 Introduction to Computer Science I 02 / 11 / 2008 Instructor: Michael Eckmann.
Intro to Java while loops pseudocode. 1 A “Loop” A simple but powerful mechanism for “making lots of things happen!” Performs a statement (or block) over.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
New Mexico Computer Science For All More Looping in NetLogo Maureen Psaila-Dombrowski.
Python quick start guide
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Introduction to Computer Programming Counting Loops.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Loops and Iteration for Statements, while Statements and do-while Statements.
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
CS 100 Introduction to Computing Seminar October 7, 2015.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Counting Loops.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
ITP © Ron Poet Lecture 6 1 More on if. ITP © Ron Poet Lecture 6 2 Remembering Tests  We often want to remember the result of a test, so that we can use.
GCSE Computing: Programming GCSE Programming Remembering Python.
ITP © Ron Poet Lecture 5 1 Branching. ITP © Ron Poet Lecture 5 2 CookTime After Midnight  We want to improve our program so that we can cook meals after.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
PHY 107 – Programming For Science. Today’s Goal  How to use while & do / while loops in code  Know & explain when use of either loop preferable  Understand.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
FOP: While Loops.
Whatcha doin'? Aims: To start using Python. To understand loops.
Chapter 4 Repetition Statements (loops)
Lecture 6 Repetition Richard Gesick.
Incrementing ITP © Ron Poet Lecture 8.
Lecture 07 More Repetition Richard Gesick.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Looping and Repetition
Iteration with While You can say that again.
Outline Altering flow of control Boolean expressions
Lecture Notes – Week 3 Lecture-2
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Repetition Structures
Repetition Statements (Loops) - 2
Building Java Programs
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Looping Structures.
while Loops Looping Control Statement
Presentation transcript:

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 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.

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%,...

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

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.

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;

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.

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;

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”);

ITP © Ron Poet Lecture 7 10 Running The Program

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.

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.