Lecture 10.2 Different Types of Loops, including for and do.

Slides:



Advertisements
Similar presentations
Overloading Having more than one method with the same name is known as overloading. Overloading is legal in Java as long as each version takes different.
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Computer Science 1620 Loops.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops –For For Reading for this Lecture, L&L, Part of 5.8.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
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.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 5: Control Structures II (Repetition)
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Java Programming: From the Ground Up
© 2006 Pearson Addison-Wesley. All rights reserved Inheritance Systems Merchandise ElectronicsClothing TelevisionCamcorderShirtDressShoe DigitalAnalog.
Loops and Iteration for Statements, while Statements and do-while Statements.
© 2004 Pearson Addison-Wesley. All rights reserved February 20, 2006 ‘do’ and ‘for’ loops ComS 207: Programming I (in Java) Iowa State University, SPRING.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
Control Structures. 2  Control – the order in which instructions are performed in a program.  Control structures are well defined ways of determining.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
EVENTS Wiring together objects, code, and actions.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Lecture 7.1 Selection - the if Instruction. © 2006 Pearson Addison-Wesley. All rights reserved An algorithm often needs to make choices… choose.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
The for loop.
© 2006 Pearson Addison-Wesley. All rights reserved Non-void Methods Parameters are largely one-way communication.  Shared instances variables is.
Application development with Java Lecture 6 Rina Zviel-Girshin.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Loops. More Flow of Control  Sometimes we want to do something many times.  Don’t have to write all the steps out multiple times.  Use a LOOP – control.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Lecture 7: Repeating a Known Number of Times
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Iteration with While You can say that again.
Debugging October 3, 2007 ComS 207: Programming I (in Java)
Outline Altering flow of control Boolean expressions
CS1100 Computational Engineering
The University of Texas – Pan American
Class Hierarchies and Type Conformance
Loop Strategies Repetition Playbook.
Lecture 8:The For Loop AP Computer Science Principles.
Building Java Programs
Debugging October 4, 2006 ComS 207: Programming I (in Java)
Building Java Programs
Repetition Statements (Loops) - 2
Loops & Nested Loops CSE 120 Winter 2019
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
Chapter 3 Flow of Control Loops in Java.
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
Chapter 4: Loops and Iteration
Presentation transcript:

Lecture 10.2 Different Types of Loops, including for and do

© 2006 Pearson Addison-Wesley. All rights reserved Counting loops General Form A loop that relies upon a counting variable to control the end of loop repetition is called a counting loop. initialize_counter while (counter_satisfies_some_condition) { perform_work; update_counter_value; } public void makeDotRow( JFrame f ) { Oval circle; int xDelta, yDelta; int count; xDelta = w.getWidth() / 10; yDelta = w.getHeight() / 10; count = 0; while (count != 10) { dot = new Oval(count*xDelta+2, count*yDelta+2,5,5); dot.setBackground(Color.red); f.add( dot, 0 ); count++; } public void makeDotRow( JFrame f ) { Oval circle; int xDelta, yDelta; int count; xDelta = w.getWidth() / 10; yDelta = w.getHeight() / 10; count = 0; while (count != 10) { dot = new Oval(count*xDelta+2, count*yDelta+2,5,5); dot.setBackground(Color.red); f.add( dot, 0 ); count++; } Example

© 2006 Pearson Addison-Wesley. All rights reserved Some Exercises Exercise 1 Write a loop to draw a “ladder with twelve rungs. Each rung is 3 pixels thick and 20 pixels long. Rungs are spaced 14 pixels apart. Exercise 2 Write a loop to draw ten vertical lines as follows.  The first line is half the distance between the window sides.  The second line is half the distance between the window’s left edge and the first line.  The third line is half the distance between the window’s left edge and the second line....  The tenth line is half the distance between the window’s left edge and the ninth line.

© 2006 Pearson Addison-Wesley. All rights reserved for loop Syntax for ( initStmt ; condition ; progressStmt ) { loopBody ; } where loopBody is a sequence of zero or more instructions, initStmt and progressStmt are instructions, and condition is a Boolean expression Semantics (how to execute a for instruction) 2) Evaluate condition. 3.b) If condition is false, then terminate loop. 3.a) If condition is true, then execute loopBody followed by progressStmt and return to Step 2. 1) execute initStmt.

