Carefully observe
the way that the inner loop both begins and ends within the outer loop. Karel should only move one block forward within the outer WHILE
loop. The values that an int can hold are familiar to you as
values such as -85, and 42. We reasoned that we could use a WHILE loop to move
Karel forward and that was the mistake. Money, Money, Money.In the PPM Program, you receive a government payment of 95 percent of what it would cost the government to move you. It appears
that no matter how many beepers are initially on the corner, Karel will eventually
pick them all up when this instruction is executed. Since it is easier for a robot to turn left
(faster anyway) than to turn right, lets program the robot to walk around the
field in a counter-clockwise direction, turning left at each corner. 31. Winning membership numbers will be shown in your Casino Lobby. The last is executed after each of the
repeated executions of the body. That may take five minutes or it may take ten, but we need to keep stirring until the batter is smooth. What else do we know? If this third pile has an even
number of beepers, the safe room is the eastern room. How we choose to
remedy this situation is up to us, but at least in reasoning about the loop
we have become aware of a potential problem with our loop--the fencepost problem. To even save you more time we made sure that the calculations are automatically calculated as you type in the input boxes. This is very simple with the combination of the neighbors
enumeraton and a while loop. Karel remains focused on the inner loop and makes the inner test again. Our next planning
segment appears below. Since the robot is looking
for a beeper, we want it to be next to a beeper. Write a program which finds the factorial of a given number. Our three test situations
each put the beeper in a corner. As we explore the problem and learn more about the task,
we may discover situations that are beyond our original horizons--situations
that are legal, but special trouble-causing cases. What do we want to be true when the loop terminates? . As explained at the end of the last chapter, a decision map is a technique
that asks questions about the problem we are trying to solve. The second is the termination test. We must
be very careful when we plan the body of the WHILE loop to avoid the possibility
of infinite repetition. Since
all of our edge sensing is in followEdge, we look there for a solution. Our method will execute infinitely
in this case! We've already learned that we can repeat Karel actions with a for loop. Now Karel
rechecks the test and discovers that nextToABeeper is false, so the robot is
finished executing the WHILE loop. The first also seems to be ok until we simulate it in our beyond-the-horizon
situation, in which it will continue to execute forever. It should end in a colon rather than a semicolon b. Be as creative as you like. By "interesting" we mean which
item is important in terms of the problem being solved. If we have done a thorough
job of planning our implementation, the decision map should suggest an appropriate
instruction to use. 1. We may need other methods
to help us build these, but a first approximation to our class definition is
as follows. We could simply ask the
robot to put a beeper if it has any, but this requires that the robot begin
with no beepers in its beeper-bag. a. move b. move; c. move(); d. move() 2. Karel must
move forward one block within the loop body, but we must be careful here. An alternate way to solve this is to start as we did previously with a simple
while loop, but suppose the existence of an method moveTowardBeeper. Why not two or three? In this problem the inner loop is responsible for picking beepers. We can argue that if Karel keeps
picking one beeper, it must eventually pick all the beepers leaving none on the
corner. Question: What robot instruction can we use to keep Karel traveling
southward until it reaches the southern boundary wall? Another way we have made it to save you time is, if you are using a computer, you can simply move from one input to the next by pressing “Tab” on your keyboard and to move to the previous input press “Shift + Tab” together on your keyboard! Question: What instruction will allow Karel to pick all the beepers
that might be on a corner? In this case, since we don't know how long the world is, we can't put a fixed number in the loop. Also, we
sometimes need to execute an instruction or two before the WHILE loop to get
our robot into the correct position. Before using a
WHILE loop in a robot program, it would be helpful to have a framework for
thinking about the WHILE loop. Assignments / Labs 26 Karel Programming Exercises and Challenges in total Program-specific tasks for Karel the Dog Example Exercise: Pyramid of Karel Write a program to have Karel build a pyramid. Although we would like to prove that all other
situations are not too different from these sample situations, frequently the
best we can do is hope. Identify the one test that must be true when the robot is finished with
the loop. The discussion of that is beyond the scope of this book. If Karel executes this instruction correctly, then our plan is on the right
track. - First, Karel must walk an unknown distance to the southern wall. What happens now? Curly braces are needed to mark the beginning and end of a loop body only if … Step 4: Do whatever is required before or
after the WHILE is executed to ensure we solve the given problem. Consequently, there will be one beeper left when the loop finishes. We refer to
as the body of the FOR-LOOP instruction. . We continue the process below. What do we have to do to move toward the beeper? We appear to have
forgotten the fence post discussion of section 6.3.1. Of course if we have overridden pickBeeper or move then anything
is possible. Let's simulate Karel's execution of this instruction
on a corner containing two beepers. is equal to 3 x 2 x 1; 5! The robot must move one (and only one) block
forward during each iteration of the loop to ensure that each corner is examined. If we can
do this we can also convince ourselves that this loop does solve the problem. We also presented an informal way to reason about the correctness of WHILE
loops. The WHILE's test is the same as
the IF's test. Which of these statements are "interesting?" Since this is a slight modification of the clearAllBeepersToTheWall task of
the class BeeperController, it would be advantageous to have solutions to both
problems available. We can use our question/answer format to plan a solution to this problem. In this fragment there is an unnecessary test. Karel is required to make the inner test, frontIsClear, which is true and moves
one block forward away from the beeper. Program Karel to pick the beepers in the safe room. It makes
no state changes in the collection or in the enumeration itself. Take some time and examine some of the discussions presented
in this chapter and see how the complete decision map, which is presented below,
might have been useful. That would mean that the initial situation would look
like this: Second: We must show that each time the loop body is executed, Karel's new
situation is a simpler and similar version of the old situation.