University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops II Lecture 18, Mon Mar 1 2010

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
 Demonstrate use of a “for loop” in the design and development of a Java program to calculate the total of a one-dimensional array of 6 integers.  Design.
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.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Static Methods, Conditionals Lecture 15, Mon Feb 8.
CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 20, Fri Mar
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb
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
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops Lecture 12, Tue Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays Lecture 21, Mon Mar
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.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
The switch Statement, DecimalFormat, and Introduction to Looping
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.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
1 CS 177 Week 5 Recitation Slides Loops. 2 Announcements Project 2 due next Thursday at 9PM. Exam 1 this evening (switch and loop not covered) Old exams.
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.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
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.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Sophomore Scholars Java
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
Lecture 6 Repetition Richard Gesick.
The switch Statement, and Introduction to Looping
CS161 Introduction to Computer Science
Lecture 4: Program Control Flow
Repetition-Counter control Loop
CiS 260: App Dev I Chapter 4: Control Structures II.
ITM 352 Flow-Control: Loops
Outline Altering flow of control Boolean expressions
Lecture Notes – Week 3 Lecture-2
Repetition Statements
PROGRAM FLOWCHART Iteration Statements.
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.
Loops and Iteration CS 21a: Introduction to Computing I
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops II Lecture 18, Mon Mar borrowing from slides by Kurt Eiselt

2 Reading n This week: Chapter 6 all ( ) n second edition: Chap 7

3 News n Welcome back! n Midterms returned last time n get yours after class if you didn't already n Department news

4 Department of Computer Science Undergraduate Events Events this week Resume & Cover Letter Drop-In Session Date: Wed., Mar 3 Time: 12 – 3 pm (20 mins. sessions) Location: Rm 255, ICICS/CS Find a Job Fast! Info Session Date: Thurs., Mar 4 Time: 12:30 – 1:45 pm Location: DMP 201 Registration: Townhall Meting – 1 st Year CS Students Date: Thurs., Mar 4 Time: 12: pm Location: DMP 310 Lunch will be provided! Faculty Talk – Son Vuong Title: Mobile Learning via LIVES Date: Thurs., Mar 4 Time: 12:30 – 1:45 pm Location: DMP 201 Events next week Townhall Meeting – Combined Majors/Honours, BA, B.Comm in CS Date: Thurs., Mar 11 Time: 12:30 – 2 pm Location: DMP 310 Lunch will be provided! CS Distinguished Lecture Series – Featuring David Parkes Title: Incentive Mechanism Engineering in the Internet Age Date: Thurs., Mar 11 Time: 3:30 – 4:50 pm Location: DMP 110 CSSS Moive Night – “Zombieland” & “Iron Man” Date: Thurs., Mar 11 Time: 6 – 10 pm Location: DMP 310 Free pop & popcorn!

5 Recap: While Statement while (boolean expression) body n Simplest form of loop in Java n Body of loop can be n single statement n whole block of many statements in curly braces n Control flow n body executed if expression is true n then boolean expression evaluated again n if expression still true, body executed again n repetition continues until expression false n then processing continues with next statement after loop

6 Recap: If Versus While Statements n How can loop boolean change from false to true? boolean expression statement true how if statement works boolean expression statement truefalse how while statement works false

7 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } n while statement

8 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } n boolean expression

9 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } n while statement body

10 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } n statement after while n control flow resumes here when boolean is false

11 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } n trace what happens when execute

12 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } limit 3

13 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } limit 3 counter 1

14 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } limit 3 counter 1 Is counter <= limit? yes

15 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } limit 3 counter 1 Is counter <= limit? yes "The square of 1 is 1" printed on monitor

16 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } limit 3 counter 2

17 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } limit 3 counter 2 Is counter <= limit? yes

18 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } limit 3 counter 2 Is counter <= limit? yes "The square of 2 is 4" printed on monitor

19 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } limit 3 counter 3

20 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } limit 3 counter 3 Is counter <= limit? yes

21 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } limit 3 counter 3 Is counter <= limit? yes "The square of 3 is 9" printed on monitor

22 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } limit 3 counter 4

23 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } limit 3 counter 4 Is counter <= limit? NO!

24 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } limit 3 counter 4 Is counter <= limit? NO! “End of demonstration" printed on monitor

25 Climbing Stairs Again n Am I at the top of the stairs? n No. n Climb up one step. n Am I at the top of the stairs? n No. n Climb up one step. n Am I at the top of the stairs? n No. n Climb up one step. n Am I at the top of the stairs? n No. n Climb up one step. n...and so on...

26 Climbing Stairs Again while (I’m not at the top of the stairs) { Climb up one step } n Climbing stairs is a while loop!

27 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter >= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } n change termination condition

28 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter >= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } n change termination condition n body of loop never executed

29 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter >= counter) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } n change termination condition n always true

30 Infinite Loops public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter >= counter) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } n if termination condition always true, loop never ends n infinite loop goes forever

31 Infinite Loops public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter - 1; } System.out.println("End of demonstration"); } n good termination condition n but process never gets closer to condition

