(We’ll spend a few lectures on iteration)

Slides:



Advertisements
Similar presentations
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Advertisements

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.
CS100J October 07, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Copyright © Texas Education Agency, Computer Programming For Loops.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
CIS Computer Programming Logic
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:
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Repetition Statements while and do while loops
ADMIT TICKET WHAT DOES THIS OUTPUT? double y = 2.5; int x = 6 / (int) y; System.out.println(“x = “ + x);
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
1 CS100J September 27, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
Chapter 9 Repetition.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
CS100A, 10 Sept Lecture 4: Continue discussion of classes and introduces Class String, Printing output using System.out.print, System,out.println,
Start reading Sec and chapter 7 on loops
GC211Data Structure Lecture2 Sara Alhajjam.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
(Tuesday, 20 October, 7:30-9PM) Encryption-Decryption
Lecture 4: Program Control Flow
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Topic 5 for Loops -Arthur Schopenhauer
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5 Repetition.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
CS October 2008 The while loop and assertions
Outline Altering flow of control Boolean expressions
LRobot Game.
Lecture Notes – Week 3 Lecture-2
Introduction to Object-Oriented Programming with Java--Wu
Chapter (3) - Looping Questions.
Algorithm Discovery and Design
Control Statements Loops.
Lab5 PROGRAMMING 1 Loop chapter4.
Computer Science Core Concepts
Building Java Programs
Asymptotic complexity Searching/sorting
Concepts for this lecture: Class Instance of a class, (an object)
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Building Java Programs
Repetition Statements (Loops) - 2
Control Statements Loops.
PROGRAM FLOWCHART Iteration Statements.
Javascript Chapter 19 and 20 5/3/2019.
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.
CS100A Sections Dec Loop Invariant Review C Review and Example
Looping Structures.
Presentation transcript:

(We’ll spend a few lectures on iteration) CS100A, Lecture 8, 24 Sept 1997 Iteration (We’ll spend a few lectures on iteration) Reading: Holmes, Chapter 4, p. 112-140 Iterate: Reiterate Reiterate: to say or do over again or repeatedly; repeat often or continually, sometimes with a wearying effect. Iteration: the action of repeating or reiterating: repetition, reiteration. CS100A, Lecture 8, 24 Sept. 1998

Views on Iteration, Repetition Like warmed-up cabbage served at each repast, The repetition kills the wretch at last. Juvenal, Satires Use not vain repetitions, as the heathens do. Matthew vi, 7 Oh, thou hast damnable iteration, and art indeed able to corrupt a saint. Shakespeare, Henry IV Iteration, like friction, is likely to generate heat instead of progress. George Eliot, The Mill on the Floss A languid, leaden iteration reigns, And ever must, o’er those whose joys are joys Of sight, smell, taste Young, Night Thoughts CS100A, Lecture 8, 24 Sept. 1998

// Write out the numbers 0..9, each on a // separate line System.out.println(0); System.out.println(1); System.out.println(2); System.out.println(3); System.out.println(4); System.out.println(5); System.out.println(6); System.out.println(7); System.out.println(8); System.out.println(9); The while loop allows us to do this more tersely. i= 0; while (i<10) { System.out.println(i); i= i+1; } CS100A, Lecture 8, 24 Sept. 1998

Syntax of the Java while loop: while ( <boolean expression> ) statement called the “body” called the of the loop “condition” Example: i= 0; // General picture: integers 0..i-1 have been // printed while (i<10) { System.out.println(i); i= i+1; } CS100A, Lecture 8, 24 Sept. 1998

Execution of the while loop while ( B ) flowchart S Execution of a while loop: Evaluate condition B; if it is true, execute body S and repeat the process. The first execution of S is called iteration 0. The second execution of S is called iteration 1. The third execution of S is called iteration 2. ... Evaluate B Execute S true false CS100A, Lecture 8, 24 Sept. 1998

