Start reading Sec and chapter 7 on loops

Slides:



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

1 CS100J February 28, 2006 Loops, iterative statements, or repetitive statements A bit about graphics From news.com, on 23 February 2006, about browser.
1 CS March 2009 While loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim in two days: Th 7:30-9pm Uris Aud.
1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
CS100J October 07, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
1 CS100J October 06, 2005 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.
1 CS100J 15 March, 2006 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal.
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
1 Python Chapter 4 Branching statements and loops © Samuel Marateck 2010.
1 CS1110, 20 Oct. 2009, Lec 14 Elementary graphics; intro to loops and for-loops Reading: Sec and chapter 7 on loops. The lectures on the ProgramLive.
1 CS October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day:
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
1 CS1110, 8 March 2009 Two topics: elementary graphics (for A5); loops Reading: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
1 CS March 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
1 CS100J 4 March 2008 Two topics: Turtles; loops Start reading Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
Counting Loops.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
1 CS100J September 27, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
1 CS100J 06 March 2008 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
CS100J October 21, 2003 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
while Repetition Structure
Correctness issues and Loop invariants
Department of Computer Science
Lecture 6 Repetition Richard Gesick.
Chapter 4 Loops DDC 2133 Programming II.
Java's for Statement.
CS October 2009 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
CiS 260: App Dev I Chapter 4: Control Structures II.
Lecture 07 More Repetition Richard Gesick.
Python - Loops and Iteration
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Chapter 4 Control structures and Loops
Lecture 4A Repetition Richard Gesick.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Arrays, For loop While loop Do while loop
Iteration with While You can say that again.
CS October 2008 The while loop and assertions
Lecture Notes – Week 3 Lecture-2
Chapter (3) - Looping Questions.
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
Repetition Control Structure
Control structures to direct program flow
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
CSC115 Introduction to Computer Programming
CHAPTER 6: Control Flow Tools (for and while loops)
Building Java Programs
Chapter 4: Repetition Structures: Looping
Java LESSON 3 Loops.
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Developing loops from invariants
CS100A Lecture 25 1 December 1998 Chance to replace grade on Prelim 3, Question 2. Everyone is expected to be in lecture on Thursday, 3 December.
Chapter 13 Control Structures
CS100J 16 Oct, 2007 Assignment A5: loops
CS October 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
Chapter 4: Loops and Iteration
Presentation transcript:

CS100J 1 March, 2006 Loops, iterative statements, or repetitive statements Start reading Sec. 2.3.8 and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Learning without thought is labor lost. Thought without learning is perilous. or, alternatively, Study without reflection is a waste of time; reflection without study is dangerous. -- Confucius

The for loop, for processing a range of integers for (int i= 2; i <= 200; i= i +1) { x= x + i*i; } x= 0; // add the squares of ints // in range 2..200 to x x= x + 2*2; x= x + 3*3; … x= x + 200; loop counter: i initialization: int i= 2; loop condition: i <= 200; increment: i= i + 1 repetend or body: { x= x + i*i; } for each number i in the range 2..200, add i*i to x. repetend: the thing to be repeated. The block: { x= x + i*i; }

Execution of the for-loop for (int i= 2; i <= 4; i= i +1) { x= x + i*i; } loop counter: i initialization: int i= 2; loop condition: i <= 4; increment: i= i + 1 repetend or body: { x= x + i; } To execute the for-loop. Execute initialization. If loop condition is false, terminate execution. Execute the repetend. Execute the increment and repeat from step 2. i= 2; i <= 4 i= i +1; true false x= x + i*i; Called a “flow chart”

Execution of the for-loop for (int i= 2; i <= 4; i= i +1) { x= x + i*i; } loop counter: i initialization: int i= 0; loop condition: i <= 4; increment: i= i + 1 repetend or body: { x= x + i; } Trace execution of for-loop. We do it as shown below, rather than using a single box, for x and one for i, so that we can keep track of when events happened. x 4 13 29 i 2 3 5

The pattern for processing range of integers: range a..b-1 range c..d for (int k= a; k != b; k= k + 1) { Process integer k; } for (int i= c; i <= d; i= i + 1) { Process integer i; } // Print the integers in 10..n-1 // inv: All ints in 10..k-1 been printed for (int k= 10; k != n; k= k +1) { System.out.println(k); } // All ints in 10..n-1 been printed // Print the integers in 1..10 // inv: All ints in 10..i-1 printed for (int i= 1; i <= 10; i= i +1) { System.out.println(i); } // All ints in 10..i-1 printed

The pattern for processing range of integers: range a..b-1 range c..d for (int i= a; i != b; i= i + 1) { Process integer i; } for (int i= c; i <= d; i= i + 1) { Process integer i; } // Print the indices of all ‘e’s in String s // inv: Indices of ‘e’s in s[0..s.i-1] for (int i= 0; i != s.length(); i= i +1) { if (s.charAt(i) == ‘e’) System.out.println(i); } // Indices of ‘e’s in s[0..s.length()-1] // printed // Store in double variable v the sum // 1/1 + 1/2 + …+ 1/n v= 0; // inv: 1/1 + 1/2 + …+ 1/(i-1) for (int i= 1; i <= n; i= i +1) { v= v + 1.0 / i; } // v= 1/1 + 1/2 + …+ 1/n

A note on ranges. 2..5 contains 2, 3, 5. It contains 5+1 – 2 = 4 values 2..4 contains 2, 3, 4. It contains 4+1 – 2 = 4 values 2..3 contains 2, 3. It contains 3+1 – 2 = 2 values 2..2 contains 2. It contains 2+1 – 2 = 1 values 2..1 contains . It contains 1+1 – 2 = 0 values In the notation m..n, we require always, without saying it, that m–1 <= n . The number of values in m..n is n+1 – m. If m–1 = n, the range has 0 values./

Loops are often not easy to develop or understand. Our goal: Provide you with a methodolgy for the development of loops that process a range of integers. 1. Separate your concerns —focus on one thing at a time. 2. Make small steps toward completing the loop. 3. Don’t introduce a new variable without a good reason. 4. Keep program simple.

Development of a loop to process a range a..b for (int i= a; i <= b; i= i + 1) { Process integer i; } Follow this methodology for ease in writing loops!!! Step 1. Recognize that a range of integers has to be processed. // Store in m the sum of even // numbers in 10..46 m= 0; // m = sum of even ints in 10..46 Step 2. Write a postcondition, based on the spec, which says what is true at the end. Step 5. Write down, before the loop, what the variables mean. // m = sum of even ints in 10..(k-1) Step 3. Write the skeleton of the loop. for (int k= ; ; ) { // Process k } 10 k <= 46 k= k+1 Step 4. Fill in the loop control. Step 6. Write the method body (to process k). if (k % 2 == 0) { m= m + k; }

Development of a loop to process a range a..b for (int i= a; i >= b; i= i – 1) { Process integer i; } // Set c to the number of chars is String s that are digits 0..9

Try these problems. Develop them using the methodology given on slide 9. Then type them into DrJava and test them! 1. Set c to the number of chars is String s that are digits ( in 0..9). 2. Store in res a copy of String s but with no blanks. 3. Store in res a copy of String s but with adjacent duplicates removed. 4. Set boolean v to the value of “no integer in 2..n–1 divides x”. 5. Set boolean v to the value of “every element in Vector v is an object of class JFrame”. 6. Add up the squares of the odd integers in the range m..n.