Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science Instructions that Repeat –iterate instruction –while instruction A Large Program written using Top-Down Design Unit 4.

Similar presentations


Presentation on theme: "Introduction to Computer Science Instructions that Repeat –iterate instruction –while instruction A Large Program written using Top-Down Design Unit 4."— Presentation transcript:

1 Introduction to Computer Science Instructions that Repeat –iterate instruction –while instruction A Large Program written using Top-Down Design Unit 4

2 4- 2 Last time, on “Intro to CS”... More examples of extending a class, and adding methods

3 4- 3 Multiple Objects Objects Sending Messages to Other Objects

4 4- 4 Decomposition Good Object Oriented Programming (OOP) means seeing appropriate decomposition

5 4- 5 Getting Responses from Objects Sending messages to objects, getting responses Using those responses as parts of other messages

6 4- 6 Getting Responses from Objects Sending messages to objects, getting responses Using those responses as parts of other messages myworld.nextToABeeper(bill.xLoc, bill.yLoc)

7 4- 7 Using Those Responses to Control Execution if ( myworld.nextToABeeper(bill.xLoc, bill.yLoc) ) bill.pickBeeper;

8 4- 8 The iterate Instruction Provides us with a shorthand notation that tells a robot to repeat an instruction a specified number of times. iterate times is the body of the iterate instruction. The whole thing we sometimes call an “iterate loop”.

9 4- 9 Another Way of Defining turnRight void turnRight { iterate 3 times turnLeft; }

