430 likes | 685 Views
Loops. ISYS 350. Three Types of Loops. while loop do while loop for loop. An Infinite Loop. while (true){ System.out.println("enter loan:"); double loan=sc.nextDouble(); System.out.println("enter rate in %:"); double rate=sc.nextDouble();
E N D
Loops ISYS 350
Three Types of Loops • while loop • do while loop • for loop
An Infinite Loop while (true){ System.out.println("enter loan:"); double loan=sc.nextDouble(); System.out.println("enter rate in %:"); double rate=sc.nextDouble(); System.out.println("enter term in year:"); double term=sc.nextDouble(); double monPayment=(loan*rate/100/12)/(1-(Math.pow(1+rate/100/12,-12*term))); System.out.println("Monthly payment is: =" + monPayment); }
Using a Flag String flag="Y"; while (flag.equalsIgnoreCase("Y")){ System.out.println("enter loan:"); double loan=sc.nextDouble(); System.out.println("enter rate in %:"); double rate=sc.nextDouble(); System.out.println("enter term in year:"); double term=sc.nextDouble(); double monPayment=(loan*rate/100/12)/(1-(Math.pow(1+rate/100/12,-12*term))); System.out.println("Monthly payment is: =" + monPayment); System.out.println("Do you want to continue? (Y/N):"); flag=sc.next(); } System.out.println("Thank you for using payment calculator!"); Note: Flag may be declared as a boolean.
Flag Declared as boolean boolean repeatFlag=true; //while (repeatFlag) while (repeatFlag==true){ System.out.println("enter loan:"); double loan=sc.nextDouble(); System.out.println("enter rate in %:"); double rate=sc.nextDouble(); System.out.println("enter term in year:"); double term=sc.nextDouble(); double monPayment=(loan*rate/100/12)/(1-(Math.pow(1+rate/100/12,-12*term))); System.out.println("Monthly payment is: =" + monPayment); System.out.println("Do you want to continue? (Y/N):"); String YON=sc.next(); if (YON.equalsIgnoreCase("N")) repeatFlag=false; }
Accumulator Find the sum of all numbers between 1 and N. Scanner sc=new Scanner(System.in); System.out.println("enter an integer:"); int N = sc.nextInt(); int Sum=0; while (N>0) { Sum=Sum+N; --N; } System.out.println("The sum of all numbers is: " + Sum);
Exit Loop with the break statement Scanner sc=new Scanner(System.in); while (true){ System.out.println("enter loan:"); double loan=sc.nextDouble(); System.out.println("enter rate in %:"); double rate=sc.nextDouble(); System.out.println("enter term in year:"); double term=sc.nextDouble(); double monPayment=(loan*rate/100/12)/(1-(Math.pow(1+rate/100/12,-12*term))); System.out.println("Monthly payment is: =" + monPayment); System.out.println("Do you want to continue? (Y/N):"); String YON=sc.next(); if (YON.equalsIgnoreCase("N")) break; } System.out.println("Thank you for using payment calculator!");
While Loop ExampleN Factorial, N! Scanner sc=new Scanner(System.in); System.out.println("enter an integer:"); int N = sc.nextInt(); int factorial=1; while (N>0){ factorial=factorial*N; N=N-1; } System.out.println("factorial= " + factorial);
Compute N! or Stop when the product greater than 100(Determine if it is a normal exit or earlier exit) Scanner sc=new Scanner(System.in); System.out.println("enter an integer:"); int N = sc.nextInt(); boolean exitEarlier = false; int factorial=1; int Count = 1; while (Count<=N){ factorial=factorial*Count; if (factorial >= 100){ exitEarlier = true; break; } Count++; } if (exitEarlier==false) System.out.println("factorial= " + factorial); else System.out.println("Reach 100 when N = " + Count);
Use continue statement to jump to the beginning of a loop Find the sum of all even numbers between 1 and N but ignore numbers ending with 6 . Scanner sc=new Scanner(System.in); System.out.println("enter an integer:"); int N = sc.nextInt(); int Sum=0; while (N>0) { if (N % 10 == 6){ --N; continue; } if (N % 2==0) Sum=Sum+N; --N; } System.out.println("The sum of all even numbers ignoreing 6 is: " + Sum);
Accumulator Find the sum of all numbers between 1 and N using do loop. Scanner sc=new Scanner(System.in); System.out.println("enter an integer:"); int N = sc.nextInt(); int Sum=0; do { Sum=Sum+N; N=N-1; //Sum+=N; //--N; } while (N>0); System.out.println("The sum of all numbers is: " + Sum);
Do while loop example String flag; do { System.out.println("enter loan:"); double loan=sc.nextDouble(); System.out.println("enter rate in %:"); double rate=sc.nextDouble(); System.out.println("enter term in year:"); double term=sc.nextDouble(); double monPayment=(loan*rate/100/12)/(1-(Math.pow(1+rate/100/12,-12*term))); System.out.println("Monthly payment is: =" + monPayment); System.out.println("Do you want to continue? (Y/N):"); flag=sc.next(); } while (flag.equalsIgnoreCase("Y"));
Sum of 1 to N Scanner sc=new Scanner(System.in); System.out.println("enter an integer:"); int N = sc.nextInt(); int Sum=0; int i=0; for (i=0; i<=N;++i){ Sum=Sum + i; } System.out.println("The sum is: " + Sum);
Increment smaller than 1 Scanner sc=new Scanner(System.in); System.out.println("enter an integer:"); int N = sc.nextInt(); double Sum=0; double i; for (i=0; i<=N; i += 0.5){ Sum=Sum + i; System.out.println("loop i is " + i); } System.out.println("The sum is: " + Sum); Note: What is wrong if I and Sum are declared as integer? Infinite loop
For loop with break: Compute N! or Stop when the product greater than 100 (Determine if it is a normal exit or earlier exit) Scanner sc=new Scanner(System.in); System.out.println("enter an integer:"); int N = sc.nextInt(); boolean exitEarlier = false; int factorial=1; int Count = 1; for (Count=1;Count<=N;++Count){ factorial=factorial*Count; if (factorial >= 100){ exitEarlier = true; break; } } if (exitEarlier==false) System.out.println("factorial= " + factorial); else System.out.println("Reach 100 when N = " + Count);
For loop with the continue statement Find the sum of all even numbers between 1 and N but ignore numbers ending with 6 . Scanner sc=new Scanner(System.in); System.out.println("enter an integer:"); int N = sc.nextInt(); int Sum=0, Count; for (Count=N;Count>0;--Count) { if (Count % 10 == 6) continue; if (Count % 2==0) Sum=Sum+Count; } System.out.println("The sum of all even numbers ignoreing 6 is: " + Sum);
Formatting Numeric Print OutputSystem.out.format • System.out.format(String format, variables separated by commas); • Java Tutorials/Numbers and Strings/ Numbers/Formatting Numeric Print Output • int i = 461012; • System.out.format("The value of i is: %d%n", i); The output is: The value of i is: 461012
Printing more than one variables Control decimal places: $%.2f Print with $ and start a new line: $%.2f%n System.out.format(" %d $%.2f%n", Year,FV);
DecimalFormat Class • Use DecimalFormat class to define a format: • Format: “###,###.###” • 123,456.789 • Format: “###.##” • 123456.79 • Format: “000000.000” • 000123.780 • Format: “$###,###.###” • $12,345.67
DecimalFormat Example DecimalFormat myFormatter = new DecimalFormat("00"); Int i=2, j=3, product; product=i*j; String output = myFormatter.format(product);
Future Value=Present Value*(1+Rate)Year Given PV and Rate, compute the FV starting from year 5 until specified year with a step of 5 Scanner sc=new Scanner(System.in); System.out.println("enter present value:"); double PV = sc.nextDouble(); System.out.println("enter rate:"); double Rate = sc.nextDouble(); System.out.println("enter ending year:"); double endYear = sc.nextDouble(); double FV; int Year; for (Year=5;Year<=endYear;Year+=5) { FV=PV*Math.pow(1+Rate,Year); //System.out.format("year = %d, FV = %f%n", Year,FV); System.out.format(" %d $%.3f%n", Year,FV); }
Nested Loop String line=""; int i,j,product; for (i=1;i<=3;++i){ for (j=1;j<=4;++j){ product=i*j; line=line+ "( " + i + ", " + j +") "; } System.out.println(line); line=""; } ( 1, 1) ( 1, 2) ( 1, 3) ( 1, 4) ( 2, 1) ( 2, 2) ( 2, 3) ( 2, 4) ( 3, 1) ( 3, 2) ( 3, 3) ( 3, 4) Note: Use a while loop to produce the same output.
Nested LoopMultiplication Table 1 2 3 4 5 6 7 8 9 1 01 02 03 04 05 06 07 08 09 2 02 04 06 08 10 12 14 16 18 3 03 06 09 12 15 18 21 24 27 4 04 08 12 16 20 24 28 32 36 5 05 10 15 20 25 30 35 40 45 6 06 12 18 24 30 36 42 48 54 7 07 14 21 28 35 42 49 56 63 8 08 16 24 32 40 48 56 64 72 9 09 18 27 36 45 54 63 72 81
First Try String line=""; int i,j,product; for (i=1;i<=9;++i){ for (j=1;j<=9;++j){ product=i*j; line=line+product + " "; } System.out.println(line); line=""; }
Second Try DecimalFormat myFormatter = new DecimalFormat("00"); String line=""; int i,j, product; System.out.println(" 1 2 3 4 5 6 7 8 9"); for (i=1;i<=9;++i){ line=line+ i + " "; for (j=1;j<=9;++j){ product=i*j; String output = myFormatter.format(product); line=line+output + " "; } System.out.println(line); line=""; }
Future Value=Present Value*(1+Rate)Year Given PV, compute the FV starting from year 5 until specified year with a step of 5 and rate from 3% to specified rate with a step of 0.5% Rate/Year 5 10 15 20 3.00% $1,159.27 $1,343.92 $1,557.97 $1,806.11 3.50% $1,187.69 $1,410.60 $1,675.35 $1,989.79 4.00% $1,216.65 $1,480.24 $1,800.94 $2,191.12 4.50% $1,246.18 $1,552.97 $1,935.28 $2,411.71 5.00% $1,276.28 $1,628.89 $2,078.93 $2,653.30
Code example Scanner sc=new Scanner(System.in); System.out.println("enter present value:"); double PV = sc.nextDouble(); System.out.println("enter ending rate:"); double endRate = sc.nextDouble(); System.out.println("enter ending year:"); double endYear = sc.nextDouble(); double FV, Rate; int Year; String line=""; DecimalFormat dollarFormatter = new DecimalFormat("$###,###.00"); DecimalFormat percentFormatter = new DecimalFormat("###.00%"); line="Rate/Year "; for (Year=5;Year<=endYear;Year+=5) line=line + Year + " "; System.out.println(line); line=""; for (Rate=0.03;Rate<=endRate;Rate+=0.005){ line=line+ percentFormatter.format(Rate)+ " "; for (Year=5;Year<=endYear;Year+=5) { FV=PV*Math.pow(1+Rate,Year); line=line+ dollarFormatter.format(FV) + " "; } System.out.println(line); line=""; }
Exercise Create a monthly payment table for loans with term specified by user and rate range from 5% to any specified rate with an increment of .25% and loan from 100,000 to any specified loan with an increment of 50000. Monthly payment table term 30 Loan rate 100,000 150,000 200,000 250,000 5% $536.82 $805.23 $1,073.64 $1,342.05 5.25% $552.20 $828.31 $1,104.41 $1,380.51 5.50% $567.79 $851.68 $1,135.58 $1,419.47 5.75% $583.57 $875.36 $1,167.15 $1,458.93 6% $599.55 $899.33 $1,199.10 $1,498.88
Monthly Payment Method public static void main(String[] args) { Scanner sc=new Scanner(System.in); String flag="Y"; while (flag.equalsIgnoreCase("Y")){ System.out.println("enter loan:"); double loan=sc.nextDouble(); System.out.println("enter rate in %:"); double rate=sc.nextDouble(); System.out.println("enter term in year:"); double term=sc.nextDouble(); double monPayment=MPayment(loan,rate,term); System.out.println("Monthly payment is: =" + monPayment); System.out.println("Do you want to continue? (Y/N):"); flag=sc.next(); } System.out.println("Thank you for using payment calculator!"); } public static double MPayment(double loan,double rate,double term){ double monPayment=(loan*rate/100/12)/(1-(Math.pow(1+rate/100/12,-12*term))); return monPayment; }