Loops ISYS 350. Three Types of Loops while loop do while loop for loop.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Murach’s Java SE 6, C6© 2007, Mike Murach & Associates, Inc.Slide 1.
Murach’s Java SE 6, C5© 2007, Mike Murach & Associates, Inc.Slide 1.
Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Handle Exceptions. Murach’s Java SE 6, C5© 2007, Mike Murach & Associates, Inc. Slide 2 What is an Exception?
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Work with Data and Decision Structure. Slide 2 Note: String and Date are classes.
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Chapter 5 Loops.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 4: Control Structures II
Loops ISYS 350. Compute the sum of a list of numbers: Example: 5, 2, 10, 8, 4 Process: Sum= 0 Get a number from the list Sum = Sum + the number Repeat.
Chapter 5: Control Structures II
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Java Review if Online Time For loop Quiz on Thursday.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Loop Continue ISYS 350. Exiting a Loop Prematurely In some cases it is necessary to end a loop before the test condition would end it Use the break statement:
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Java Fundamentals 4. Java Programming: From Problem Analysis to Program Design, Second Edition2 Parsing Numeric Strings  Integer, Float, and Double are.
Loops ISYS 350. Write a Program that asks user to enter any numbers and displays the largest number Process: Largest = the first number Get the next number.
CSE 110 Midterm Review Hans and Carmine. If Statements If statements use decision logic In order to properly use if statements relational operators must.
Java Fundamentals 4.
CSC111 Quick Revision.
Chapter 6 More Conditionals and Loops
Chapter 5: Control Structures II
Repetition-Counter control Loop
Repetition-Sentinel,Flag Loop/Do_While
Repetition.
Chapter 5: Control Structures II
TK1114 Computer Programming
SELECTION STATEMENTS (1)
Decision statements. - They can use logic to arrive at desired results
CSS161: Fundamentals of Computing
Outline Altering flow of control Boolean expressions
The for-loop and Nested loops
An Introduction to Java – Part I, language basics
Control Statements Loops.
Loops ISYS 350.
Loop Continue.
Loops ISYS 350.
Control Statements Loops.
Repetition Statements
Building Java Programs
Building Java Programs
Random Numbers while loop
CSS161: Fundamentals of Computing
Chapter 4: Loops and Iteration
Presentation transcript:

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