The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 6: Iteration 1 Chapter 6 Iteration.
Chapter 7 – Arrays.
Computer Science A 4 13/3. Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Chapter 6 Iteration Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Iteration and Loop Statements Horstmann Chapter 7 Loop statements control repeated execution of a block of statements Each time the statements in the block.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Datalogi A 5: 6/10. while Loops Executes a block of code repeatedly A condition controls how often the loop is executed while (condition) statement; Most.
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.
Chapter 6  Iteration 1 Chapter 6 Iteration. Chapter 6  Iteration 2 Chapter Goals  To be able to program loops with while and for (sometimes do ) statements.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
For each primitive type there is a wrapper class for storing values of that type: Double d = new Double(29.95); Wrapper Classes Wrapper objects can be.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Java Programming: From the Ground Up
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Chapter 6 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
ICOM 4015 Fall 2008 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. ICOM 4015: Advanced Programming Lecture 6 Chapter.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
Copyright © 2013 by John Wiley & Sons. All rights reserved. LOOPS CHAPTER Slides by Donald W. Smith TechNeTrain.com Final Draft Oct 30,
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 6 - Loops.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 6 - Loops.
 Executes a block of code repeatedly  A condition controls how often the loop is executed  Most commonly, the statement is a block statement (set of.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
 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 J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 6 – Iteration.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 5: Control Structures II
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
Chapter 6 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
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.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Copyright © 2013 by John Wiley & Sons. All rights reserved. LOOPS CHAPTER Slides by James Tam Department of Computer Science, University of Calgary April.
Chapter 7 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
Slides by Evan Gallagher
Slides by Evan Gallagher
Chapter 8 – Arrays and Array Lists
Chapter Six: Iteration
Department of Computer Science
Chapter Goals To implement while, for, and do loops
Chapter 5: Control Structures II
Loop Structures.
Chapter 6 – Loops.
Repetition-Counter control Loop
Selected Topics From Chapter 6 Iteration
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
LOOPS.
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.
Outline Altering flow of control Boolean expressions
Chapter Six - Iteration
Chapter 7 Iteration.
Repetition Statements
Announcements Lab 3 was due today Assignment 2 due next Wednesday
6.2 for Loops Example: for ( int i = 1; i
Presentation transcript:

The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.

The while Loop The code: while (balance < targetBalance) { year++; double interest = balance * RATE / 100; balance = balance + interest; }

Syntax 6.1 while Statement

The while Loop For a variable declared inside a loop body: – Variable is created for each iteration of the loop – And removed after the end of each iteration while (balance < targetBalance) { year++; double interest = balance * RATE / 100; balance = balance + interest; } // interest no longer declared here

Trace balance = rate = 5; How many years until we reach targetBalance >= $20,000?

Trace targetBalance = 20000

1 /** 2 A class to monitor the growth of an investment that 3 accumulates interest at a fixed annual rate. 4 */ 5 public class Investment 6 { 7 private double balance; 8 private double rate; 9 private int year; /** 12 Constructs an Investment object from a starting balance and 13 interest rate. aBalance the starting balance aRate the interest rate in percent 16 */ 17 public Investment(double aBalance, double aRate) 18 { 19 balance = aBalance; 20 rate = aRate; 21 year = 0; 22 } 23 Continued

24 /** 25 Keeps accumulating interest until a target balance has 26 been reached. targetBalance the desired balance 28 */ 29 public void waitForBalance(double targetBalance) 30 { 31 while (balance < targetBalance) 32 { 33 year++; 34 double interest = balance * rate / 100; 35 balance = balance + interest; 36 } 37 } /** 40 Gets the current investment balance. the current balance 42 */ 43 public double getBalance() 44 { 45 return balance; 46 } 47 Continued

48 /** 49 Gets the number of years this investment has accumulated 50 interest. the number of years since the start of the investment 52 */ 53 public int getYears() 54 { 55 return year; 56 } 57 }

1 /** 2 This program computes how long it takes for an investment 3 to double. 4 */ 5 public class InvestmentRunner 6 { 7 public static void main(String[] args) 8 { 9 final double INITIAL_BALANCE = 10000; 10 final double RATE = 5; 11 Investment invest = new Investment(INITIAL_BALANCE, RATE); 12 invest.waitForBalance(2 * INITIAL_BALANCE); 13 int years = invest.getYears(); 14 System.out.println("The investment doubled after " 15 + years + " years"); 16 } 17 } Program Run: The investment doubled after 15 years

Common Error: Infinite Loops Example: – forgetting to update the variable that controls the loop int years = 1; while (years <= 20) { double interest = balance * RATE / 100; balance = balance + interest; }

Common Error: Infinite Loops Example: – incrementing instead of decrementing int years = 20; while (years > 0) { double interest = balance * RATE / 100; balance = balance + interest; years++; } loop runs forever – must kill program

Common Error: Off-by-One Errors Off-by-one error: a loop executes one too few, or one too many, times. int years = 0; while (balance < targetBalance) { years++; balance = balance * (1 + RATE / 100); } System.out.println("The investment doubled after " + year + " years."); Should years start at 0 or 1 ? Should the test be < or <= ?

Avoiding Off-by-One Error Look at a scenario with simple values: initial balance : $100 interest rate : 50% after year 1, the balance is $150 after year 2 it is $225, or over $200 so the investment doubled after 2 years the loop executed two times, incrementing years each time Therefore: years must start at 0, not at 1.

Avoiding Off-by-One Error interest rate : 100% after one year: balance is 2 * initialBalance loop should stop Therefore: must use < not <=

Problem Solving: Hand-Tracing What value is displayed? int n = 1729; int sum = 0; while (n > 0) { int digit = n % 10; sum = sum + digit; n = n / 10; } System.out.println(sum);

Hand-Tracing - Step by Step Step 1 Step 2

Hand-Tracing - Step by Step Step 3

Hand-Tracing - Step by Step Step 4

Hand-Tracing - Step by Step Step 5

Hand-Tracing - Step by Step Step 6

Hand-Tracing - Step by Step Step 7

Hand-Tracing - Step by Step Step 8

Hand-Tracing - Step by Step Step 9 Step 10 The sum, which is 19, is printed

The for Loop Could use a while loop controlled by a counter int counter = 1; // Initialize the counter while (counter <= 10) // Check the counter { System.out.println(counter); counter++; // Update the counter } Instead use a special type of loop called for loop for (int counter = 1; counter <= 10; counter++) { System.out.println(counter); }

Syntax 6.2 for Statement

The for Loop A for loop can count down instead of up: for (int c = 10; c >= 0; c--) The increment or decrement need not be in steps of 1: for (int c = 0; c <= 10; c = c + 2)

The for Loop If the counter variable is defined in the loop header, – It does not exist after the loop for (int counter = 1; counter <= 10; counter++) {... } // counter no longer declared here

The for Loop If you declare the counter variable before the loop, – You can continue to use it after the loop int counter; for (counter = 1; counter <= 10; counter++) {... } // counter still declared here

The for Loop To traverse all the characters of a string: for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); Process ch. } The counter variable i starts at 0, and the loop is terminated when i reaches the length of the string.

