Download presentation
Presentation is loading. Please wait.
Published byMatthew Wilkinson Modified over 6 years ago
1
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field By defining new instructions we have been able to minimize repetitive instructions runMarathon();
2
Loops for known number of repetitions
There are times when you know how many times an instruction needs to be repeated… turnRight() is always 3 left turns moveMile() is always 8 moves() Method turnRight() with a loop: void turnRight() { for (int count = 0; count < 3; count++) { turnLeft(); }
3
FOR loop: General form for (int count = 0; count < <numberOfTimes>; count++) { <instruction-list>; } int count: count is the name of a variable that stores integers (whole numbers); it’s initial value is 0 count < <numberOfTimes> : as long as the current value of count is less than the number of loops we want, we keep looping; count will be equal to the number of loops we wanted when the loop is finished count++: we increment (add 1 to) the value of count <instructions>: Any valid robot instructions, including other loops
4
FOR loop: Other examples
void moveMile() { for (int count = 0; count < 8; count++) { move(); } Harvester Karel = new Harvester(2, 2, East, 0); for (int count = 0; count < 3; count++) { Karel.pickTwoRows();
5
FOR loop: What does this do?
void whatDoIDo() { for (int count = 0; count < 4; count++) { for (int count2 = 0; count2 < 6; count2++) { move(); } turnLeft();
6
Loops for an unknown number of repetitions
Suppose we know we want a robot to repeat a series of instructions, but we do not know how many times… Task: There is a beeper somewhere on the same street in front of the robot, but we do not know exactly where… How does the robot find the beeper? FOR loop requires an upper limit (and there is no guarantee the beeper will fall within the range) IF statements are a one-time execute statement We need something to combine the testing ability of the IF statement with the iteration of a FOR loop
7
WHILE loop: General form
while ( <test> ) { <instruction-list>; } Reserved word while starts the instruction, parentheses enclose the <test> , and braces enclose the <instruction-list>; in the usual way <test>: The same conditions used in the IF instructions A robot starts the loop by checking the <test> in it’s current situation If TRUE, the robot executes the <instruction-list>; and rechecks the situation If FALSE, the robot executes instructions that follow the WHILE loop
8
WHILE loop: find the beeper
class BeeperFinder extends Robot { // usual constructor // assumes no intervening wall segments void findBeeper() { while (! nextToABeeper()) { move(); }
9
WHILE loop: Clear a corner
A robot is on a corner that may or may not have beepers on it. The robot is to make sure that the corner is clear of all beepers // fill in the blanks void clearCorner() { while ( _____________________) { _____________________; }
10
Build a WHILE loop: 4 step process
When a robot has to do something an unknown number of times (clear a corner of beepers) Step 1: Identify the one test that must be true when the robot is finished with the loop ! nextToABeeper() Step 2: Use the opposite form of the test nextToABeeper() Step 3: Within the WHILE, make progress toward completion of the WHILE pickBeeper() Step 4: Do whatever you need to do before or after the loop to complete solving the problem For this problem, nothing else needs to be done
11
Clear all beepers to a wall
A robot is somewhere in the world facing south. One beeper is on each corner between the robot’s current position and the southern boundary wall. There is no beeper on the corner the robot is currently standing. Write a new method, clearAllBeepersToTheWall(), to pick up all of the beepers Download BeeperSweeper Demo
12
Is the loop correct? We cannot test all possible solutions
We have to convince ourselves the method is correct Step 1: Show the method works correctly when the initial situation results in the test being false Step 2: Each time the loop body executes, the robot’s new situation is simpler and similar to the original situation
13
Errors to avoid with WHILE loops
The fence post problem In order to have five sections of fence, how many fence posts do we need? We often encounter the fence post problem when working with WHILE loops Re-examine BeeperSweeper, in which the robot has a beeper on the corner he starts from
14
Errors to avoid with WHILE loops
Infinite Execution Look at the following loop while ( facingNorth()) { pickBeeper(); move(); } Think of step 3 in building loops (Within the WHILE, make progress toward completion of the WHILE). Is this happening here? What will happen to the robot is he is not facing north? If he is facing north?
15
Errors to avoid with WHILE loops
When is the test of a WHILE loop checked? while (nextToABeeper()) { pickBeeper(); move(); } If there is more than one beeper on a corner, will the robot pick them all before moving on? The robot checks the <test> only before it executes the <instruction-list>, not after each instruction in the list The body of a while is like a fence section, and the test is the fence post. The number of test evaluations is always one more then the number of executions of the body of the WHILE instruction
16
Nested WHILE loops Look again at BeeperSweeper
Instead of just one beeper per corner, there can be any number (possibly 0, but less than infinite) beepers on a corner
17
WHILE and IF instructions
What will happen with this code fragment? if (facingSouth()) { while (! facingSouth()) { turnLeft(); } Conflicting tests
18
WHILE and IF instructions
What will happen with this code fragment? while (! frontIsClear() ) { if (frontIsClear()) { move(); } else { turnLeft(); } Unnecessary test
19
WHILE and IF instructions
What will happen with this code fragment? while (nextToABeeper() ) { if (nextToABeeper()) { pickBeeper(); } Redundant test
20
When to use repeating instructions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.