// General picture: s= 1 + 2 + … + i-1. while (i != 5) Another example: // Store 1 + 2 + 3 + 4 in s s= 0; i= 1; // General picture: s= 1 + 2 + … + i-1. while (i != 5) {s= s + i; i= i+1;} Execution: i | ? | 1 | | 2 | | 3 | | 4 | | 5 | s | 0 | | 1 | | 3 | | 6 | | 10 | | Execute on computer. CS100A, Lecture 8, 24 Sept. 1998

s= 0; i= 1; // Initialization // General picture: s = 1 + 2 + … + i-1. // Store 1 + 2 + 3 + 4 in s s= 0; i= 1; // Initialization // General picture: s = 1 + 2 + … + i-1. while (i != 5) {s= s + i; i= i+1;} The “General picture”, or “invariant” is a relation that shows how the values of the variables are related just before and just after each iteration. It gives the meaning of the variables before and after each iteration. It helps us understand what the loop is doing. For each loop, write an invariant, which shows the state of affairs before and after each iteration. The invariant gives the meaning of the variables during execution of the loop. The initialization “truthifies” the invariant (makes it true). Each iteration “maintains” the invariant (keeps it true). Condition is written so that when the loop terminates, the general picture helps us see that the desired answer has been obtained. CS100A, Lecture 8, 24 Sept. 1998

Note the indentation used below: 1 // Store 1 + 2 + 3 + 4 in s 2 s= 0; i= 1; 3 // Invariant: s = 1 + 2 + … + i-1 4 while (i != 5) { 5 s= s + i; 6 i= i+1; 7 } Line 1 is a statement-comment: a statement to be executed that it is written in English, so it is a comment. The statement-comment says WHAT to do. Lines 2-7 are the implementation of the statement comment. They are indented beneath it. Lines 5-7 are the body of the while loop; they form a substatement of the loop. They are indented. Rule: Indent substatements of a statement! CS100A, Lecture 8, 24 Sept. 1998

Develop another algorithm for same task // Store 1 + 2 + 3 + 4 in s Use invariant s= 0 + 1 + 2 + … + i. To make it true, use initialization: s= 0; i= 0; When will s = 1+2+3+4 be true? When i=4. Use condition i != 4 Body of loop: Make “progress” by increasing i and changing s to maintain the invariant: i= i+1; s= s+i; Loop: s= 0; i= 0; // Invariant: s = 1+2+3+4 while (i !=4) {i= i+1; s= s+i;} Invariant is different, so loop is different! CS100A, Lecture 8, 24 Sept. 1998

// Print the factors of integer n int i= 1; Another Example: (Assume that n has been declared and assigned a variable before the following program segment is to be executed.) // Print the factors of integer n int i= 1; // Invariant: the factors of n that are less than // i have been printed, and 1 <= i <= n+1 while (i <= n) { if (n % i == 0) System.out.println(i); i= i+1; } x % y yields the remainder when x is divided by y. Example: 13 % 1 = 0 13 % 2 = 1 13 % 3 = 1 13 % 5 = 3 CS100A, Lecture 8, 24 Sept. 1998

MEMORIZE THE DEFINITIONS OF THESE TERMS! Iterate. Slide 1 While loop. Slide 3 Body of a while loop. Slide 3 Condition of a while loop. Slide 3 How to execute a while loop. Slide 5 Iteration of a while loop. Slide 5 Invariant of a loop. Slide 7 A statement “truthifies” a relation. Slide 7 A statement “maintains” a relation. Slide 7 Statement-comment. Slide 8 Implementation of a statement-comment. Slide 8 CS100A, Lecture 8, 24 Sept. 1998

You MUST know precise meanings of following! Type (e.g. int, bool) PRELIM MONDAY EVENING You MUST know precise meanings of following! Type (e.g. int, bool) Class Subclass Field (of a class) or instance variable Static field (of a class) or class variable Instance of a class –also called an object Method function procedure constructor Local variable (of a method) Parameter (of a method) Method call –how each of the three kinds of methods is called Argument (of a method call) How to execute an assignment, if statement. How to execute a method call How to evaluate expression new C(…) CS100A, Lecture 8, 24 Sept. 1998