1 CS100J September 27, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.

Slides:



Advertisements
Similar presentations
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
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.
Executes a statement or statements for a number of times – iteration. Syntax for(initialize; test; increment) { // statements to be executed } Initial.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
CS100J October 07, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
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.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Loops – While, Do, For Repetition Statements Introduction to Arrays
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
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Looping Yong Choi School of Business CSU, Bakersfield.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
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:
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 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 4: Control Structures 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.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
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 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
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 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.
Catie Welsh February 9,  Friday - No Lab! ◦ Bring questions on Project 2  Lab 3 due on Friday 2.
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.
Arrays and Loops. Learning Objectives By the end of this lecture, you should be able to: – Understand what a loop is – Appreciate the need for loops and.
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Start reading Sec and chapter 7 on loops
Introduction To Repetition The for loop
CiS 260: App Dev I Chapter 4: Control Structures II.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
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
Java I.
CS October 2008 The while loop and assertions
Outline Altering flow of control Boolean expressions
Lecture Notes – Week 3 Lecture-2
Summary Two basic concepts: variables and assignments Basic types:
Asymptotic complexity Searching/sorting
CHAPTER 6: Control Flow Tools (for and while loops)
(We’ll spend a few lectures on iteration)
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.
Presentation transcript:

1 CS100J September 27, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt a saint.” Shakespeare, Henry IV, Pt I, 1 ii “Use not vain repetition, as the heathen do.” Matthew V, 48 Your “if” is the only peacemaker; much virtue if “if”. Shakespeare, As You Like It. Start reading chapter 7 on loops. The lectures on the ProgramLive CD can be a big help.

2 The while loop System.out.println(5*5); System.out.println(6*6); System.out.println(7*7); System.out.println(8*8); int k= 5; while ( k != 9) { System.out.println(k*k); k= k+1; } To execute the while loop: (1)Evaluate condition k != 9; if false, stop execution. (2) Execute the repetend. (3) Repeat again from step (1). Repetend: the thing to be repeated. The block: { … }

3 The while loop int k= 5; while ( k != 9) { System.out.println(k*k); k= k+1; } To execute the while loop: (1)evaluate condition k != 9; if it is false, stop execution. (2) Execute the repetend. (3) Repeat again from step (1). Trace execution of the loop: Section shows you how to “trace” execution of a loop, showing the values of variables as you go. STUDY THIS SECTION!

4 The while loop: syntax while ( ) while ( { sequence of declarations and statements } : a boolean expression. : a statement. BUT: We always make the a block.

5 Understanding assertions x is the sum of 1..n h..k is the set of values h, h+1, h+1, …, k. Example: 3..6 is 3, 4, 5, 6. In this notation, we require h ≤ k–1. h..h–1 is the empty set. What value of x or n makes this assertion true? n 1 xn 3 xn 4 xn 0 xn x 0 n x What’s the simplest solution to this? Make the range 1..n as small as possible.

6 Understanding assertions d is the number of days before month m What value of d makes this assertion true? d m 1 h..k is the set of values h, h+1, h+1, …, k. In this notation, we require h ≤ k–1. h..h–1 is the empty set. m?d ? // { d is the number of days before month m and m is in } m= m + 1; // { d is the number of days before month m and m is in } Below, write in English what has to be done to d so that the final assertion is true. Make it a COMMAND to do something

7 Understanding assertions d is the number of days before month m What value of m makes this assertion true? d m h..k is the set of values h, h+1, h+1, …, k. In this notation, we require h ≤ k–1. h..h–1 is the empty set. m?d ? // { d is the number of days before month m and m is in } m= m + 1; // { d is the number of days before month m and m is in } Below, write in English what has to be done to d so that the final assertion is true. Make it a COMMAND to do something 31

8 Understanding assertions b = “some value in 5..9 divides x” h..k is the set of values h, h+1, h+1, …, k. In this notation, we require h ≤ k–1. h..h–1 is the empty set. What value of x makes this assertion true? x 16 b x 10 b x 1 b false k= ; b= ; // { b = “some value in 2..k divides x” } b x ? k Below, fill in the assignments so that the assertion is true afterward.

9 Understanding assertions h..k is the set of values h, h+1, h+1, …, k. In this notation, we require h ≤ k–1. h..h–1 is the empty set. // { b = “some value in 2..k divides x” } k= k + 1 ; // { b = “some value in 2..k divides x” } bx ? k Fill in the space after the assignment to k with an if-statement so that the assertion remains true after execution. Make up an example, if it helps, but learn to work in this more abstract setting ? ?

10 Understanding assertions // { nothing has been printed } k= ; // {squares of 5..k-1 printed} Fill in the assignments on this page so that the assertions following them are true. k= ; // {All chars in String s[0..k–1] are ‘$’} // { k ≥ 1 } k= ; c= ; // // {c is the smallest character in s[0..k–1]} Hint: make the range 0..k–1 as small as possible

11 Understanding assertions Suppose this assertion is true: x = sum of 1..k Under what extra condition is this one true? x = sum of 1..n Put your answer here Suppose this assertion is true: x = sum of h..10 Under what extra condition is this one true? x = sum of Put your answer here Suppose this assertion is true: no value in h..k divides x Under what extra condition is this one true? no value in 2..n–1 divides x Put your answer here

12 Understanding assertions This is an assertion about v and k. It is true because chars of v[0..3] are greater than ‘C’ and chars of v[6..8] are ‘Z’s X Y Z X A C Z Z Z v This is a list of Characters v ≥ C ? all Z’s k k 8 v ≥ C ? all Z’s k k 8 v ≥ C all Z’s k 6 0 k 8 v ≥ W A C all Z’s k 4 0 k 8 Indicate whether each of these 3 assertions is true or false.