Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science.

Similar presentations


Presentation on theme: "1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science."— Presentation transcript:

1 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com

2 2 while Loop Situation Team up with your neighbor for a couple minutes and discuss a situation that would necessitate that you use a while (indefinite) loop to solve the problem –Note: We might use one or two of these as examples later on when we are discussing while loops.

3 3 while Loops General form: while ( ) { } Loop continues until test is false

4 4 while Loops (cont.) Example: while (frontIsClear()) { move(); } Causes the robot to move continuously until there is a wall in front of it

5 5 while Loops (cont.) Flow of while loops: 1.If test is true, proceed to step 2; if test is false, go to step 4 2.Instructions in body of loop are executed 3.Go to step 1 4.Statements after loop are executed

6 6 Control structures IterationConditional Definite Loops MethodsSequential Iteration Graphic Organizer Indefinite Loops initialize variabletestincrementtest Types of loops Types of control structures Syntax Examples: for ( int i=1; i<=3; i++ ) { move(); putBeeper(); } while ( frontIsClear() ) { move(); } whilefor Java statement

7 7 Building a While Loop The following is a more systematic way of creating while loops. This will be extremely helpful to you at various times in the course and on the AP exam. Example: the robot moves until it comes to a beeper and then picks it up Step 1 – Identify what is true when the loop is finished (this is always easier than determining what is false while it is still executing) Step 2 – Use the opposite form of step 1’s result as the for the loop. You just need to negate the entire thing. ( note: Now and again you might find that “DeMorgan- izing” your result is helpful when answering AP-like questions.) Step 3 – make progress toward the goal (completion of the loop) within the loop nextToABeeper() while ( ! nextToABeeper() ) move();

8 8 Building a while Loop (cont’d) Step 4 – Do whatever is required before and/or after the loop to ensure that we solve the given problem Putting it all together using this 4-step approach: while ( ! nextToABeeper() ) { move(); } pickBeeper(); Another Example: Pick all beepers from a corner without knowing how many there are. pickBeeper();

9 9 while Loop Exercise Back on an earlier slide, you drew up a situation that would best be solved using a while loop - now write the code to solve your problem If you did not come up with a problem, use the following situation: –The robot should lay down a single beeper on each street corner in a straight line in the direction it is facing until it comes to a wall. In this case it should stop and turn off. –To test this, create a robot on street corner (4, 8) which is facing west, and has 10 beepers in its beeper bag.

10 10 DeMorgan’s Law Useful in Step 2 for negating the end situation We want to loop until a wall is in front AND a beeper is on the current corner ! frontIsClear() && nextToABeeper() Use DeMorgan’s Law to negate this. Negate the two predicates and replace && with || : while ( frontIsClear || !nextToABeeper() )

11 11 Let’s build a while loop Move a robot until it is not on a corner with any other robots and there is a wall on the left, then pick up the beeper that will be waiting there (the “hard” part is getting the condition correct without using a trial-and-error method – you don’t get to run any programs during our tests nor on the AP exam – also, you’ll code your labs faster if you have a way to ensure correct conditions EVERY time – without the crutch of running your code) You may use && and || for this exercise. Use the 4-step process now… while (???) { ??? }

12 12 You try the while condition 1)What is true when the loop is finished? !nextToARobot() && !leftIsClear() 2)Use the opposite form of step 1’s result as the for the loop while (nextToARobot() || leftIsClear()) 3)Make progress toward the goal within loop move(); 4)Do whatever is required before and/or after the loop to ensure that we solve the given problem pickBeeper();

13 13 You try the while condition You may use && and || for this Write a loop that moves a robot forward until it is either next to a robot, next to a beeper, or there is no wall on its right while ( ??? ) { ??? }

14 14 You try the while condition 1)What is true when the loop is finished? nextToARobot() || nextToABeeper() || rightIsClear() 2)Use the opposite form of step 1’s result as the for the loop while (!nextToARobot() && !nextToABeeper() && !rightIsClear()) 3)Make progress toward the goal within loop move(); 4)Do whatever is required before and/or after the loop to ensure that we solve the given problem Nothing required

15 15 Fence Post Problem This is a situation where we must perform some (but not all) of the statements in the loop either before or after the loop to solve the given problem. This is because the instructions in the loop do not entirely solve the problem for either the first time through the loop or the last time through the loop. This is called the Fence Post Problem because if we order 3 fence sections, we actually need 4 fence posts. What if we have five beepers in a row. The robot starts on the same corner as the first beeper. It needs to pick up all 5 beepers and it needs to end up at the corner of the last beeper. How would we go about doing this? for (int i=1; i<=4; i++) { pickBeeper(); move(); } pickBeeper();

16 16 Fence Post Problem pickBeeper() move()

17 17 Infinite Loops In a for or while loop, it is possible for the test to never be false When this happens, the loop continues infinitely Depending on the compiler, application, and other circumstances, an error may occur or the app may crash with a memory error


Download ppt "1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science."

Similar presentations


Ads by Google