University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops Lecture 12, Tue Feb 21 2006

Slides:



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

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.
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.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
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 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb
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
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops II Lecture 18, Mon Mar
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.
© 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.
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
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
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.
Control statements Mostafa Abdallah
The for loop.
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.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
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)
REPETITION CONTROL STRUCTURE
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.
Control Structure Chapter 3.
Repetition Statements (Loops) - 2
Chap 7. Advanced Control Statements in Java
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)
Control Structure.
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops Lecture 12, Tue Feb based on slides by Kurt Eiselt

News n Welcome back! n resume lectures, labs, tutorials, office hours n Midterm and Assignment 1 returned n pick up after class if you don't have yet n midterm solutions posted on WebCT n Assignment 2 posted soon n probably later today

Reading n This week: Chapter 7 all ( )

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

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

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!");

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 { }

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.”);

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

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

Objectives n Practice with conditionals n Understand basic loops

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

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

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

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

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?

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.

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?

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...

Washing Hair n Lather

Washing Hair n Lather n Rinse

Washing Hair n Lather n Rinse n Repeat

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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!

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

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...

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

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

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

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

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

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

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

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

Questions?