Building java programs, chapter 5 Program logic and indefinite loops.

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Advertisements

Building Java Programs
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
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.
Copyright 2008 by Pearson Education 1 Midterm announcements Next week on Friday May 8 Must bring an ID Open book, open notes, closed electronics Must attend.
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
Building Java Programs Chapter 5 Program Logic and Indefinite Loops.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Topic 14 while loops and loop patterns Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Loop - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 1.
1 Fencepost loops suggested reading: The fencepost problem Problem: Write a static method named printNumbers that prints each number from 1 to a.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
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 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
CS 112 Introduction to Programming Loop Patterns: break; Fencepost Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
CS 112 Introduction to Programming Program Analysis; Fencepost Loops Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Categories of loops definite loop: Executes a known number of times.
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
Lecture 5: Program Logic
Lecture 7: Input and Miscellaneous
Chapter 5: Control Structures II
Repetition-Counter control Loop
Chapter 5: Control Structures II
CSc 110, Autumn 2016 Lecture 12: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Spring 2017 Lecture 11: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
Building Java Programs Chapter 4
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Fencepost loops reading: 4.1.
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
Topic 14 while loops and loop patterns
Building Java Programs
Building Java Programs
Control Statements Loops.
Building Java Programs
Loop problem Write a method printLetters that prints each letter from a word separated by commas. For example, the call: printLetters("Atmosphere") should.
Building Java Programs
CIS 110: Introduction to Computer Programming
Building Java Programs
Building Java Programs
Control Statements Loops.
Repetition Statements
Building Java Programs
Building Java Programs
Indefinite loop variations
Building Java Programs
Building Java Programs
CSE 142, Spring 2013 Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
Building Java Programs
Building Java Programs
Building Java Programs
Presentation transcript:

Building java programs, chapter 5 Program logic and indefinite loops

