Download presentation
Presentation is loading. Please wait.
Published byEric Pierce Modified over 9 years ago
1
Recognizing Patterns Counting: continually updating a value by a fixed amount Counting raindrops int dropCount = 0; //Raindrop counter while (dropCount < MAX) { new Raindrop( … ); dropCount++; }
2
Counting Bricks while ( count < TOTAL ) { new Brick(…); count++; }
3
The Counting while Loop int i = initialValue;// initialize while (i < stopVal) { // test... // do stuff i++; // increment }
4
The for loop Especially useful for counting Ex: for ( int i=initialVal; //initialize i<stopVal; //test i++;) {//increment …//do stuff }
5
Counting Raindrops with for Loop for (int dropCount = 0; dropCount <MAX; dropCount++) { new Raindrop ( … ); }
6
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
7
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
8
Counting Backwards with for Loop Ex: Printing a countdown for (int count = 10; count >= 1; count--) { System.out.println(count); }
9
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); }
10
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
11
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
12
Trace the following statements summkk< 5?
13
Trace the following statements Solution: summkk< 5? 0100T 121T 11142T 12163T 13184T 14205F done
14
Another trace example jj<=18?Console
15
Another trace example--SOLN jj<=18?Console 10T 13T10 13 16T10 13 16 19F10 13 16
16
and another ii<4pp%2 == 0Console
17
and another SOLN ii<4pp%2 == 0Console 10 1T17F 2T24T17 24 even 3T31F17 24 even 31 4F(done)
18
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
19
Nested Loops Any loop body can contain another loop Ex: for ( … ) { while (…) { for(…) { }
20
Tracing a nested loop ii<4kk>2Console
21
Tracing a nested loop -- SOLN ii<4kk>2Console 0T5T5 4T4 3T3 2F 1T5T6 4T5 etc..........
22
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 }
23
The do while Loop Syntax: do { } while ( ) Craps Example
24
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
25
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
26
Off by one errors Suppose we want to run a for loop 5 times: The left hand version will run it 6 times, not 5. for(int i=0;i<=5; i++){ } for(int i=0;i<5;i++) { }
27
Infinite Loops Ex: while ( count< TOTAL ) { new Brick (…); } Since value of count is not updated, the condition in while will stay true forever.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.