Download presentation
Presentation is loading. Please wait.
1
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 20, Fri Mar 5 2010 http://www.cs.ubc.ca/~tmm/courses/111-10 borrowing from slides by Kurt Eiselt
2
2 Reading n Reading question for Chap 6 due today n Next week: n Chap 7: 7.1, 7.5-7.7. Topics 7.3 and 7.4 (3rd ed) n Chap 8: 8.1, 8.5-8.7. Topics 6.3 and 6.4 (2nd ed)
3
3 News n Midterms returned before break n get yours after class if you didn't already
4
4 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"); } Recap: While Loop Example n while version
5
5 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"); } Recap: For Loop Example n for version
6
6 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"); } Recap: Do Loop Example n do version
7
7 Recap: 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
8
8 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
9
9 Flipping Coins n while version
10
10 Flipping Coins n for version
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.