140 likes | 332 Views
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.
E N D
Iteration Robert Stumpf, Professor Computer Information Systems California State Polytechnic University Pomona iteration
Iteration • Bohm and Jacopini • Iteration • Forms • while loop • for loop • do while loop iteration
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
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
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
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
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
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
Forms • Using ++ count ++;Is same ascount = count + 1; • count -- is also legal • Its precedence is at the first level along with ! and – (unary) iteration
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
Forms • Basic For (with a block)for (int i=0; i < 3; i++ ) { System.out.println(count); } • Using a block is better; why? iteration
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
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
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