Introduction to Object-Oriented Programming with Java--Wu

Slides:



Advertisements
Similar presentations
Chapter 7 - Iteration. Chapter Goals Program repitiation statements – or loops – with the for, while, and do-while statements Program repitiation statements.
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Repetition Chapter 4: Control structures. Introduction to OOPDr. S. GANNOUNI & Dr. A. TOUIRPage 2 Loop Statements After reading and studying this Section,
June 10, 2015ICS102: while & do-while1 while and do-while Statements.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter 5: Loops and Files.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©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.
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
Loops and Iteration for Statements, while Statements and do-while Statements.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
While ( number
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Chapter 5: Loops Tarik Booker CS 201 California State University, Los Angeles.
Repetition Statements
Topic 4: Looping Statements
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Lecture 7: Repeating a Known Number of Times
Loop Structures.
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Chapter 4 Control structures and Loops
Arrays, For loop While loop Do while loop
Looping and Repetition
MSIS 655 Advanced Business Applications Programming
Outline Altering flow of control Boolean expressions
Java Programming Loops
Repetition Control Structure
Chapter 6: Repetition Statements
Java Programming Loops
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Loops and Iteration CS 21a: Introduction to Computing I
Chapter 7 Repetition Statements
Looping and Repetition
Presentation transcript:

Introduction to Object-Oriented Programming with Java--Wu Chapter 7 The while Statement int sum = 0, number = 1; while ( number <= 100 ) { sum = sum + number; number = number + 1; } These statements are executed as long as number is less than or equal to 100. There are basically two types of terminating the loop: 1. count-controlled and 2. sentinel-controlled. Count-controlled loops terminate the execution of the loop after the loop body is executed for a fixed number of times. Sentinel-controlled loops terminate the execution of the loop after one of the designated values called a sentinel is encountered. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Syntax for the while Statement Chapter 7 Syntax for the while Statement while ( <boolean expression> ) { <statement> } Boolean Expression while ( number <= 100 ) { sum = sum + number; number = number + 1; } Statement (loop body) © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 7 Control Flow of while int sum = 0, number = 1 number <= 100 ? sum = sum + number; number = number + 1; true false © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Example Programs Puuuurginooo … out to reality … While10000.java Puuuurginooo … out to reality … WhileInput.java © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 7 while Loop Pitfall - 1 int product = 0; while ( product < 500000 ) { product = product * 5; } 1 Infinite Loops Both loops will not terminate because the boolean expressions will never become false. int count = 1; while ( count != 10 ) { count = count + 2; } 2 Note: In theory, this while statement is an infinite loop, but in programming languages other than Java, this loop will eventually terminate because of an overflow error. An overflow error will occur if you attempt to assign a value larger than the maximum value the variable can hold. When an overflow error occurs, the execution of the program is terminated in almost all programming languages. With Java, however, an overflow will not cause the program termination. When an overflow occurs in Java, a value that represents infinity (IEEE 754 infinity, to be precise) is assigned to a variable and no abnormal termination of a program will happen. Also, in Java an overflow occurs only with float and double variables; no overflow will happen with int variables. When you try to assign a value larger than the maximum possible integer an int variable can hold, the value “wraps around” and becomes a negative value. Whether the loop terminates or not because of an overflow error, the logic of the loop is still an infinite loop, and we must watch out for it. When you write a loop, you must make sure that the boolean expression of the loop will eventually become false. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 7 while Loop Pitfall - 2 float count = 0.0f; while ( count != 1.0f ) { count = count + 0.3333333f; } 1 Using Real Numbers Loop 2 terminates, but Loop 1 does not because only an approximation of a real number can be stored in a computer memory. float count = 0.0f; while ( count <= 1.0f ) { count = count + 0.3333333f; } 2 Although 1/3 + 1/3 + 1/3 == 1 is mathematically true, the expression 1.0/3.0 + 1.0/3.0 + 1.0/3.0 in computer language may or may not get evaluated to 1.0 depending on how precise the approximation is. In general, avoid using real numbers as counter variables because of this imprecision. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 7 while Loop Pitfall - 3 Goal: Execute the loop body 10 times. count = 1; while (count < 10) { . . . count++; } 1 count = 1; while (count <= 10) { . . . count++; } 2 count = 0; while (count <= 10) { . . . count++; } 3 count = 0; while (count < 10) { . . . count++; } 4 Yes, you can write the desired loop as count = 1; while (count != 10 ) { ... count++; } but this condition for stopping the count-controlled loop is dangerous. We already mentioned about the potential trap of an infinite loop. 1 3 and exhibit off-by-one error. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Example Programs Grunk … out to reality … WhileLog.java Grunk … out to reality … WhileBoolean.java © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu

The do-while Statement Chapter 7 The do-while Statement int sum = 0, number = 1; do { sum += number; number++; } while ( sum <= 1000000 ); These statements are executed as long as sum is less than or equal to 1,000,000. The following is the routine presented earlier that inputs a person’s age using the do–while statement. do { age = inputBox.getInteger("Your Age (between 0 and 130):"); if (age < 0 || age > 130) { messageBox.show("An invalid age was entered. " + "Please try again."); } } while (age < 0 || age > 130); This code is not as good as the version using the while statement it includes an if statement inside its loop body and the if test is repeating the same boolean expression of the do–while. Since the loop body is executed repeatedly, it is important not to include any extraneous statements. Moreover, duplicating the testing conditions tends to make the loop statement harder to understand. For this example, we can avoid the extra test inside the loop body and implement the control flow a little more clearly by using a while statement. In general, the while statement is more frequently used than the do–while statement. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Syntax for the do-while Statement Chapter 7 Syntax for the do-while Statement do { <statement> } while (<boolean expression>); do { sum += number; number++; } while (sum <= 1000000); Statement (loop body) Boolean Expression © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Control Flow of do-while Chapter 7 Control Flow of do-while int sum = 0, number = 1 sum += number; number++; true sum <= 1000000 ? false © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Example Programs Orque … out to reality … DoWhileInput.java Orque … out to reality … DoWhileDrink.java © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu

Pre-test vs. Post-test loops Chapter 7 Pre-test vs. Post-test loops Use a pre-test loop for something that may be done zero times Use a post-test for something that is always done at least once © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Checklist for Repetition Control Chapter 7 Checklist for Repetition Control Watch out for the off-by-one error (OBOE). Make sure the loop body contains a statement that will eventually cause the loop to terminate. Make sure the loop repeats exactly the correct number of times. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 7 The for Statement int i, sum = 0, number; for (i = 0; i < 20; i++) { number = inputBox.getInteger(); sum += number; } These statements are executed for 20 times ( i = 0, 1, 2, … , 19). © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Syntax for the for Statement Chapter 7 Syntax for the for Statement for ( <initialization>; <boolean expression>; <update> ) <statement> Initialization Boolean Expression Update for ( i = 0 ; i < 20 ; i++ ) { number = inputBox.getInteger(); sum += number; } The <initialization> component also can include a declaration of the control variable. We can do something like this: for (int i = 0; i < 10; i++) instead of int i; for (i = 0; i < 10; i++) Statement (loop body) © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Chapter 7 Control Flow of for i = 0; i < 20 ? false number = inputBox.getInteger( ); sum += number; true i ++; © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Introduction to Object-Oriented Programming with Java--Wu Example Programs Poodyplat … out to reality … ForPrint.java Poodyplat … out to reality … ForPower.java Poodyplat … out to reality … ForFibonacci.java © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu

The Nested-for Statement Chapter 7 The Nested-for Statement Nesting a for loop inside another is a common technique Generate the following table using nested-for statements. ForTable.java © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

Indefinite vs. Definite loops Chapter 7 Indefinite vs. Definite loops For loops and while loops are exchangeable but Use a for loop when the number of iterations is definite Use a while or do-while when the number of iterations depends on statements in the loop body © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu