Lecture 4 Loops. Looping Constructs The while Loop.

26
Lecture 4 Loops

Transcript of Lecture 4 Loops. Looping Constructs The while Loop.

Page 1: Lecture 4 Loops. Looping Constructs The while Loop.

Lecture 4Loops

Page 2: Lecture 4 Loops. Looping Constructs The while Loop.

Looping Constructs

Page 3: Lecture 4 Loops. Looping Constructs The while Loop.

The while Loop

Page 4: Lecture 4 Loops. Looping Constructs The while Loop.

public class DoubleInvestment{ public static void main(String[] args) { final double RATE = 5; final double INITIAL_BALANCE = 10000; final double TARGET = 2 * INITIAL_BALANCE; double balance = INITIAL_BALANCE; int year = 0; // Count the years required for the investment to double while (balance < TARGET) { year++; double interest = balance * RATE / 100; balance = balance + interest; } System.out.println("The investment doubled after " + year + " years."); }}

Years to Double Investment

Page 5: Lecture 4 Loops. Looping Constructs The while Loop.

Hand-Tracing Code Execution

This program computes the sum of the digits in an integer.

Page 6: Lecture 4 Loops. Looping Constructs The while Loop.

Example: The Number Guessing GameAlgorithm Design

The Problem: Write a program that guesses the user's secret number between 1 and 100. The program should be able to guess the number in seven tries or less.

The Algorithm:

1. Set the initial range between lo = 0 and hi = 101

2. The program makes a guess in the middle of the range

guess = (lo + hi)/2

3. If guess is too high, let hi = guess

4. If guess is too low, let lo = guess

5. If guess is correct claim victory and quit else return to Step 2.

Page 7: Lecture 4 Loops. Looping Constructs The while Loop.

Example: The Number Guessing GameImplementation

Let the game begin...

This is the setup

Page 8: Lecture 4 Loops. Looping Constructs The while Loop.

Example: The Number Guessing GameAnalysis

Starting with 100 possible answers, each wrong guess eliminates 1/2 of the possibilities.

Starting with 100, how many times would be have to divide by 2 before the resulting value is <= 1?

= 1

100=2𝑛

n =

n = 6.643856...

100 -> 50 -> 25 -> 12 -> 6 -> 3 -> 1

Page 9: Lecture 4 Loops. Looping Constructs The while Loop.

Example: Building a StopWatch

Whenever currentTimeMillis() is called it returns the current time in milliseconds.

The stopwatch gets the current time when the first OK is pressed (startTime) and it gets the current time again when the second OK is pressed (finishTime).

The elapsed time is computed as the difference between these two times converted to seconds.

Page 10: Lecture 4 Loops. Looping Constructs The while Loop.

Example: Building a StopWatch

Whenever currentTimeMillis() is called it returns the current time in milliseconds.

The stopwatch gets the current time when the first OK is pressed (startTime) and it gets the current time again when the second OK is pressed (finishTime).

The elapsed time is computed as the difference between these two times converted to seconds.

Page 11: Lecture 4 Loops. Looping Constructs The while Loop.

A Delay Timer

This method is very useful for controlling the speed of execution of a program. Control remains in the do-while( ) loop for delMsec milliseconds.

time delay in milliseconds

setting the time when loop is entered

stay in the loop for delMsec (Msec)

Page 12: Lecture 4 Loops. Looping Constructs The while Loop.

data.txt

Example: Reading Integers from a Textfile

necessary when readingdata from a file

Blanks, carriage returns, and other white-space will be ignored by Scanner inputs for numeric data types such as integer

Page 13: Lecture 4 Loops. Looping Constructs The while Loop.

for LoopThe for-loop is the oldest looping construct. It has been implemented in every procedural high-level programming language since FORTRAN (1957). For-loops are commonly used when you know the number of iterations to be performed before beginning the loop.

Page 14: Lecture 4 Loops. Looping Constructs The while Loop.
Page 15: Lecture 4 Loops. Looping Constructs The while Loop.

for Loop Examples

Page 16: Lecture 4 Loops. Looping Constructs The while Loop.

for Loop Test Program

Page 17: Lecture 4 Loops. Looping Constructs The while Loop.

The do while Loop

Page 18: Lecture 4 Loops. Looping Constructs The while Loop.

do while Test Program

Page 19: Lecture 4 Loops. Looping Constructs The while Loop.

Nested Loops

body of outer loop executes 10 times

body of inner loop executes 10 timeseach time it is called by the outer loop

inner loop prints 10 stars on a line

outer loop executes inner loop to print a line of stars and then starts a new line

Page 20: Lecture 4 Loops. Looping Constructs The while Loop.

more Nested Loop Examples

Page 21: Lecture 4 Loops. Looping Constructs The while Loop.

even more Nested Loop Examples

Page 22: Lecture 4 Loops. Looping Constructs The while Loop.

Multiplication Table 1 2 3 4 5 6 7 8 9-----------------------------------------1 | 1 2 3 4 5 6 7 8 92 | 2 4 6 8 10 12 14 16 183 | 3 6 9 12 15 18 21 24 274 | 4 8 12 16 20 24 28 32 365 | 5 10 15 20 25 30 35 40 456 | 6 12 18 24 30 36 42 48 547 | 7 14 21 28 35 42 49 56 638 | 8 16 24 32 40 48 56 64 729 | 9 18 27 36 45 54 63 72 81

Multiplication Table Generator

Page 23: Lecture 4 Loops. Looping Constructs The while Loop.

Greatest Common Divisor

Page 24: Lecture 4 Loops. Looping Constructs The while Loop.

Using break & continue

x = 369x = 531x = 89x = 453x = 801x = 777x = 28x = 241x = 597x = 989x = 657x = 597x = 335Must have gotten a 42

break - break out of the current loop

continue - continue immediately to the next iteration of the loop

Page 25: Lecture 4 Loops. Looping Constructs The while Loop.

Controlling Loops with Sentinel Values

Page 26: Lecture 4 Loops. Looping Constructs The while Loop.