© 2006 Pearson Addison-Wesley. All rights reserved Comparison of while & for Both while and for loops are well suited to counting loops int count; count = 1; while (count != 10) { // do something count++; } int count; count = 1; while (count != 10) { // do something count++; } Example int count; for (count=1; count != 10, count++) { // do something } int count; for (count=1; count != 10, count++) { // do something } A variable declared in initStmt is local to the loop’s body. for (int count=1; count != 10, count++) { // do something } for (int count=1; count != 10, count++) { // do something }

© 2006 Pearson Addison-Wesley. All rights reserved Sentinel loops Counting Loop: Print the first fifteen numbers that are perfect squares. Which of the following are sentinel loops? Non-counting loops are sometimes called sentinel loops, because they use some other circumstance (the “sentinel”) to determine when to terminate loop repetition. Sentinel Loop: Print all perfect squares that are less than Print the first ten letters of the alphabet. Print the alphabet up to and including the letter “R”. Approximate the value of 22/7 to the nearest eight digits. Calculate the first integer power of 83 that is greater than one billion. Starting with distance d1 and d2 repeatedly divide them both in half until the resulting lengths are within 1 inch of each other.

© 2006 Pearson Addison-Wesley. All rights reserved The Infinite Loop - Caution #1 An infinite loop (also called a dead loop) is one that will execute “forever”. Example public void makeDotRow( JFrame f ) { Oval dot; int xDelta, yDelta; int count; xDelta = w.getWidth() / 10; yDelta = w.getHeight() / 10; count = 11; while (count != 10) { circle = new Oval(count*xDelta+2, count*yDelta+2,5,5); circle.setBackground(Color.red); f.add( dot, 0 ); count++; } public void makeDotRow( JFrame f ) { Oval dot; int xDelta, yDelta; int count; xDelta = w.getWidth() / 10; yDelta = w.getHeight() / 10; count = 11; while (count != 10) { circle = new Oval(count*xDelta+2, count*yDelta+2,5,5); circle.setBackground(Color.red); f.add( dot, 0 ); count++; } A More Subtle Example int k; k = 20; while ( k > 0 ) { k = Math.round(k / 2.0); } int k; k = 20; while ( k > 0 ) { k = Math.round(k / 2.0); }

© 2006 Pearson Addison-Wesley. All rights reserved More About Infinite Loops Potential Infinite Loop public int factorial( int n ) { int result; result = 1; while (n != 0) { result = result * n n-- } return result; } public int factorial( int n ) { int result; result = 1; while (n != 0) { result = result * n n-- } return result; } A Different Version public int factorial( int n ) { int result; result = 1; while (n > 0) { result = result * n n-- } return result; } public int factorial( int n ) { int result; result = 1; while (n > 0) { result = result * n n-- } return result; } Consider factorial(-1) Why is the logic error in this code obvious at runtime? How does this code behave for factorial(-1) ? Moral: Infinite loops are not always a bad thing.

© 2006 Pearson Addison-Wesley. All rights reserved Off-by-one Errors - Caution #2 It is common to write a counting loop that executes one time too many or one time too few. int k = 1; while ( k != 100 ) { // do something k++; } int k = 1; while ( k != 100 ) { // do something k++; } Which of the following repeat the body 100 times? int k = 0; while ( k < 99 ) { // do something k++; } int k = 0; while ( k < 99 ) { // do something k++; } int k = 1; while ( k < 100 ) { // do something k++; } int k = 1; while ( k < 100 ) { // do something k++; } int k = 100; while ( k >= 0 ) { // do something k--; } int k = 100; while ( k >= 0 ) { // do something k--; }

© 2006 Pearson Addison-Wesley. All rights reserved Loops Don’t Animate - Caution #3 The following is an attempt to cause rect to move. What goes wrong? Rectangle rect; rect = new Rectangle( 10, 10, 10, 10 ); window.add( rect, 0 ); while ( rect.getX() < window.getWidth() ) { rect.setLocation( rect.getX()+5, rect.getY() ); rect.repaint(); } Rectangle rect; rect = new Rectangle( 10, 10, 10, 10 ); window.add( rect, 0 ); while ( rect.getX() < window.getWidth() ) { rect.setLocation( rect.getX()+5, rect.getY() ); rect.repaint(); }

© 2006 Pearson Addison-Wesley. All rights reserved do loop Syntax do { loopBody ; } while ( condition ) where loopBody is a sequence of zero or more instructions and condition is a Boolean expression Semantics (how to execute a do instruction) 2) Evaluate condition. 3.b) If condition is false, then terminate loop. 3.a) If condition is true, then return to Step 1. 1) execute loopBody.

© 2006 Pearson Addison-Wesley. All rights reserved Nested Loops A nested loop is one loop is within the body of another. int a, b, c; a = 1; while (a != 101) { b = 1; while (b != 101) { c = 1; while (c != 101) { System.out.println(a + “, “ + b + “, “ +c); c++; } b++; } a++; } int a, b, c; a = 1; while (a != 101) { b = 1; while (b != 101) { c = 1; while (c != 101) { System.out.println(a + “, “ + b + “, “ +c); c++; } b++; } a++; }