32 Infinite Loops public class WhileDemo { public static void main (String[] args) { int limit = 9; int counter = 0; while (counter != limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 2; } System.out.println("End of demonstration"); } n process gets closer to termination condition n but never satisfies condition, keeps going past it

33 Another while Example public class PrintFactorials { public static void main (String[] args) { int limit = 10; int counter = 1; int product = 1; while (counter <= limit) { System.out.println("The factorial of " + counter + " is " + product’\); counter = counter + 1; product = product * counter; } System.out.println("End of demonstration"); } n accumulate product

34 Fun With Loops public class BeerSong { public static void main (String[] args) { int beerNum = 99; String word = "bottles"; while (beerNum > 0) { if (beerNum == 1) { word = "bottle"; } System.out.println(beerNum + " " + word + " of beer on the wall."); System.out.println(beerNum + " " + word + " of beer."); System.out.println("If one of the bottles"); System.out.println("should happen to fall..."); beerNum = beerNum - 1; if (beerNum < 1) { System.out.println("No more bottles of beer on the wall."); }

35 Fun With Loops import java.util.Scanner; public class BeerSong2 { public static void main (String[] args) { int beerNum = 99; String word = "bottles"; boolean keepgoing = true; String answer; Scanner in = new Scanner(System.in); while ((beerNum > 0) && keepgoing) { if (beerNum == 1) { word = "bottle"; } System.out.println(beerNum + " " + word + " of beer on the wall."); System.out.println(beerNum + " " + word + " of beer."); System.out.println("If one of the bottles"); System.out.println("should happen to fall..."); beerNum = beerNum - 1;

36 Fun With Loops System.out.println("Continue? (y/n): "); answer = in.nextLine(); if (answer.equals("n")) { keepgoing = false; } if (beerNum < 1) { System.out.println("No more bottles of beer on the wall."); }

37 public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Other Loop Statements n Equivalent to...

38 public class ForDemo { public static void main (String[] args) { for (int counter = 1; counter <= 3; counter = counter + 1) { System.out.println("The square of " + counter + " is " + (counter * counter)); } System.out.println("End of demonstration"); } Other Loop Statements n...this loop using for statement

39 public class ForDemo { public static void main (String[] args) { for (int counter = 1; counter <= 3; counter = counter + 1) { System.out.println("The square of " + counter + " is " + (counter * counter)); } System.out.println("End of demonstration"); } For Statement n for statement

40 public class ForDemo { public static void main (String[] args) { for (int counter = 1; counter <= 3; counter = counter + 1) { System.out.println("The square of " + counter + " is " + (counter * counter)); } System.out.println("End of demonstration"); } For Statement n Header has three parts n separated by semicolons

41 public class ForDemo { public static void main (String[] args) { for (int counter = 1; counter <= 3; counter = counter + 1) { System.out.println("The square of " + counter + " is " + (counter * counter)); } System.out.println("End of demonstration"); } For Statement n Initialization: first part n executed only one time, at beginning

42 public class ForDemo { public static void main (String[] args) { for (int counter = 1; counter <= 3; counter = counter + 1) { System.out.println("The square of " + counter + " is " + (counter * counter)); } System.out.println("End of demonstration"); } For Statement n boolean expression: second part n evaluated just before loop body, like in while

43 public class ForDemo { public static void main (String[] args) { for (int counter = 1; counter <= 3; counter = counter + 1) { System.out.println("The square of " + counter + " is " + (counter * counter)); } System.out.println("End of demonstration"); } For Statement n Increment: third part n executed at end of loop body n Despite name, arbitrary calculation allowed n could decrement, for example!

44 For Versus While Statement boolean expression statement truefalse boolean expression statement truefalse initialization increment how for statement works how while statement works

45 For Versus While Statement boolean expression statement truefalse boolean expression statement truefalse initialization increment how for statement works how while statement works n flowcharts can be somewhat deceptive n need initialization and incrementing/modifying in while loop too n although syntax does not require it in specific spot

46 For Versus While Statement n Anything that can be done with one type of loop can be done with another n for and while are equivalent n For statement convenient when n loop should be executed specific number of times n number can be determined before loop starts n While statement convenient when n don't know yet how many times to execute loop body n but can check if it’s time to end loop as you go

47 Four Things Needed In Any Loop n Give starting values to one or more variables used in loop test do useful stuff truefalse initialize get closer to termination how loops work in general

48 Four Things Needed In Any Loop n Give starting values to one or more variables used in loop n Test to see when looping stops test do useful stuff truefalse initialize get closer to termination how loops work in general

49 Four Things Needed In Any Loop n Give starting values to one or more variables used in loop n Test to see when looping stops n One or more useful operations here test do useful stuff truefalse initialize get closer to termination how loops work in general

50 Four Things Needed In Any Loop n Give starting values to one or more variables used in loop n Test to see when looping stops n One or more useful operations here n Change something to move process closer termination test do useful stuff truefalse initialize get closer to termination how loops work in general

51 public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter <= limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } Yet Another Loop Statement n while version

52 public class ForDemo { public static void main (String[] args) { for (int counter = 1; counter <= 3; counter = counter + 1) { System.out.println("The square of " + counter + " is " + (counter * counter)); } System.out.println("End of demonstration"); } Yet Another Loop Statement n for version

53 public class DoDemo { public static void main (String[] args) { int limit = 3; int counter = 1; do { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } while (counter <= limit); System.out.println("End of demonstration"); } Yet Another Loop Statement n do version

54 public class DoDemo { public static void main (String[] args) { int limit = 3; int counter = 1; do { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } while (counter <= limit); System.out.println("End of demonstration"); } Do Statement n do version: not quite equivalent n termination test at end, so body executed at least once

55 Four Things Needed In Any Loop n Give starting values to one or more variables used in loop n Test to see when looping stops n One or more useful operations here n Change something to move process closer termination test do useful stuff truefalse initialize get closer to termination how loops work in general

56 Do Statement n Body always executed at least once test do useful stuff true false initialize get closer to termination order of four things can change, but need them all