Repetition Chapter 6 12/06/16 & 12/07/16 1 1

Slides:



Advertisements
Similar presentations
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Advertisements

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.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
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:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
Chapter 4: Control Structures II
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Chapter 5 Loops.
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 II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Chapter 5: Control Structures II
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
Introduction Chapter 1 1/22/16. Check zyBooks Completion Click on the boxes for each section.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Wrapper Classes Debugging Interlude 1
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Chapter 4 – Loops Dr. Larry G. Thomas – University of Toledo / LCCC
Loops.
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Loop Structures.
Chapter 5: Control Structure
Repetition-Counter control Loop
Lecture 07 More Repetition Richard Gesick.
Computer Science 101 While Statement.
Lecture 4B More Repetition Richard Gesick
Outline Altering flow of control Boolean expressions
Module 4 Loops.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
CSC115 Introduction to Computer Programming
Suggested self-checks:
Repetition Statements (Loops) - 2
Repetition Statements
Building Java Programs
Outline Boolean Expressions The if Statement Comparing Data
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.
Building Java Programs
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Repetition Chapter 6 12/06/16 & 12/07/16 1 1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 1 1

Chapter Contents The Logic of a Loop The while Statement A Problem Solved: A Guessing Game Errors in Loops Off-by-One Errors Infinite Loops Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 2 2

The Logic of a Loop Iteration  repeat a group of statements Use a controlling statement Figure 12-1, Steps of a typical loop Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 3 3

The while Statement Controls the execution of a while loop Syntax Logic of a while statement Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 4 4

The while Statement Consider – exactly what numbers are printed by this example? Note often used style of incrementing counter = counter + 1; // and counter++; // have same result Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 5 5

The while Statement Counting loops Sentinel controlled loops Loop must run specific number of times Variable incremented/decremented required number of times Sentinel controlled loops Task must be repeated until a certain condition is true That condition is the sentinel Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 6 6

A Guessing Game One person (computer) thinks of a number Other person tries to guess the number First person responds “too high” “too low” “that’s it” Process repeats until guess is correct We seek a Java program to play the game Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 7 7

A Guessing Game Pseudocode solution Let's write the code. 8 8 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 8 8

Off by One Errors Common mistake in loop design Incorrect condition is tested Possible misuse of ≤ vs < Incorrect initial value Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 9 9

Off by One Errors Placement of the increment statement //This main() is suppose to count by 10 from 10 to 100 public static void main(String[] args) { int n = 10; while(n <= 100){ n = n + 10; System.out.println(n); } See: MisplacedInc.java Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 10 10

public class ErrorMisplaceInc { //This program is suppose to count by 10 from 10 to 100 //It still has problems. public static void main(String[] args) { int n = 10; while(n != 99) { System.out.println(n); n = n + 10; } 11

Infinite Loops More mistakes in loop design Again, incorrect condition tested Check for equality that can never happen Checking for equality with reals Should check for “closeness” Missing increment/decrement statement Extra semicolon Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 12 12

Question: What is the ouput? int num =0, max = 20; while (num < max){ System.out.println(num); num = num + 4; } 13 13

Participation Imagine a program that read the population of a city using the following statements: System.out.println(“Enter the population:”); Int population = scan.nextInt(); Write a while loop after these statements that ensures the population is positive. If the user enters a population that is either negative or zero, ask the user to enter a non-negative value. Keep asking until a non-negative is entered. Echo output the number that is finally stored. 14 14

Strings charAt(n) – returns char at position n. Line.charAt(4) →'g'

Participation Work with a partner. Write a program that inputs a word. Use a while loop and the charAt() method to output the word spelled backwards. Sample execution: Input:desserts Output:stressed 16 16

Estimation of Square Root (Skip) Can not discuss finding roots as the text does because it requires calculus. Can use an algorithm for finding the square root of a number that is based on the same principle. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 17 17

Estimation of Square Start with an estimate. Get another estimate based on that estimate. Repeat this until the two estimates are within the require error tolerance. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 18 18

Estimation of Square Input s, tolerance x = s //First estimate for x is the input newX = ½(x+ s/x) // Second estimate While( |newX – x| < tolerance) X = newX //Keep new est. newX = ½(x+ s/x) //Find next est. 5)Output x “is the sqrt of “ s Try it with 7. Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 19 19

Nested Loops(Skip) Consider code to generate a two dimensional pattern of asterisks Nested loop solution Inner loop displays each star in a row Outer loop drives the creation of the rows View code to create pattern of stars, Listing 12-2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 20 20

Scope of a Variable(skip) Scope  portion of program where variable can be used Figure 12-13 Scope of Variables Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 21 21

Repetition Chapter 6 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010 22 22