University of British Columbia CPSC 111, Intro to Computation - - PowerPoint PPT Presentation

university of british columbia cpsc 111 intro to
SMART_READER_LITE
LIVE PREVIEW

University of British Columbia CPSC 111, Intro to Computation - - PowerPoint PPT Presentation

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb 22 2006 based on slides by Kurt Eiselt http://www.cs.ubc.ca/~tmm/courses/cpsc111-06-spr News Assignment 2 out


slide-1
SLIDE 1

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Loops II Lecture 13, Thu Feb 22 2006 http://www.cs.ubc.ca/~tmm/courses/cpsc111-06-spr based on slides by Kurt Eiselt

slide-2
SLIDE 2

News

■ Assignment 2 out

■ it’s challenging, start now!!

slide-3
SLIDE 3

Reading

■ This week: Chapter 7 all (7.1-7.4) ■ Next week: 8.1, 8.5-8.7, topics 6.3 and 6.4

slide-4
SLIDE 4

String Comparison Followup

■ Why did (name == "Kermit") work?

■ vs. object comparison with equals method

■ Strings are special case

■ intern method stores them in central table,

then equality check with “==“ works

■ Strings are often but not always interned

automatically

■ details tricky, see

http://javatechniques.com/public/java/docs/basics/string-equality.html

slide-5
SLIDE 5

Recap: While Statement

while (boolean expression) body

■ Body of loop can be

■ single statement ■ whole block of many statements in curly braces

■ Control flow

■ body executed if expression is true ■ then boolean expression evaluated again ■ if expression still true, body executed again ■ repetition continues until expression false ■ then processing continues with next statement after loop

slide-6
SLIDE 6

Recap: If Versus While Statements

boolean expression statement true

how if statement works

boolean expression statement true false

how while statement works

false

slide-7
SLIDE 7

Recap: Loop Termination

while (termination condition) { body }

■ Loop boolean expression aka termination condition ■ Logic of termination condition must match logic in loop body

for updating variables used in condition

■ If termination condition always true, infinite loop never ends ■ If termination condition always false, body never executed

slide-8
SLIDE 8

Objectives

■ Understand when and how to use

■ for loops ■ nested loops

slide-9
SLIDE 9

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."); } } } }

slide-10
SLIDE 10

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;

slide-11
SLIDE 11

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."); } } } }

slide-12
SLIDE 12

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

■ Equivalent to...

slide-13
SLIDE 13

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

■ ...this loop using for statement

slide-14
SLIDE 14

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

■ for statement

slide-15
SLIDE 15

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

■ Header has three parts

■ separated by semicolons

slide-16
SLIDE 16

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

■ Initialization: first part

■ executed only one time, at beginning

slide-17
SLIDE 17

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

■ boolean expression: second part

■ evaluated just before loop body, like in while

slide-18
SLIDE 18

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

■ Increment: third part

■ executed at end of loop body

■ Despite name, arbitrary calculation allowed

■ could decrement, for example!

slide-19
SLIDE 19

For Versus While Statement

boolean expression statement

true false

boolean expression statement

true false

initialization increment

how for statement works how while statement works

slide-20
SLIDE 20

For Versus While Statement

boolean expression statement

true false

boolean expression statement

true false

initialization increment

how for statement works how while statement works

■ flowcharts can be somewhat deceptive

■ need initialization and incrementing/modifying in while loop too ■ although syntax does not require it in specific spot

slide-21
SLIDE 21

For Versus While Statement

■ Anything that can be done with one type of loop can

be done with another

■ for and while are equivalent

■ For statement convenient when

■ loop should be executed specific number of times ■ number can be determined before loop starts

■ While statement convenient when

■ don't know yet how many times to execute loop body ■ but can check if it’s time to end loop as you go

slide-22
SLIDE 22

Four Things Needed In Any Loop

■ Give starting values to one or

more variables used in loop

test do useful stuff

true false

initialize get closer to termination

how loops work in general

slide-23
SLIDE 23

Four Things Needed In Any Loop

■ Give starting values to one or

more variables used in loop

■ Test to see when looping

stops

test do useful stuff

true false

initialize get closer to termination

how loops work in general

slide-24
SLIDE 24

Four Things Needed In Any Loop

■ Give starting values to one or

more variables used in loop

■ Test to see when looping

stops

■ One or more useful

  • perations here

test do useful stuff

true false

initialize get closer to termination

how loops work in general

slide-25
SLIDE 25

Four Things Needed In Any Loop

■ Give starting values to one or

more variables used in loop

■ Test to see when looping

stops

■ One or more useful

  • perations here

■ Change something to move

process closer termination

test do useful stuff

true false

initialize get closer to termination

how loops work in general

slide-26
SLIDE 26

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

■ while version

slide-27
SLIDE 27

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

■ for version

slide-28
SLIDE 28

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

■ do version

slide-29
SLIDE 29

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

■ do version: not quite equivalent

■ termination test at end, so body executed at

least once

slide-30
SLIDE 30

Four Things Needed In Any Loop

■ Give starting values to one or

more variables used in loop

■ Test to see when looping

stops

■ One or more useful

  • perations here

■ Change something to move

process closer termination

test do useful stuff

true false

initialize get closer to termination

how loops work in general

slide-31
SLIDE 31

Do Statement

■ Body always executed at

least once

test do useful stuff

true false

initialize get closer to termination

  • rder of four things can change, but need them all
slide-32
SLIDE 32

Nested Loops

■ Very simple for loop

public class SimpleLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } } }

■ What does it do?

slide-33
SLIDE 33

Nested Loops

■ Very simple for loop

public class SimpleLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } } }

■ What does it do? Prints

1 2 3

slide-34
SLIDE 34

Nested Loops

■ Very simple for loop

public class SimpleLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } } }

