©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 6 - 1 Chapter 6 Repetition Statements.

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

CS0007: Introduction to Computer Programming
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.
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.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 6 Repetition Statements Animated Version.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to Implement repetition control.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter Chapter 6 Repetition Statements. Objectives Understand repetition control (loop ) statements in Java: while statement. do-while statement.
Chapter 6 Repetition. Topics Some additional operators –increment and decrement –assignment operators Repetition –while –do-while –for Random numbers.
©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.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
Java Programming: From the Ground Up
Loops and Iteration for Statements, while Statements and do-while Statements.
© 2004 Pearson Addison-Wesley. All rights reserved February 17, 2006 The ‘while’ Statement ComS 207: Programming I (in Java) Iowa State University, SPRING.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
CS101 Computer Programming I Chapter 4 Extra Examples.
Repetition Statements while and do while loops
Chapter 5: Control Structures II
Repetition. Control of Flow SEQUENCE SELECTION (if..else, switch…case) REPETITION.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
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)
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
While ( number
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Repetition Statements
Chapter 4 Repetition Statements (loops)
Loop Structures.
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Intro to OOP with Java, C. Thomas Wu
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.
Looping and Repetition
Outline Altering flow of control Boolean expressions
Introduction to Object-Oriented Programming with Java--Wu
Building Java Programs
Outline Boolean Expressions The if Statement Comparing Data
Chap 7. Advanced Control Statements in Java
Loops and Iteration CS 21a: Introduction to Computing I
Chapter 7 Repetition Statements
Looping and Repetition
Presentation transcript:

©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 Definition Often need to execute block of code multiple times –Repetition statements (loops) are used for this Loop termination controlled by a Boolean expression –Counting loops execute a fixed number of times. –Sentinel loops terminate on encountering a sentinel value Java has three loop structures

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Pre-Test loop int sum = 0, number = 1 number <= 100 ? false sum = sum + number; number = number + 1; true

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter while ( number <= 100 ) { sum = sum + number; number = number + 1; } Syntax for the while Statement while ( ) Statement (loop body) Boolean Expression

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Some Examples Keeps adding the numbers 1, 2, 3, … until the sum becomes larger than 1,000,000. Computes the product of the first 20 odd integers. int sum = 0, number = 1; while ( sum <= ) { sum = sum + number; number = number + 1; } 1 int product = 1, number = 1, count = 20, lastNumber; lastNumber = 2 * count - 1; while (number <= lastNumber) { product = product * number; number = number + 2; } 2

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter String inputStr; int age; inputStr = JOptionPane.showInputDialog(null, "Your Age (between 0 and 130):"); age = Integer.parseInt(inputStr); while (age 130) { JOptionPane.showMessageDialog(null, "Invalid age : Please try again."); inputStr = JOptionPane.showInputDialog(null, "Your Age (between 0 and 130):"); age = Integer.parseInt(inputStr); } Example: Testing Input Data Priming Read

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Common Loop Pitfalls infinite loops : control expression can never be false –no change is made to the variables in the control expression –the control expression is too restrictive off-by-one errors : one too many or one too few iterations of the loop

Examples of Infinite loops 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 int product = 0; while ( product < ) { product = product * 5; } 1

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Overflow An overflow error occurs when you attempt to assign a value larger than the maximum value the variable can hold. In Java, an overflow does not cause program termination. –For float and double, a value that represents infinity is assigned to the variable. –For int, the value “wraps around” and becomes negative.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Round-off Errors -> Infinite Loops 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 f; } //eight 3s 2 float count = 0.0f; while ( count != 1.0f ) { count = count f; }//seven 3s 1

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Round-off Errors -> Off-By-One Errors int result = 0; double cnt = 1.0; while (cnt <= 10.0){ cnt += 1.0; result++; } System.out.println(result); 1 int result = 0; double cnt = 0.0; while (cnt <= 1.0){ cnt += 0.1; result++; } System.out.println(result); 2 Using Real Numbers Loop 1 prints out 10, as expected, but Loop 2 prints out 11. The value 0.1 cannot be stored precisely in computer memory

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Off-by-One Errors Goal: Execute the loop body 10 times. count = 1; while ( count < 10 ){... count++; } 1 count = 0; while ( count <= 10 ){... count++; } 3 count = 1; while ( count <= 10 ){... count++; } 2 count = 0; while ( count < 10 ){... count++; } 4 13 andexhibit off-by-one error.

Post-Test Loop int sum = 0, number = 1 sum += number; number++; sum <= ? true false

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter do { sum += number; number++; } while ( sum <= ); Syntax for the do-while Statement do while ( ) ; Statement (loop body) Boolean Expression

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Counting Loop i = 0; false number =... ; sum += number; true i ++; i < 20 ?

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter for (i = 0; i < 20; i++ ) { number = scanner.nextInt(); sum += number; } Syntax for the for Statement for ( ; ; ) Initialization Boolean Expression Increment Statement (loop body)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter for Loop Examples for (int i = 0; i < 100; i += 5) 1 i = 0, 5, 10, …, 95 for (int j = 2; j < 40; j *= 2) 2 j = 2, 4, 8, 16, 32 for (int k = 100; k > 0; k--) ) 3 k = 100, 99, 98, 97,..., 1

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Nested Loops can be nested We can use nested loops to generate tables for (int i=1; i<=10; i++) { for (int j=1; j<=10; j++) System.out.print(i * j + "\t"); System.out.println(); } How many times does the loop execute? –The inner loop executes 10 times for each value of i

Input Loops We often need to read an unknown number of values of a particular type. –How do you know when to stop? The Scanner class has a set of methods for determining what comes next in the input –boolean hasNext() –boolean hasnextLine() –boolean hasNextInt() –boolean hasNextDouble()

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Recursion Recursion is another technique that can be used for iteration A recursive method is one that calls itself n! = n * (n-1) * (n-2) * … * 2 * 1 int factorial ( int n) { if (n<=1) return 1; else return n * factorial( n-1); } Recursion is generally less efficient than loops so use it only when you need to

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Finding GCD