The for Loop To compute the growth of our savings account over a period of years, – Use a for loop because the variable year starts at 1 and then moves in constant increments until it reaches the target for (int year = 1; year <= numberOfYears; year++) { Update balance. }

The for Loop - Flowchart for (int year = 1; year <= numberOfYears; year++) { Update balance. }

1 /** 2 A class to monitor the growth of an investment that 3 accumulates interest at a fixed annual rate 4 */ 5 public class Investment 6 { 7 private double balance; 8 private double rate; 9 private int year; /** 12 Constructs an Investment object from a starting balance and 13 interest rate. aBalance the starting balance aRate the interest rate in percent 16 */ 17 public Investment(double aBalance, double aRate) 18 { 19 balance = aBalance; 20 rate = aRate; 21 year = 0; 22 } 23 Continued

24 /** 25 Keeps accumulating interest until a target balance has 26 been reached. targetBalance the desired balance 28 */ 29 public void waitForBalance(double targetBalance) 30 { 31 while (balance < targetBalance) 32 { 33 year++; 34 double interest = balance * rate / 100; 35 balance = balance + interest; 36 } 37 } 38 Continued

39 /** 40 Keeps accumulating interest for a given number of years. numberOfYears the number of years to wait 42 */ 43 public void waitYears(int numberOfYears) 44 { 45 for (int i = 1; i <= numberOfYears; i++) 46 { 47 double interest = balance * rate / 100; 48 balance = balance + interest; 49 } 50 year = year + n; 51 } /** 54 Gets the current investment balance. the current balance 56 */ 57 public double getBalance() 58 { 59 return balance; 60 } 61 Continued

62 /** 63 Gets the number of years this investment has accumulated 64 interest. the number of years since the start of the investment 66 */ 67 public int getYears() 68 { 69 return year; 70 } 71 }