■ What if for every number below, want multiplication

table of value times 2, x3, etc?

1 2 3 2 4 6 3 6 9

slide-35
SLIDE 35

Nested Loops

■ Very simple for loop

public class SimpleLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } } }

■ For every number printed by loop above

1 2 3 2 4 6 3 6 9

slide-36
SLIDE 36

Nested Loops

■ Very simple for loop

public class SimpleLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } } }

■ For every number printed by loop above

■ need another loop to print numbers in row

1 2 3 2 4 6 3 6 9

slide-37
SLIDE 37

Nested Loops

■ Very simple for loop

public class SimpleLoop { public static void main (String[] args) { for (int i = 1; i <= 3; i++) { System.out.println(i); } } }

■ For every number printed by loop above

■ need another loop to print numbers in row

1 2 3 2 4 6 3 6 9

How do we do that?

slide-38
SLIDE 38

Nested Loops

■ Put a loop inside a loop

■ 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(); } } }

slide-39
SLIDE 39

Nested Loops

■ Put a loop inside a loop

■ 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

slide-40
SLIDE 40

Nested Loops

■ Put a loop inside a loop

■ 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

slide-41
SLIDE 41

Nested Loops

■ Put a loop inside a loop

■ 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 j 1 1

slide-42
SLIDE 42

Nested Loops

■ Put a loop inside a loop

■ 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 j 1 1

slide-43
SLIDE 43

Nested Loops

■ Put a loop inside a loop

■ 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 j 1 1 1_

slide-44
SLIDE 44

Nested Loops

■ Put a loop inside a loop

■ 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 j 1 2 1_

slide-45
SLIDE 45

Nested Loops

■ Put a loop inside a loop

■ 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 j 1 2 1_

slide-46
SLIDE 46

Nested Loops

■ Put a loop inside a loop

■ 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 j 1 2 1 2_

slide-47
SLIDE 47

Nested Loops

■ Put a loop inside a loop

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

slide-48
SLIDE 48

Nested Loops

■ Put a loop inside a loop

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

slide-49
SLIDE 49

Nested Loops

■ Put a loop inside a loop

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

slide-50
SLIDE 50

Nested Loops

■ Put a loop inside a loop

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

slide-51
SLIDE 51

Nested Loops

■ Put a loop inside a loop

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

slide-52
SLIDE 52

Nested Loops

■ Put a loop inside a loop

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

slide-53
SLIDE 53

Nested Loops

■ Put a loop inside a loop

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

slide-54
SLIDE 54

Nested Loops

■ Put a loop inside a loop

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

slide-55
SLIDE 55

Nested Loops

■ Put a loop inside a loop

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

slide-56
SLIDE 56

Nested Loops

■ Put a loop inside a loop

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

slide-57
SLIDE 57

Nested Loops

■ Put a loop inside a loop

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

slide-58
SLIDE 58

Nested Loops

■ Put a loop inside a loop

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

slide-59
SLIDE 59

Nested Loops

■ Put a loop inside a loop

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

slide-60
SLIDE 60

Nested Loops

■ Put a loop inside a loop

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

slide-61
SLIDE 61

Nested Loops

■ Put a loop inside a loop

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

slide-62
SLIDE 62

Nested Loops

■ Put a loop inside a loop

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

slide-63
SLIDE 63

Nested Loops

■ Put a loop inside a loop

■ 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 j 2 3 1 2 3 2 4 6_

slide-64
SLIDE 64

Nested Loops

■ Put a loop inside a loop

■ 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 j 2 4 1 2 3 2 4 6_

slide-65
SLIDE 65

Nested Loops

■ Put a loop inside a loop

■ 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 j 2 4 1 2 3 2 4 6_

slide-66
SLIDE 66

Nested Loops

■ Put a loop inside a loop

■ 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 j 2 4 1 2 3 2 4 6 _

slide-67
SLIDE 67

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 4 1 2 3 2 4 6 _

slide-68
SLIDE 68

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 4 1 2 3 2 4 6 _

slide-69
SLIDE 69

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 1 1 2 3 2 4 6 _

slide-70
SLIDE 70

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 1 1 2 3 2 4 6 _

slide-71
SLIDE 71

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 1 1 2 3 2 4 6 3_

slide-72
SLIDE 72

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 2 1 2 3 2 4 6 3_

slide-73
SLIDE 73

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 2 1 2 3 2 4 6 3_

slide-74
SLIDE 74

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 2 1 2 3 2 4 6 3 6_

slide-75
SLIDE 75

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 3 1 2 3 2 4 6 3 6_

slide-76
SLIDE 76

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 3 1 2 3 2 4 6 3 6_

slide-77
SLIDE 77

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 3 1 2 3 2 4 6 3 6 9_

slide-78
SLIDE 78

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 4 1 2 3 2 4 6 3 6 9_

slide-79
SLIDE 79

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 4 1 2 3 2 4 6 3 6 9_

slide-80
SLIDE 80

Nested Loops

■ Put a loop inside a loop

■ 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 j 3 4 1 2 3 2 4 6 3 6 9 _

slide-81
SLIDE 81

Nested Loops

■ Put a loop inside a loop

■ 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 j 4 4 1 2 3 2 4 6 3 6 9 _

slide-82
SLIDE 82

Nested Loops

■ Put a loop inside a loop

■ 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 j 4 4 1 2 3 2 4 6 3 6 9 _

slide-83
SLIDE 83

Nested Loops

■ Put a loop inside a loop

■ 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 j 4 4 1 2 3 2 4 6 3 6 9 _

Exit!

slide-84
SLIDE 84

Practice Problem

■ Write program using loop to simulate flipping

a coin one million times

■ keep track of how many times it’s heads up

and how many heads down

■ print results

■ Make version for each loop type

■ while, for, do