University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar 3 2010

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

 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.
More loops Horstmann Ch 7 continued.. The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition?
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.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
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
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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 Jan-Apr 2006 Tamara Munzner Loops Lecture 12, Tue Feb
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 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops II Lecture 18, Mon Mar
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.
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
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.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
1 The for loop. 2 Repetition with for loops So far, repeating a statement is redundant: System.out.println("Homer says:"); System.out.println("I am so.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
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.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
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.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
Chapter 6 More Conditionals and Loops
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5 Repetition.
Decision statements. - They can use logic to arrive at desired results
OPERATORS (2) CSC 111.
Outline Altering flow of control Boolean expressions
Lab5 PROGRAMMING 1 Loop chapter4.
Building Java Programs
while while (condition) { statements }
Building Java Programs
Building Java Programs
Control Statements Loops.
Repetition Statements
Building Java Programs
PROGRAM FLOWCHART Iteration Statements.
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
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.
Chapter 4: Loops and Iteration
Presentation transcript:

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

2 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"); } Review: For Statement n Header has three parts, separated by semicolons n first: initialization: executed only one time, at start n second: boolean expression: evaluated just before loop body, like in while n third: increment: executed at end of loop body, arbitrary calculation allowed

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 Nested Loops n Very simple for loop public class SimpleLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } n What does it do?

16 Nested Loops n Very simple for loop public class SimpleLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } n What does it do? Prints

17 Nested Loops n Very simple for loop public class SimpleLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } n What if for every number below, want multiplication table of value times 2, x3, etc?

18 Nested Loops n Very simple for loop public class SimpleLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } n For every number printed by loop above

19 Nested Loops n Very simple for loop public class SimpleLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } n For every number printed by loop above n need another loop to print numbers in row

20 Nested Loops n Very simple for loop public class SimpleLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } n For every number printed by loop above n need another loop to print numbers in row How do we do that?

21 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); }

22 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } i 1

23 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } i 1

24 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij 1 1

25 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij 1 1

26 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij 1 1 1_

27 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij 1 2 1_

28 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij 1 2 1_

29 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

30 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

31 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

32 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

33 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

34 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

35 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

36 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

37 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

38 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

39 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

40 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

41 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

42 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

43 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

44 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

45 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

46 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

47 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

48 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

49 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

50 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

51 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

52 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

53 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

54 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

55 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

56 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

57 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

58 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

59 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

60 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

61 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

62 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

63 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

64 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

65 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _

66 Nested Loops n Put a loop inside a loop n trace to see how it works public class NestedLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { System.out.print((i * j) + " "); } System.out.println(); } ijij _ Exit!

67 Practice Problem n Write program using loop to simulate flipping a coin one million times n keep track of how many times it’s heads up and how many heads down n print results n Make version for each loop type n while, for, do