1 / 26

Lecture 4 Loops

Lecture 4 Loops. Looping Constructs. The while Loop. Years to Double Investment.

jeb
Download Presentation

Lecture 4 Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 4 Loops

  2. Looping Constructs

  3. The whileLoop

  4. Years to Double Investment publicclass DoubleInvestment{publicstaticvoid main(String[] args) {finaldouble RATE = 5;finaldouble INITIAL_BALANCE = 10000;finaldouble TARGET = 2 * INITIAL_BALANCE;double balance = INITIAL_BALANCE;int year = 0;// Count the years required for the investment to doublewhile (balance < TARGET) { year++;double interest = balance * RATE / 100; balance = balance + interest; } System.out.println("The investment doubled after " + year + " years."); }}

  5. Hand-Tracing Code Execution This program computes the sum of the digits in an integer.

  6. Example: The Number Guessing Game Algorithm 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.

  7. Example: The Number Guessing Game Implementation This is the setup Let the game begin...

  8. Example: The Number Guessing Game Analysis 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 -> 50 -> 25 -> 12 -> 6 -> 3 -> 1 n = n = 6.643856...

  9. 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.

  10. 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.

  11. A Delay Timer time delay in milliseconds setting the time when loop is entered stay in the loop for delMsec (Msec) This method is very useful for controlling the speed of execution of a program. Control remains in the do-while( ) loop for delMsec milliseconds.

  12. Example: Reading Integers from a Textfile necessary when reading data from a file data.txt Blanks, carriage returns, and other white-space will be ignored by Scanner inputs for numeric data types such as integer

  13. for Loop The 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.

  14. for Loop Examples

  15. for Loop Test Program

  16. The do while Loop

  17. do while Test Program

  18. Nested Loops body of outer loop executes 10 times body of inner loop executes 10 times each 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

  19. more Nested Loop Examples

  20. even more Nested Loop Examples

  21. Multiplication Table Generator Multiplication Table1 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

  22. Greatest Common Divisor

  23. Using break & continue x = 369 x = 531 x = 89 x = 453 x = 801 x = 777 x = 28 x = 241 x = 597 x = 989 x = 657 x = 597 x = 335 Must have gotten a 42 break- break out of the current loop continue- continue immediately to the next iteration of the loop

  24. Controlling Loops with Sentinel Values

More Related