Download presentation
Presentation is loading. Please wait.
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
2
News n Assignment 2 out n it’s challenging, start now!!
3
Reading n This week: Chapter 7 all (7.1-7.4) n Next week: 8.1, 8.5-8.7, topics 6.3 and 6.4
4
String Comparison Followup n Why did (name == "Kermit") work? n vs. object comparison with equals method n Strings are special case n intern method stores them in central table, then equality check with “==“ works n Strings are often but not always interned automatically n details tricky, see http://javatechniques.com/public/java/docs/basics/string-equality.html
5
Recap: While Statement while (boolean expression) body 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 boolean expression statement true how if statement works boolean expression statement truefalse how while statement works false
7
Recap: Loop Termination while (termination condition) { body } n Loop boolean expression aka termination condition n Logic of termination condition must match logic in loop body for updating variables used in condition n If termination condition always true, infinite loop never ends n If termination condition always false, body never executed
8
Objectives n Understand when and how to use n for loops n nested loops
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."); }
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;
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."); }
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 n Equivalent to...
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 n...this loop using for statement
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 n for statement
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 n Header has three parts n separated by semicolons
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 n Initialization: first part n executed only one time, at beginning
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 n boolean expression: second part n evaluated just before loop body, like in while
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 n Increment: third part n executed at end of loop body n Despite name, arbitrary calculation allowed n could decrement, for example!
19
For Versus While Statement boolean expression statement truefalse boolean expression statement truefalse initialization increment how for statement works how while statement works
20
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
21
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
22
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
23
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
24
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
25
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
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 n while version
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 n for version
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 n do version
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 n do version: not quite equivalent n termination test at end, so body executed at least once
30
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
31
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
32
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?
33
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
34
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
35
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
36
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
37
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?
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(); }
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(); } i 1
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(); } i 1
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 1 1
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 1 1
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 1 1 1_
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 1 2 1_
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 1 2 1_
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 1 2 1 2_
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 1 3 1 2_
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 1 3 1 2_
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 1 3 1 2 3_
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 1 4 1 2 3_
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 1 4 1 2 3_
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 1 4 12 3 _
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 2 4 12 3 _
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 2 4 12 3 _
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 2 1 12 3 _
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 2 1 12 3 _
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 2 1 12 3 2_
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 2 2 12 3 2_
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 2 2 12 3 2_
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 2 1 12 3 24_
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 2 3 12 3 24_
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 2 3 12 3 24_
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 2 3 12 3 246_
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 2 4 12 3 246_
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 2 4 12 3 246_
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 2 4 12 3 246 _
67
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 _
68
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 _
69
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 _
70
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 _
71
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_
72
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_
73
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_
74
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_
75
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_
76
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_
77
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_
78
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_
79
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_
80
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 _
81
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 _
82
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 _
83
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!
84
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.