Iteration: Intro Two types of loops: 1. Pretest Condition precedes body Iterates 0+ times 2. Posttest Condition follows body Iterates 1+ times 1
Iteration: While Loops Pretest loop Most general loop construct Only loop construct needed Syntax: while (<boolean_expression>) <statement> Note the similarity to the if statement Semantics: Example: //Count from 1 to 10 int i = 1; while (i < 11){ System.out.println(i); i++; 2
Iteration: Potential Problems 1. Infinite loop Loop that never ends Can result due to many reasons: Failure to update LCV Failure to update LCV properly Poor choice of termination condition Failure to update process 2. Off-by-one error Count-controlled loop that iterates one too many or one too few times 3
Iteration: Loop-and-a-half Construct In some situations, makes sense to halt loop execution in middle of loop This referred to as loop-and-a-half control No specific loop construct that implements this Implemented via a break statement Syntax: break; Semantics: Control passes to statement following containing control statement Example: //Check that values are within range Scanner scnr = new Scanner(System.in); int next, count = 0; boolean badinput = false; while (count < 100) { if ((next < 50) (next > 75)) { badinput = true; break; count++; if (badinput) System.out.println("Invalid input at position " + count); else System.out.println("All input valid"); 4
Example without break: Iteration: Loop-and-a-half Construct //Check that values are within range Scanner scnr = new Scanner(System.in); int next, count = 0; boolean badinput = false; while ((count < 100) &&!badinput){ if ((next < 50) (next > 75)) { badinput = true; else count++; if (badinput) System.out.println("Invalid input at position " + count); else System.out.println("All input valid"); Problems: 1. Potential infinite loop 2. More difficult to debug as now have multiple exit points 5
Iteration: Do-while Loops Post test loop Syntax: do <statement> while (<Boolean_expression>); Semantics: Body must execute at least once Example: //Error check input Scanner scnr = new Scanner(System.in); int next; do { if ((next < 75) (next > 100)) { System.out.println(next + " is invalid"); System.out.println("Enter a value between 75 and 100"); while ((next < 75) (next > 100)); Example: Corresponding while loop with priming read //Error check input Scanner scnr = new Scanner(System.in); int next; while ((next < 75) (next > 100)) { System.out.println(next + " is invalid"); System.out.println("Enter a value between 75 and 100"); 6
Iteration: For Loops Pre test loop Works same as while Intended for count-controlled looping Loop control handled automatically in header Syntax: for ([<initializers>];[<continuation_condition>];[<updates>]) <statement> where < initializers > is a comma-separated list that initializes variables (including the LCV) < continuation condition > halts the iteration when false < updates > modify variables in < initializers > Semantics: v1 = expression1; //variables in <initializers> v2 = expression2;... while (<continuation_condition>) { statement v1 = update_expression1; v2 = update_expression2;... Usually only a single variable (LCV) appears in <initializers> and <updates> If <continuation condition> omitted, defaults to true 7
Example: Basic count control Iteration: For Loops (2) //Basic count control: Average n integers. int limit, sum; limit = 10; sum = 0; for (int counter = 0; counter < limit; counter++) { number = (int) ((Math.random() * 100) + 1); sum += number; System.out.println("The average = " + (float) sum / limit); Example: Basic count control //Basic count control: Counting backwards by 2 s int max, counter; max = (int) (Math.random() * 101); for (counter = max; counter >= 0; counter = counter - 2) System.out.println("Counter = " + counter); LCV declared in for header is local to loop body: int i = 10; for (int i = 0; i < 5; i++) System.out.println("i = " + i"); System.out.println("i = " + i"); vs int i = 10; for (i = 0; i < 5; i++) System.out.println("i = " + i"); System.out.println("i = " + i"); 8
Iteration: For Loops (3) continue statement Syntax: continue; Semantics: Control jumps to end of loop without exiting loop Example: // Display even integers for (int counter = 0; counter < 100; counter++) { if (counter % 2 == 1) continue; System.out.println(counter); 9