1 / 12

General Condition Loop

A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change some value so that the condition becomes false. Otherwise, if the loop does run, it becomes an infinite loop. General Condition Loop. int a = 19; while (a > 0) {

amckay
Download Presentation

General Condition Loop

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. A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change some value so that the condition becomes false. Otherwise, if the loop does run, it becomes an infinite loop. General Condition Loop

  2. int a = 19; while (a > 0) { System.out.println(a % 2); a = a / 2; } Example

  3. Suppose that you want to take out a loan for $10,000, with 18% APR, and you're willing to make payments of $1,200/month. How long will it take you to pay off the loan and how much interest will you pay? An amortization table will show you how much you owe each month, the number of months to pay off the loan, and the total interest. Loop Exercise 1

  4. Mth Payment Amount 0 10000.00 1 1200.00 8950.00 2 1200.00 7884.25 3 1200.00 6802.51 4 1200.00 5704.55 5 1200.00 4590.12 6 1200.00 3458.97 7 1200.00 2310.86 8 1200.00 1145.52 9 1162.70 0.00 Total Interest = 762.70 Amortization Table

  5. The interest owed at the end of a month is: interest = amount * APR/1200 The amount owed at the end of a month is: amount += interest The payment is the either the fixed payment amount ($1,200 in this case) or the total amount, if the total amount is less than the fixed payment. The new amount is the amount - payment. Amortization Calculation

  6. Write a program that will read in the amount of the loan, the Annual Percentage Rate (APR), and the amount of the fixed payment, and then will calculate and print out the amortization table and then the total amount of interest paid on the loan. The program should prompt the user for the inputs and print out the table formatted properly with labeled columns. Program

  7. Enter amount of loan> 10000 Enter APR> 18 Enter monthly payment> 1200 Mth Payment Amount 0 10000.00 1 1200.00 8950.00 2 1200.00 7884.25 3 1200.00 6802.51 4 1200.00 5704.55 5 1200.00 4590.12 ... 9 1162.70 0.00 Total Interest = 762.70 Sample Output

  8. A prime number is an integer greater 1 than is divisible only by 1 and itself. For example, 2 is a prime, 3 is a prime, but 4 is not a prime (4 is divisible by 2). Write a program to print out all the prime numbers between 1 and 100, printing ten numbers per line. Loop Exercise 2

  9. Primes between 1 and 100 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 Sample Output

  10. To check if one number divides another, just check if the remainder after division is 0. For example if (i % j == 0) then j is a factor of i (and if j is neither i nor 1, i is not prime). Hint: It would be helpful to use a boolean variable, isPrime, to record whether or not the number is prime. Prime Calculation

  11. The third Java statement for implementing loops in the Do-While statement, which looks like: do <stmt> while(<test>) The only difference between the Do-While statement and the While Statement is that the test is checked after the loop body is executed. Thus, the loop body is always done at least one time. Do-While Loop

  12. One common usage of the Do-While statement is to read in input until a valid value is read. For example, in the guessing game, ask the user for a number be 1 and 10 (inclusive): do { System.out.print("Pick a number between 1 and 10 >"); n = sc.nextInt(); } while(n < 1 || n > 10); Example

More Related