Announcements public static int exam1Score() { if (homeworkScore() > 0.80 && notebookCheck() == 1.0 && CompletedExtraAssignedPracticeIts*) { int exam2 = scoreOnExam2(); int exam1 = Math.max(exam1, exam2); return exam1; } * 6 unassigned Practice-It questions from each of the Chapters 3, 4, 5 and 6. Due on the day of the exam 2.

At the end of the class, you will be able to Differentiate between definite and indefinite loops. Understand the syntax of while and do-while loops Use simple while and do-while loops in your own programs

Categories of loops definite loop: Executes a known number of times. – The for loops we have seen are definite loops. Print "hello" 10 times. Find all the prime numbers up to an integer n. Print each odd number between 5 and 127. indefinite loop: The number of times its body repeats is not known in advance. Prompt the user until they type a non-negative number. Print random numbers until a prime number is printed. Repeat until the user has types "q" to quit.

While loop Repeatedly executes its body as long as a logical test is true.

While loop syntax while (test) { statement(s); }

While loop example int num = 1; // initialization while (num <= 200) { // test System.out.print(num + " "); num = num * 2; // update } // output:

Converting for loops to while loops Any for loop of the following form: for ( ; ; ) { ; } can be replaced by a while loop of the following form: ; while ( ) { ; }

Converting for loops to while loops for (int i = 0; i < 10; i++) { System.out.println(i); } int i = 0; while(i < 10) { System.out.println(i); i++; }

Do-while loops Sometimes you want to do your test at the end Example: int sum = 0; do { int num = console.nextInt(); sum += num; // body } while (sum <= 200); // test System.out.print(sum);

Homework for chapter 5 HomeworkAssigned onDue on Read BJP 5.1 and write notes11/04/201411/05/2014 Practice It : SC 1, 2, 3, 411/04/201411/05/2014

At the end of the class, you will be able to Define and write your own Fence-Post and Sentinel loops Define and write your own forever loops Create an object of the Random Class and use its methods

A deceptive problem... Write a method printNumbers that prints each number from 1 to a given maximum, separated by commas. For example, the call: printNumbers(5) should print: 1, 2, 3, 4, 5

Flawed solution 1 public static void printNumbers(int max) { for (int i = 1; i <= max; i++) { System.out.print(i + ", "); } System.out.println(); // to end the line of output } – Output from printNumbers(5) : 1, 2, 3, 4, 5,

Flawed solution 2 public static void printNumbers(int max) { for (int i = 1; i <= max; i++) { System.out.print(", " + i); } System.out.println(); // to end the line of output } – Output from printNumbers(5) :, 1, 2, 3, 4, 5

Fence post analogy We print n numbers but need only n - 1 commas. Similar to building a fence with wires separated by posts: – If we use a flawed algorithm that repeatedly places a post + wire, the last post will have an extra dangling wire. for (length of fence) { place a post. place some wire. }

Fencepost loop Add a statement outside the loop to place the initial "post." – Also called a fencepost loop or a "loop-and-a-half" solution. place a post. for (length of fence - 1) { place some wire. place a post.}

Fencepost method solution – correct solution public static void printNumbers(int max) { System.out.print(1); for (int i = 2; i <= max; i++) { System.out.print(", " + i); } System.out.println(); } Alternate solution: Either first or last "post" can be taken out: public static void printNumbers(int max) { for (int i = 1; i <= max - 1; i++) { System.out.print(i + ", "); } System.out.println(max); }

Sentinel loops sentinel: A value that signals the end of input. sentinel loop: Repeats until a sentinel value is seen. Example: Write a program that prompts the user for numbers until the user types -1, then outputs their sum. – (In this case, -1 is the sentinel value.) Enter a number (-1 to quit): 10 Enter a number (-1 to quit): 20 Enter a number (-1 to quit): 30 Enter a number (-1 to quit): -1 The sum is 60

What is wrong with this? Scanner console = new Scanner(System.in); int sum = 0; int number = 1; // "dummy value", anything but -1 while (number != -1) { System.out.print("Enter a number (-1 to quit): "); number = console.nextInt(); sum = sum + number; } System.out.println("The total is " + sum);

Correct solution Scanner console = new Scanner(System.in); int sum = 0; // pull one prompt/read ("post") out of the loop System.out.print("Enter a number (-1 to quit): "); int number = console.nextInt(); while (number != -1) { sum = sum + number; System.out.print("Enter a number (-1 to quit): "); number = console.nextInt(); } System.out.println("The total is " + sum);

Forever loops Called "forever" loops because their header's boolean test is often changed to a trivial true. Can be used to write a loop whose test is in the middle. break statement: Immediately exits a loop. Example: int num = 0; while (true) { if (num == 5) { break; } System.out.println(num); num++; } System.out.println(“Forever is over!”);

Forever loop syntax while (true) { ; if ( ) { break; } ; }

The Random class Java has a class named Random whose objects generate pseudo-random numbers. – Class Random is found in the java.util package. import java.util.*;

In your eclipse Random r = new Random(); if (r.nextInt(10)%2 == 0) { System.out.println("random int " + r.nextInt(10) + " is even"); } else { System.out.println("random int " + r.nextInt(10) + " is odd"); } Hint: don’t forget to import java.util.*;

In your eclipse Random r = new Random(); if (r.nextInt(10)%2 == 0) { System.out.println("random int " + r.nextInt(10) + " is even"); } else { System.out.println("random int " + r.nextInt(10) + " is odd"); } nextInt generated a different random number with each call. Just like scanner.nextInt() reads a new int with every call. how do we fix this?

In your eclipse – correct solution Random r = new Random(); int randomInt = r.nextInt(10); if (randomInt %2 == 0) { System.out.println("random int " + randomInt + " is even"); } else { System.out.println("random int " + randomInt + " is odd"); }

Homework for chapter 5 HomeworkAssigned onDue on Read BJP 5.1 and write notes11/04/201411/05/2014 Practice It : SC 1, 2, 3, 411/04/201411/05/2014 Practice It : SC 6,711/04/201411/05/2014 Practice It : Ex 511/04/201411/05/2014