CS100J 16 Oct, 2007 Assignment A5: loops

Slides:



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

While loops.
08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
© Trainer Bubble. Cryptic anagram puzzles (the clues are in the questions): dirty room here come dots lost.
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.
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.
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
Loops – While, Do, For Repetition Statements Introduction to Arrays
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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:
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
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.
Chapter 05 (Part III) Control Statements: Part II.
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.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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 18 October 2005 Arrays Reading: You are responsible for: Secs 8.1, 8.2, 8.3, 8.4 A decimal point I'm a dot in placeSlot machines Cash lost in.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
COMP Loop Statements Yi Hong May 21, 2015.
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.
1 CS100J 29 March 2005 Arrays Reading: You are responsible for: Secs 8.1, 8.2, 8.3, 8.4 A decimal point I'm a dot in placeSlot machines Cash lost in 'em.
C Language 1 Program Looping. C Language2 Topics Program looping Program looping Relational operators / expressions Relational operators / expressions.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
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.
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.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
1 C++ Programming C++ Basics ● C++ Program ● Variables, objects, types ● Functions ● Namespace ● Tests ● Loops ● Pointers, references.
Repetition Statements
Correctness issues and Loop invariants
Start reading Sec and chapter 7 on loops
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.
Programming Fundamentals
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.
Developing Loops from Invariants
Arrays, For loop While loop Do while loop
Looping and Repetition
CS October 2008 The while loop and assertions
Outline Altering flow of control Boolean expressions
Testing change to a linked list
Introduction to Object-Oriented Programming with Java--Wu
Chapter (3) - Looping Questions.
© A+ Computer Science - What is a LOOP? © A+ Computer Science -
Chapter 6: Repetition Statements
Loops.
Repetition Statements (Loops) - 2
COMPUTER PROGRAMMING SKILLS
PROGRAM FLOWCHART Iteration Statements.
Developing loops from invariants
Comparing Python and Java
CS100A Sections Dec Loop Invariant Review C Review and Example
Developing Loops from Invariants
Looping and Repetition
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.
Presentation transcript:

CS100J 16 Oct, 2007 Assignment A5: loops Read: Sec. 2.3.8 and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm a dot in place Animosity Is no amity Debit card Bad credit Desperation A rope ends it Dormitory Dirty room Funeral Real fun Schoolmaster The classroom Slot machines Cash lost in 'em Statue of liberty Built to stay free Snooze alarms Alas! No more Z's The Morse code Here come dots Vacation times I’m not as active Western Union No wire unsent George Bush He bugs Gore Parishioners I hire parsons The earthquakes That queen shake Circumstantial evidence Can ruin a selected victim Victoria, England’s queen Governs a nice quiet land Eleven plus two Twelve plus one (and they have 13 letters!)

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; } Iteration: 1 execution of repetend i= 2; 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. // invariant The invariant is an assertion about the variables that is true before and after each iteration (execution of the repetend) i <= 4 true x= x + i*i; false i= i +1;

If k is the next integer to process, which ones have been processed? a..k a..k–1 None of these // Process integers in a..b // invariant: integers in a..k–1 have been processed for (int k= a; k <= b; k= k + 1) { Process integer k; } Command to do something and equivalent post-condition // post: the integers in a..b have been processed Loop invariant says which integers have been processed (and what that means). It is true before and after each iteration. Iteration: 1 execution of repetend invariant: unchanging

// Store in double variable v the sum Finding an invariant: something that is true before and after each iteration (execution of the repetend). // Store in double variable v the sum // 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + … + 1/n // for (int k= 1; k <= n; k= k +1) { Process k } // v =1/1 + 1/2 + … + 1/n Command to do something and equivalent postcondition invariant: v = 1/1 + 1/2 + … + 1/(k–1) What is the invariant? v = 1/1 + 1/2 + … + 1/n v = 1/0 + 1/1 + … + 1/k v = 1/0 + 1/1 + … + 1/(k–1) v = 1/1 + 1/1 + … + 1/(k–1) None of these

Find invariant: true before and after each iteration // set x to no. of adjacent equal pairs in s[0..s.length()-1] Command to do something and equivalent post-condition // invariant: for (int k= 0; k < s.length(); k= k +1) { Process k } // x = no. of adjacent equal pairs in s[0..s.length()-1] x = no. of adjacent equal pairs in s[0..k-1] for s = ‘ebeee’, x = 2. k: next integer to process. Which ones have been processed? 0..k C. a..k 0..k–1 D. a..k–1 E. None of these What is the invariant? x = no. adj. equal pairs in s[1..k] x = no. adj. equal pairs in s[0..k] x = no. adj. equal pairs in s[1..k–1] x = no. adj. equal pairs in s[0..k–1] None of these

// { String s has at least 1 char } 1. What is the invariant? Being careful Command postcondition // { String s has at least 1 char } // Set c to largest char in String s // inv: for (int k= ; k < s.length(); k= k + 1) { // Process k; } // c = largest char in s[0..s.length()–1] 2. How do we initialize c and k? k= 0; c= s.charAt[0]; k= 1; c= s.charAt[0]; k= 1; c= s.charAt[1]; k= 0; c= s.charAt[1]; None of the above c is largest char in s[0..k–1] An empty set of characters or integers has no maximum. Therefore, be sure that 0..k–1 is not empty. Therefore, start with k = 1.

Methodology for developing a for-loop Recognize that a range of integers b..c has to be processed Write the command and equivalent postcondition. 3. Write the basic part of the for-loop. 4. Write loop invariant, based on the postcondition. Figure out any initialization. Initialize variables (if necessary) to make invariant true. 6. Implement the repetend (Process k). // Process b..c // Postcondition: range b..c has been processed // Invariant: range b..k-1 has been processed for (int k= b; k <= b; k= k+1) { // Process k }

The while loop syntax and semantics while ( <condition> ) <repetend> while (<condition> { sequence of declarations and statements } <condition>: a boolean expression. <repetend>: a statement. BUT: We always make the <repetend> a block. condition false true repetend

for-loop and equivalent while-loop // Sum the squares of 5..60 int x= 0; // inv: x = sum of squares of 5..k-1 for (int k= 5; k <= 60; k= k+1) { // Process k x= x + k*k; } // x = sum of squares of 5..60 // Sum the squares of 5..60 int x= 0; int k= 5; // inv: x = sum of squares of 5..k-1 while (k <= 60) { // Process k x= x + k*k; k= k + 1; } // x = sum of squares of 5..60