University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb 12 2010

Slides:



Advertisements
Similar presentations
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Advertisements

5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
1 Repetition structures Overview while statement for statement do while statement.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 2 Sanjay Goel University at Albany,
CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.
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
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
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.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops II Lecture 18, Mon Mar
TODAY’S LECTURE Review Chapter 2 Go over exercises.
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.
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Chapter 5 Loops.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/29 The switch Statement The switch statement provides another way.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Control statements Mostafa Abdallah
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Application development with Java Lecture 6 Rina Zviel-Girshin.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
COMP Loop Statements Yi Hong May 21, 2015.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Lecture 4b Repeating With Loops
Chapter 4 Repetition Statements (loops)
The switch Statement, and Introduction to Looping
Control Structures.
Primitive Data, Variables, Loops (Maybe)
Outline Altering flow of control Boolean expressions
The ‘while’ Statement September 27, 2006
Lab5 PROGRAMMING 1 Loop chapter4.
Chap 7. Advanced Control Statements in Java
Review of Previous Lesson
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.
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
CprE 185: Intro to Problem Solving (using C)
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb borrowing from slides by Kurt Eiselt

2 Reading n This week: Chapter 5 all ( ) n second edition: Chap 6 n Next week: Chapter 6 all ( ) n second edition: Chap 7

3 News n Next week is reading week n no lectures or labs or tutorials n Midterms returned today n Grades, statistics already posted on WebCT n returned end of class, line up by last name (A- Z)

4 Midterm Marks Distribution n marks will not be scaled

5 Midterm Distribution: Detailed

6 Regrading n Reminder: protocol for regrade requests n read solution and marking scheme first, carefully n no regrade requests accepted until at least 24 hours after material is handed back n exception: arithmetic errors n regrade requests must be in writing (paper or ) n assignments: to marker (listed on cover sheet) n if still have dispute after discussion with TA, can escalate to instructor n exams: to instructor

7 Recap: Comparing Strings n Relational operator == is wrong way to compare String name1 = "Bubba"; String name2 = "Bubba"; System.out.println(name1 == name2); // prints false n equals method is right way to compare Strings String name1 = "Bubba"; String name2 = "Bubba"; System.out.println(name1.equals(name2)); // prints true n why? diagrams will help

8 Recap: Comparing Strings n name1 == name2 : two different references, false name1 name2 "Bubba" n name1.equals(name2) : contents same, true

9 Recap: Short-Circuting Evaluation n Java evaluates complex expressions left to right n short-circuiting: Java stops evaluating once value is clearly true or false n aka lazy evaluation if ((b > a) && (c == 10)) System.out.println("when b<=a short-circuit"); if ((b > a) || (c == 10)) System.out.println("when b>a short-circuit"); n Corollary: avoid statements with side effects if ((b > a) || (c++)) System.out.println("Danger Will Robinson!");

10 Recap: Conditional Syntax if ( boolean expression ) statement else if ( boolean expression ) statement n optional: zero, one, or many else statement n optional n if, else are reserved words n parentheses mandatory n statement can be n single line n block of several lines enclosed in { }

11 Recap: Comparing Floats/Doubles n Relational operator for equality not safe for floating point comparison if (.3 == 1.0/ / /10.0)) System.out.println("Beware roundoff error"); n Check if difference close to 0 instead if (Math.abs(f1 - f2) < TOLERANCE) System.out.println (“Essentially equal.”);

12 Recap: Comparing Characters n Safe to compare character types with relational operators char c = 'a'; char d = 'b'; if (c == d) System.out.println("they match");