10 4- 10 Another Way of Defining harvestAFurrow void harvestAFurrow { pickBeeper; iterate 4 times { move; pickBeeper; } More concise, more general, easier to change number of beepers per furrow.

11 4- 11 iterate nested within iterate void makeSquareOfLength6 { iterate 4 times { iterate 6 times move; turnLeft; } 12345 1 2 3 4 5 678 6 7 8

12 4- 12 iterate nested within iterate 12345 1 2 3 4 5 678 6 7 8 After 1 outer loop void makeSquareOfLength6 { iterate 4 times { iterate 6 times move; turnLeft; }

13 4- 13 iterate nested within iterate 12345 1 2 3 4 5 678 6 7 8 After 2 outer loops void makeSquareOfLength6 { iterate 4 times { iterate 6 times move; turnLeft; }

14 4- 14 iterate nested within iterate 12345 1 2 3 4 5 678 6 7 8 After 3 outer loops void makeSquareOfLength6 { iterate 4 times { iterate 6 times move; turnLeft; }

15 4- 15 iterate nested within iterate 12345 1 2 3 4 5 678 6 7 8 After 4 outer loops void makeSquareOfLength6 { iterate 4 times { iterate 6 times move; turnLeft; }

16 4- 16 How Do We Do This? Assume robot is facing East, and somewhere east of him is a beeper. Tell robot to go to the beeper and pick it up. 12345 1 2 3 4 5 678 6 7 8 if ( !myworld.nextToABeeper(...) ) move; if ( !myworld.nextToABeeper(...) ) move; if ( !myworld.nextToABeeper(...) ) move; pickBeeper; iterate ? times move; pickBeeper;......

17 4- 17 The while Instruction The while instruction combines the repetition ability of the iterate instruction with the testing ability of the if instruction. while while commands a robot to repeatedly execute an instruction as long as is true.

18 4- 18 How It Is Executed Robot first checks in the current situation If is true, robot executes, then re-executes the entire while loop If if false, robot is finished with the while instruction, and continues by executing instructions that follow the while loop

19 4- 19 Our Solution to Previous Problem void goToBeeper { while ( !myworld.nextToABeeper(xLoc, yLoc) ) move; } 12345 1 2 3 4 5 678 6 7 8

20 4- 20 Another Example void clearCornerOfBeepers { while ( myworld.nextToABeeper(xLoc, yLoc) ) pickBeeper; } 12345 1 2 3 4 5 678 6 7 8 What happens when robot is on a corner with 2 beepers? With zero beepers?

21 4- 21 Another while Loop void incorrectClearCornerOfBeepers { while ( myworld.nextToABeeper(xLoc, yLoc) ) turnLeft; } 12345 1 2 3 4 5 678 6 7 8 What happens when robot is on a corner with zero beepers? With 2 beepers?

22 4- 22 Another while Loop void incorrectClearCornerOfBeepers { while ( myworld.nextToABeeper(xLoc, yLoc) ) turnLeft; } 12345 1 2 3 4 5 678 6 7 8 What happens when robot is on a corner with zero beepers? With 2 beepers?

23 4- 23 Infinite WHILE Loops This is called getting a robot stuck in an infinite loop; it is a kind of intent error The ability to have a robot execute an instruction an unknown number of times has dangers as well as benefits The while instruction is the only one that can get a robot stuck in an infinite loop Check your while instructions carefully to make sure they terminate

24 4- 24 Formal Property of WHILE When (if) a while instruction finishes executing, is known to be false If you need to make some true, you write something like: while For example, to make true, write a while loop with replaced by

25 4- 25 How to Make Sure while instructions terminate? Each execution of should bring the robot closer to finishing the loop In incorrectClearCornerOfBeepers, the repeated turnLeft did not make the robot progress to having picked up all the beepers void faceNorth { while ( !facingNorth ) turnLeft; }

26 4- 26 Verifying while Loops while loops must be capable of working correctly in an unbounded number of initial situations We can use an informal argument to check for probable correctness…

27 4- 27 Verifying while Loops Show the instruction works correctly when is false Show that each time the loop body is executed, the robot’s new situation is a simpler (less work) and similar (not very different) version of the old situation The while executes correctly and eventually terminates, since each execution of the loop brings the robot closer to having become false

28 4- 28 For example… void clearCornerOfBeepers { while ( myworld.nextToABeeper(xLoc, yLoc) ) pickBeeper; } If there are no beepers on the corner, the while performs correctly by terminating right away. If there are beepers on the corner, there will be one less beeper after the instruction is executed: Simpler (one less beeper) and Similar (robot on same corner)

29 4- 29 Verifying versus Testing Often, instructions are tested in many different initial situations These may give us some confidence, but “Testing only shows the presence of bugs, not their absence” It helps to carefully simulate the execution of the while loop during its first few and last few repetitions (check out some in the middle, too)

30 4- 30 When is Checked void harvestLine { while (myworld.nextToABeeper(xLoc, yLoc) ) { pickBeeper; move; } 12345 1 2 3 4 5 678 6 7 8 Robot picks up row of beepers then stops.

31 4- 31 When is Checked Robot checks only before he executes the body of the loop He is totally insensitive to while he is executing instructions inside the loop body That is, once the robot starts executing the loop body, he does not recheck until he has completely executed the loop body, and started to re-execute the entire while instruction

32 4- 32 NOT THIS WAY! Some beginning programmers wrongly think the robot checks after each instruction inside the loop body NO!!! What would happen in the harvestLine instruction then? void harvestLine { while ( myworld.nextToABeeper(xLoc, yLoc) ) { pickBeeper; move; } 

33 4- 33 Repeating Instructions and Block Structure 12345678910 1 2 3 4 void harvestToWall { pickBeeper; while ( myworld.frontIsClear(xLoc, yLoc, facing) ) { move; pickBeeper; }

34 4- 34 Repeating Instructions and Block Structure 12345678910 1 2 3 4 void harvestToWall { pickBeeper; while ( myworld.frontIsClear(xLoc, yLoc, facing) ) { move; pickBeeper; }

35 4- 35 Repeating Instructions and Block Structure 12345678910 1 2 3 4 void harvestToWall { pickBeeper; while ( myworld.frontIsClear(xLoc, yLoc, facing) ) { move; pickBeeper; }

36 4- 36 Repeating Instructions and Block Structure 12345678910 1 2 3 4 void harvestToWall { pickBeeper; while ( myworld.frontIsClear(xLoc, yLoc, facing) ) { move; pickBeeper; }

37 4- 37 Repeating Instructions and Block Structure 12345678910 1 2 3 4 void harvestToWall { pickBeeper; while ( myworld.frontIsClear(xLoc, yLoc, facing) ) { move; pickBeeper; }

38 4- 38 Repeating Instructions and Block Structure 12345678910 1 2 3 4 Why is first pickBeeper there? What if it were not? How many move s and how many pickBeeper s? Could pickBeeper be at the end? void harvestToWall { pickBeeper; while ( myworld.frontIsClear(xLoc, yLoc, facing) ) { move; pickBeeper; }

39 4- 39 If { and } were forgotten… After harvestToWall After incorrectHarvestToWall Intent Error void incorrectHarvestToWall { pickBeeper; while ( myworld.frontIsClear(xLoc, yLoc, facing) ) move; pickBeeper; }

40 4- 40 Don’t Forget { and }! In what initial situation does this cause an error shutoff (and harvestToWall doesn’t)? In what initial situation does this instruction work correctly? Can cause intent error, error shutoff, or work! Don’t forget { and } when necessary: sometimes causes syntactic error, or worse… void incorrectHarvestToWall { pickBeeper; while ( myworld.frontIsClear(xLoc, yLoc, facing) ) move; pickBeeper; }

41 4- 41 if instructions in while loops One Initial Situation (zero or one beeper per corner) The Final Situation 12345678910 1 2 3 4 123456789 1 2 3 4 void sparseHarvestToWall { if ( myworld.nextToABeeper(xLoc, yLoc) ) pickBeeper; while ( myworld.frontIsClear(xLoc, yLoc, facing) ) { move; if (myworld.nextToABeeper(xLoc, yLoc) ) pickBeeper; } Could replace if with instruction pickbeeper-if- present, making it more readable

42 4- 42 Can Make Even More General void manyBeeperSparseHarvestToWall { clearCornerOfBeepers; while ( myworld.frontIsClear(xLoc, yLoc, facing) ) { move; clearCornerOfBeepers; } Try to solve complex problems by looking for similar but simpler problems (to develop structure of solution) Strive to re-use your previous work

43 4- 43 Syntax Error Programming errors can be hard to spot, but once found are painfully obvious. “There’s always a reason…” if ( myworld.nextToABeeper(xLoc, yLoc) ) then pickBeeper;

44 4- 44 A Large Program Written using Top-Down Design We want to write a program that has a robot escape from any rectangular room that has an open doorway exactly one block wide. After escaping, the robot should turn himself off.

45 4- 45 The General Plan of Attack 12345 1 2 3 4 5 678 6 7 8 912345 1 2 3 4 5 678 6 7 8 9 One Initial SituationFinal Situation and robot’s Path

46 4- 46 High-Level Program main { xxx.goToWall; xxx.turnLeft; xxx.followUntilDoorIsOnRight; xxx.exitDoor; xxx.turnOff; } New Methods: goToWall followUntilDoorIsOnRight exitDoor

47 4- 47 goToWall void goToWall { while (myworld.frontIsClear(xLoc, yLoc, facing)) move; } How are we doing so far?

48 4- 48 Beyond-the-horizon Situation 12345 1 2 3 4 5 678 6 7 8 9 One initial situation that causes robot to enter an infinite loop 24 possible corners; 4 directions: 96 different initial situations here. Only 6 of them cause goToWall to fail, i.e., only 7% are “beyond-the-horizon” situations. Random testing is unlikely to be effective.

49 4- 49 Have Robot “Do the Shuffle” Check for wall in front; if clear, check for wall in front of corner on the right; if clear, go back to original corner and move forward

50 4- 50 Have Robot “Do the Shuffle” 12345 1 2 3 4 5 678 6 7 8 9

51 4- 51 Have Robot “Do the Shuffle” 12345 1 2 3 4 5 678 6 7 8 9

52 4- 52 Have Robot “Do the Shuffle” 12345 1 2 3 4 5 678 6 7 8 9

53 4- 53 Have Robot “Do the Shuffle” 12345 1 2 3 4 5 678 6 7 8 9

54 4- 54 Have Robot “Do the Shuffle” 12345 1 2 3 4 5 678 6 7 8 9

55 4- 55 Have Robot “Do the Shuffle” 12345 1 2 3 4 5 678 6 7 8 9

56 4- 56 Have Robot “Do the Shuffle” 12345 1 2 3 4 5 678 6 7 8 9

57 4- 57 Have Robot “Do the Shuffle” Check for wall in front; if clear, check for wall in front of corner on the right; if clear, go back to original corner and move forward

58 4- 58 goToWall (number 2) void goToWall { while ( myworld.frontIsClear(xLoc, yLoc, facing) ) shuffle; } void shuffle { sidestepRight; if ( myworld.frontIsClear(xLoc, yLoc, facing) ) { sidestepBackLeft; move; }

59 4- 59 sidestepping instructions void sidestepRight { turnRight; move; turnLeft; } void sidestepBackLeft { turnLeft; move; turnRight; } Doing OK?

60 4- 60 Still not right! 12345 1 2 3 4 5 678 6 7 8 9 Robot’s right is blocked by wall; he can’t shuffle What’s the solution?

61 4- 61 goToWall (number 3) If Robot’s right is blocked, have him turn right void goToWall { if ( myworld.rightIsBlocked(xLoc, yLoc, facing) ) turnRight; else while ( myworld.frontIsClear(xLoc, yLoc, facing) ) shuffle; }

62 4- 62 OK, so now what? 12345 1 2 3 4 5 678 6 7 8 9 Second shuffle causes Robot to perform error-shutoff We thought his right wouldn’t become blocked What’s the solution?

63 4- 63 goToWall (number 4) Have Robot always check his right before executing shuffle void goToWall { while ( myworld.frontIsClear(xLoc, yLoc, facing) ) if (myworld.rightIsBlocked(xLoc, yLoc, facing) ) turnRight; else shuffle; }

64 4- 64 followUntilDoorIsOnRight We execute this after going to a wall, then executing a turnLeft We can thus assume that Robot’s right is blocked by a wall when the instruction starts The instruction must stop when Robot senses a door (his right becomes clear) While the door is not found, the instruction must make him follow the room’s perimeter

65 4- 65 An invariant An invariant is a condition that must be true during the execution of an instruction The invariant for following the perimeter is maintained by having the Robot move forward until reaching a corner, then turn to the left, and continue

66 4- 66 Robot, find the door! void followUntilDoorIsOnRight { while ( !myworld.rightIsClear(xLoc, yLoc, facing) ) followPerimeter; } void followPerimeter { if ( myworld.frontIsClear(xLoc, yLoc, facing) ) move; else turnLeft; } When this is finished, Robot’s right hand side will be clear.

67 4- 67 exitDoor We can assume that this is executed only when Robot’s right side is clear void exitDoor { turnRight; move; }

68 4- 68 The Complete Program class Escaper extends BasicRobot { Definition of turnRight Definition of sidestepRight Definition of sidestepBackLeft Definition of shuffle Definition of goToWall Definition of followPerimeter Definition of followUntilDoorIsOnRight Definition of exitDoor } main { Escaper bill = new Escaper(...); bill.goToWall; bill.turnLeft; bill.followUntilDoorIsOnRight; bill.exitDoor; bill.turnOff; }

69 4- 69 Another strange situation… 12345 1 2 3 4 5 678 6 7 8 9 What is Robot’s behavior in this situation?

70 4- 70 Finally this: 12345 1 2 3 4 5 678 6 7 8 9 What does Robot do here? We want him to turn himself off at corner of 8th street and 6th avenue What instruction(s) should be changed to remove the slight flaw? StreetStreet Avenues

71 4- 71 Three things to remember Don’t think you’ve got to get the program right the first time The most important thing is to write something down on paper. You can make progress then by testing, spotting errors, revising If you revise to the point of losing the original thread of solution, maybe you should rewrite the entire program. You’ll be able to use all the knowledge you’ve gained.


Download ppt "Introduction to Computer Science Instructions that Repeat –iterate instruction –while instruction A Large Program written using Top-Down Design Unit 4."

Similar presentations


Ads by Google