Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar 3 2010 http://www.cs.ubc.ca/~tmm/courses/111-10 borrowing from slides by Kurt Eiselt

2 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 123123

17 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? 1 2 3 2 4 6 3 6 9

18 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 1 2 3 2 4 6 3 6 9

19 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 1 2 3 2 4 6 3 6 9

20 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 1 2 3 2 4 6 3 6 9 How do we do that?

21 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 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 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 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 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 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 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 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 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 1 2 1 2_

30 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 1 3 1 2_

31 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 1 3 1 2_

32 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 1 3 1 2 3_

33 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 1 4 1 2 3_

34 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 1 4 1 2 3_

35 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 1 4 12 3 _

36 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 2 4 12 3 _

37 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 2 4 12 3 _

38 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 2 1 12 3 _

39 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 2 1 12 3 _

40 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 2 1 12 3 2_

41 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 2 2 12 3 2_

42 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 2 2 12 3 2_

43 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 2 1 12 3 24_

44 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 2 3 12 3 24_

45 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 2 3 12 3 24_

46 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 2 3 12 3 246_

47 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 2 4 12 3 246_

48 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 2 4 12 3 246_

49 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 2 4 12 3 246 _

50 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 3 4 12 3 246 _

51 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 3 4 12 3 246 _

52 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 3 1 12 3 246 _

53 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 3 1 12 3 246 _

54 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 3 1 12 3 246 3_

55 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 3 2 12 3 246 3_

56 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 3 2 12 3 246 3_

57 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 3 2 12 3 246 36_

58 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 3 3 12 3 246 36_

59 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 3 3 12 3 246 36_

60 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 3 3 12 3 246 369_

61 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 3 4 12 3 246 369_

62 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 3 4 12 3 246 369_

63 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 3 4 12 3 246 369 _

64 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 4 4 12 3 246 369 _

65 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 4 4 12 3 246 369 _

66 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 4 4 12 3 246 369 _ Exit!

67 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


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

Similar presentations


Ads by Google