13 Recap: Switch Syntax switch ( expression ) { case value : statements break; case value : statements break; default: statements n switch, case, break are reserved words n expression and value must be int or char n value cannot be variable n break important, or else control flow continues to next set n statements can be one line or several lines n default executed if no values match expression

14 Objectives n Practice with conditionals n Understand basic loops

15 public class NestTest3 { public static void main (String[] args) { respondToName("Flocinaucinihilipiliphication"); respondToName("Supercalifragilisticexpialidocious"); respondToName("Ambrose"); respondToName("Kermit"); respondToName("Miss Piggy!!!"); respondToName("Spot"); respondToName("me"); } public static void respondToName(String name) { System.out.println("You're named " + name); if (name.length() > 20) { System.out.println("Gosh, long name"); System.out.println("Keeping typists busy..."); } else if (name.length() > 30) { System.out.println("Over the top"); } else if (name.length() < 10) { if (name.charAt(0) == 'A') System.out.println("You're first"); else if (name == "Kermit") System.out.println("You're a frog"); System.out.println("I love animals"); } else if (name.equals("Spot")) { System.out.println("You're spotted"); } else if (name.length() < 3) { System.out.println("Concise!"); }

16 Repetition, Iteration, Loops n Computers good at performing same task many times n Loops allow repetitive operations in programs n aka iteration statements, repetition statements n Loops handy in real life too

17 Climbing Stairs n Am I at the top of the stairs?

18 Climbing Stairs n Am I at the top of the stairs? n No. n Climb up one step.

19 Climbing Stairs n Am I at the top of the stairs? n No. n Climb up one step. n Am I at the top of the stairs?

20 Climbing Stairs n Am I at the top of the stairs? n No. n Climb up one step. n Am I at the top of the stairs? n No. n Climb up one step.

21 Climbing Stairs n Am I at the top of the stairs? n No. n Climb up one step. n Am I at the top of the stairs? n No. n Climb up one step. n Am I at the top of the stairs?

22 Climbing Stairs n Am I at the top of the stairs? n No. n Climb up one step. n Am I at the top of the stairs? n No. n Climb up one step. n Am I at the top of the stairs? n No. n Climb up one step. n Am I at the top of the stairs? n No. n Climb up one step. n...and so on...

23 Washing Hair n Lather

24 Washing Hair n Lather n Rinse

25 Washing Hair n Lather n Rinse n Repeat

26 Washing Hair n Lather n Rinse n Repeat n When do you stop??

27 While Statement while (boolean expression) body n Simplest form of loop in Java 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

28 If Versus While Statements boolean expression statement true how if statement works false

29 If Versus While Statements boolean expression statement true how if statement works boolean expression statement truefalse how while statement works false

30 If Versus While Statements n How can loop boolean change from false to true? boolean expression statement true how if statement works boolean expression statement truefalse how while statement works false

31 If Versus While Statements n These diagrams called flowcharts boolean expression statement true how if statement works boolean expression statement truefalse how while statement works false

32 Using while Statements 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"); } n while statement

33 Using while Statements 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"); } n boolean expression

34 Using while Statements 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"); } n while statement body

35 Using while Statements 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"); } n statement after while n control flow resumes here when boolean is false

36 Using while Statements 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"); } n trace what happens when execute

37 Using while Statements 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"); } limit 3

38 Using while Statements 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"); } limit 3 counter 1

39 Using while Statements 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"); } limit 3 counter 1 Is counter <= limit? yes

40 Using while Statements 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"); } limit 3 counter 1 Is counter <= limit? yes "The square of 1 is 1" printed on monitor

41 Using while Statements 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"); } limit 3 counter 2

42 Using while Statements 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"); } limit 3 counter 2 Is counter <= limit? yes

43 Using while Statements 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"); } limit 3 counter 2 Is counter <= limit? yes "The square of 2 is 4" printed on monitor

44 Using while Statements 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"); } limit 3 counter 3

45 Using while Statements 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"); } limit 3 counter 3 Is counter <= limit? yes

46 Using while Statements 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"); } limit 3 counter 3 Is counter <= limit? yes "The square of 3 is 9" printed on monitor

47 Using while Statements 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"); } limit 3 counter 4

48 Using while Statements 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"); } limit 3 counter 4 Is counter <= limit? NO!

49 Using while Statements 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"); } limit 3 counter 4 Is counter <= limit? NO! “End of demonstration" printed on monitor

50 Climbing Stairs Again n Am I at the top of the stairs? n No. n Climb up one step. n Am I at the top of the stairs? n No. n Climb up one step. n Am I at the top of the stairs? n No. n Climb up one step. n Am I at the top of the stairs? n No. n Climb up one step. n...and so on...

51 Climbing Stairs Again while (I’m not at the top of the stairs) { Climb up one step } n Climbing stairs is a while loop!

52 Using while Statements 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"); } n change termination condition

53 Using while Statements 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"); } n change termination condition n body of loop never executed

54 Using while Statements public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter >= counter) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } n change termination condition n always true

55 Infinite Loops public class WhileDemo { public static void main (String[] args) { int limit = 3; int counter = 1; while (counter >= counter) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 1; } System.out.println("End of demonstration"); } n if termination condition always true, loop never ends n infinite loop goes forever

56 Infinite Loops 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"); } n good termination condition n but process never gets closer to condition

57 Infinite Loops public class WhileDemo { public static void main (String[] args) { int limit = 9; int counter = 0; while (counter != limit) { System.out.println("The square of " + counter + " is " + (counter * counter)); counter = counter + 2; } System.out.println("End of demonstration"); } n process gets closer to termination condition n but never satisfies condition, keeps going past it

58 Another while Example public class PrintFactorials { public static void main (String[] args) { int limit = 10; int counter = 1; int product = 1; while (counter <= limit) { System.out.println("The factorial of " + counter + " is " + product’\); counter = counter + 1; product = product * counter; } System.out.println("End of demonstration"); } n accumulate product

59 Questions?