1 /** 2 This program computes how much an investment grows in 3 a given number of years. 4 */ 5 public class InvestmentRunner 6 { 7 public static void main(String[] args) 8 { 9 final double INITIAL_BALANCE = 10000; 10 final double RATE = 5; 11 final int YEARS = 20; 12 Investment invest = new Investment(INITIAL_BALANCE, RATE); 13 invest.waitYears(YEARS); 14 double balance = invest.getBalance(); 15 System.out.printf("The balance after %d years is %.2f\n", 16 YEARS, balance); 17 } 18 } Program Run The balance after 20 years is

The do Loop Executes the body of a loop at least once and performs the loop test after the body is executed. Use for input validation int value; do { System.out.print("Enter an integer < 100: "); value = in.nextInt(); } while (value >= 100);

The do Loop int value; do { System.out.print( "Enter an integer < 100: "); value = in.nextInt(); } while (value >= 100);

Sentinel Values In the military, a sentinel guards a border or passage. In computer science, a sentinel value denotes the end of an input sequence or the border between input sequences.

Sentinel Values A sentinel value denotes the end of a data set, but it is not part of the data. Often times 0 is used as a sentinel value – keep accepting input until a 0 is typed If 0 is valid data, -1 is a common sentinel value

Application using Sentinel Values To compute the average of a set of salaries – use -1 to indicate termination Inside the loop – Read the input – process it if the input is not -1

1 import java.util.Scanner; 2 3 /** 4 This program prints the average of salary values that are terminated with a sentinel. 5 */ 6 public class SentinelDemo 7 { 8 public static void main(String[] args) 9 { 10 double sum = 0; 11 int count = 0; 12 double salary = 0; 13 System.out.print("Enter salaries, -1 to finish: "); 14 Scanner in = new Scanner(System.in); // Process data until the sentinel is entered 17 Continued

16 // Process data until the sentinel is entered while (salary != -1) 19 { 20 salary = in.nextDouble(); 21 if (salary != -1) 22 { 23 sum = sum + salary; 24 count++; 25 } 26 } // Compute and print the average if (count > 0) 31 { 32 double average = sum / count; 33 System.out.println("Average salary: " + average); 34 } 35 else 36 { 37 System.out.println("No data"); 38 } 39 } 40 } Continued

Boolean Sentinel Values System.out.print("Enter salaries, -1 to finish: "); boolean done = false; while (!done) { value = in.nextDouble(); if (value == -1) { done = true; } else { Process value } }

hasNext What happens if 0 and -1 are valid input? – in.hasNextDouble() returns false if the input is not a double – By the way, 1 can be read as double, but 1.0 cannot be read as an int System.out.print("Enter values, Q to quit: "); while (in.hasNextDouble()) { value = in.nextDouble(); Process value. }

The “Loop and a Half” Problem Sometimes a loop has to be terminated in the middle. Here are two different approaches: Use a Boolean variable boolean done = false; while (!done) { String input = in.next(); if (input.equals("Q")) { done = true; } else { Process data. } } Combine an assignment and a test in the loop condition while (!(input = in.next()).equals("Q")) { Process data. }

Common Loop Algorithm: Sum Sum - keep a running total: a variable to which you add each input value: double total = 0; while (in.hasNextDouble()) { double input = in.nextDouble(); total = total + input; }

Common Loop Algorithm: Average Average - count how many values you have: double total = 0; int count = 0; while (in.hasNextDouble()) { double input = in.nextDouble(); total = total + input; count++; } double average = 0; if (count > 0) { average = total / count; }

Common Loop Algorithm: Counting Matches Count how many spaces are in a string: int spaces = 0; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (ch == ' ’) { spaces++; } }

Common Loop Algorithm: Counting Matches Count how many words in the input have at most three letters: int shortWords = 0; while (in.hasNext()) { String input = in.next(); if (input.length() <= 3) { shortWords++; } }

Common Loop Algorithm: Maximum To find the largest value, update the largest value seen so far whenever you see a larger one. double largest = in.nextDouble(); while (in.hasNextDouble()) { double input = in.nextDouble(); if (input > largest) { largest = input; } }

Common Loop Algorithm: Minimum To find the smallest value, reverse the comparison. double smallest = in.nextDouble(); while (in.hasNextDouble()) { double input = in.nextDouble(); if (input < smallest) { smallest = input; } }

Common Loop Algorithm: Comparing Adjacent Values Check whether a sequence of inputs contains adjacent duplicates such as : double input = 0; while (in.hasNextDouble()) { double previous = input; input = in.nextDouble(); if (input == previous) { System.out.println("Duplicate input"); } }