1 / 14

Iteration

Iteration. Robert Stumpf, Professor Computer Information Systems California State Polytechnic University Pomona. Iteration. Bohm and Jacopini Iteration Forms while loop for loop do while loop. Bohm and Jacopini.

roza
Download Presentation

Iteration

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. Iteration Robert Stumpf, Professor Computer Information Systems California State Polytechnic University Pomona iteration

  2. Iteration • Bohm and Jacopini • Iteration • Forms • while loop • for loop • do while loop iteration

  3. Bohm and Jacopini • Bohm and Jacopini stated that a Structured program contains three constructs: 1. Sequence 2. Selection 3. Iteration • They proved this mathematically iteration

  4. Bohm and Jacopini • This time we are interested in the third construct • It is called iteration which means do again • Its basic form is the while loop • The other forms are convenient but can be simulated if not present iteration

  5. Iteration • Iteration means to repeat the process • A while loop has the test at the beginning Question resulting in a boolean response { Process to be repeated } • If question is true, repeat, if false exit loop iteration

  6. Iteration • A do while loop has the test at the end do{ Process to be repeated }Question resulting in a boolean response If question is true, repeat, if false exit loop iteration

  7. Forms • Basic While (without a block)int count = 0; while ( count < 3 ) System.out.println(count ++); • Not using a block is not very useful as only one statement is permitted iteration

  8. Forms • Basic While (with a block)int count = 0; while ( count < 3 ) { System.out.println(count); count++ } • Using a block is better • The question is asked at the beginning iteration

  9. Forms • Using ++ count ++;Is same ascount = count + 1; • count -- is also legal • Its precedence is at the first level along with ! and – (unary) iteration

  10. Forms • Basic For (without a block) for (int i=0; i < 3; i++ ) System.out.println(count); • The question still is asked at the beginning iteration

  11. Forms • Basic For (with a block)for (int i=0; i < 3; i++ ) { System.out.println(count); } • Using a block is better; why? iteration

  12. Forms • Do While char response;do { // code to input, process, and output is here response = JOptionPane.showInputDialog (“Enter N if done; Y if you wish to do it again” ); } while (response.charAt(0) = = ‘Y’); • Question is asked at the end iteration

  13. Summary • Java allows one to iterate three ways • while loop – test at the beginning • for loop – test at the beginning • do while loop– test at the end • The for loop is for convenience as there are many situations requiring one to count iteration

  14. Thank You • Java has Built in Constructs to facilitate Iteration • Any questions should be directed to Professor Robert Stumpf • Email: rvstumpf@csupomona.edu • Web Site:http://www.csupomona.edu/~rvstumpf iteration

More Related