Recognizing Patterns • Counting: continually updating a value by a fixed amount • Counting raindrops int dropCount = 0; //Raindrop counter while (dropCount < MAX) { new Raindrop(…); dropCount++; } Counting Bricks while ( count < TOTAL ) { new Brick(…); count++; } The Counting while Loop int i = initialValue; while (i < stopVal) { ... i++; } // initialize // test // do stuff // increment The for loop • Especially useful for counting • Ex: for ( int i=initialVal; i<stopVal; i++;) { … } //initialize //test //increment //do stuff Counting Raindrops with for Loop for (int dropCount = 0; dropCount <MAX; dropCount++) { new Raindrop (…); } More General Start and End Points • Loops can take whatever starting point, end point, and increment Ex: for (int i=23; i <= 1728; i=i+591;){ //do stuff } • But one should avoid using a double for any of the three values General Syntax of for Loop • for (initialization; condition; update) { //Do something } Initialization: gen’ly creates a counting variable Condition: a boolean expression to stop the loop Updating: updates the variable created Counting Backwards with for Loop Ex: Printing a countdown for (int count = 10; count >= 1; count--) { System.out.println(count); } Update Values • Can increment loop index by any value • Ex: Drawing grass blades for (int pos = 0; pos < WIDTH; pos = pos + GAP) { new Line (pos, TOP, pos, GROUND, canvas); } Debugging Loops • You may find the following useful to fix your loops when they are not working right: – Memory table (trace) – Inserting System.out.println( ) statements – Using the BlueJ debugger Debugging using the Memory Table (Trace) method • The purpose of this exercise is to help reinforce how loops actually work • The computer follows a specific set of rules involving loops. Until you get an intuition for it, you may have to periodically trace a loop to understand how it works (or does not work) • The code examples are arbitrary but reflect what comes up often in loops Trace the following statements sum m k k< 5? Trace the following statements Solution: sum 0 10 11 12 13 14 m 10 12 14 16 18 20 k 0 1 2 3 4 5 k< 5? T T T T T F done Another trace example j j<=18? Console Another trace example--SOLN j 10 13 16 19 j<=18? T T T F Console 10 10 13 10 13 16 10 13 16 and another i i<4 p p%2 == 0 Console and another SOLN i i<4 p p%2 == 0 Console 10 1 T 17 F 17 2 T 24 T 17 24 even 3 T 31 F 17 24 even 31 4 F (done ) Lab 13 Problem 8 • • • • Draw the row of windows first The size of the windows is 30 (or so) Separated by 15 ... gives 450 total + edges Then draw the outline. Try to center row of windows in buiilding Nested Loops • Any loop body can contain another loop Ex: for ( … ) { while (…) { while (…) { for(…) { } } } } Tracing a nested loop i i<4 k k>2 Console Tracing a nested loop -- SOLN i 0 i<4 k T 5 4 3 2 1 T 5 4 etc. ..... .... k>2 T T T F T T .... Console 5 4 3 6 5 .... Lab 13 Problem 14 sinitialize xpos, ypos sset up outer loop to go 0 to 19 { s set up inner loop 0 to 3 { • draw a window • change xpos • } • change ypos • reset xpos } The do while Loop • Syntax: do { <code to repeat> } while (<condition>) Craps Example do while Loop vs while Loop • do while – Condition checked at the end – Loop body executed at least once • while – Condition checked at the beginning – Loop body may never execute Avoiding Loop Errors • Easier to find errors if you know where to look • Common loop errors include: • Off by 1 in counting loops • Infinite loops Off by one errors Suppose we want to run a for loop 5 times: for(int i=0;i<=5; i++){ for(int i=0;i<5;i++) { } } The left hand version will run it 6 times, not 5. Infinite Loops Ex: while ( count< TOTAL ) { new Brick (…); } Since value of count is not updated, the condition in while will stay true forever.