Loops ISYS 350
Three Types of Loops while loop do while loop for loop
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 3
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 Example N 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);
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 12
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"));
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 15
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);
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 20
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 21
Formatting Numeric Print Output System.out.format System.out.format(String format, variables separated by commas); Java Tutorials/Numbers and Strings/ Numbers/Formatting Numeric Print Output – int i = ; – System.out.format("The value of i is: %d%n", i); The output is: The value of i is:
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, – Format: “###.##” – Format: “ ” – 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 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); } Given PV and Rate, compute the FV starting from year 5 until specified year with a step of 5
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 Loop Multiplication Table
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(" "); 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 % $1, $1, $1, $1, % $1, $1, $1, $1, % $1, $1, $1, $2, % $1, $1, $1, $2, % $1, $1, $2, $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 Monthly payment table term30 Loan rate100,000150,000200,000250,000 5%$ $ $1, $1, %$ $ $1, $1, %$ $ $1, $1, %$ $ $1, $1, %$ $ $1, $1, 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
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 34
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 35
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 36
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 37
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 38
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 39
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 40
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; }
Murach’s Java SE 6, C4© 2007, Mike Murach & Associates, Inc. Slide 42