karel the robot nested if statements while loops copyright © 2008 by helene g. kershner
Post on 19-Dec-2015
214 views
TRANSCRIPT
Karel The RobotKarel The Robot
Nested If StatementsNested If Statements
While LoopsWhile Loops
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
Five Block Square MazeFive Block Square Maze Problem statement; Problem statement; Karel Karel
needs to move through the needs to move through the five block square maze five block square maze and stop when he gets to and stop when he gets to the opening.the opening.
Define Output:Define Output: Karel Karel stops when he locates the stops when he locates the opening in the maze.opening in the maze.
Define Input.Define Input. Karel is at Karel is at the beginning of a 5-block the beginning of a 5-block square maze, facing east.square maze, facing east.
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and While Karel the Robot: Nested Ifs and While
Version 1Version 1:: If we didn’t know more If we didn’t know more
than the primitive than the primitive instructions we would do instructions we would do something like:something like:
Move;Move; Move;Move; Move;Move; Move;Move; Move;Move; Turnleft;Turnleft; Move;Move; Move;Move; Move;Move; Move;Move; Move;Move; Turnleft;Turnleft; ……
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and WhileVersion 2Version 2::
How do we get Karel to move around the maze with less of How do we get Karel to move around the maze with less of the programmer’s help? the programmer’s help?
What question would Karel need to ask to determine IF it What question would Karel need to ask to determine IF it were time to turn left?were time to turn left?
If front-is-clear then If front-is-clear then
beginbegin
move;move;
endend
elseelse
beginbegin
turnleft;turnleft;
end;end;Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
What happens when we enter that code?What happens when we enter that code? Karel moves exactly one block. Karel moves exactly one block.
Why?Why? Because, we only told Karel to move one Because, we only told Karel to move one
time or turnleft one time. time or turnleft one time.
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and WhileVersion 3Version 3:: How do we get Karel to move a bunch of times? How do we get Karel to move a bunch of times?
Iterate 40 timesIterate 40 times
begin begin
If front-is-clear then If front-is-clear then
beginbegin
move;move;
endend
elseelse
beginbegin
turnleft;turnleft;
end;end;
end;end;
Karel certainly moves around the maze when we combine the Karel certainly moves around the maze when we combine the iterate command with the IF statement. iterate command with the IF statement.
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and WhileAre we solving the problem we were asked to solve? Are we solving the problem we were asked to solve?
Problem statement; Problem statement; Karel needs to move through the five Karel needs to move through the five block square maze and stop when he gets to the block square maze and stop when he gets to the opening.opening.
Reading the problem statement carefully you’ll notice we Reading the problem statement carefully you’ll notice we are NOT solving the right problem.are NOT solving the right problem.
Why?Why?
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
Well, Well,
1. Karel stops when the iterate command finishes, 1. Karel stops when the iterate command finishes, wherever he happens to be. wherever he happens to be.
2. Karel does not stop when he reaches the 2. Karel does not stop when he reaches the opening as the problem statement required. opening as the problem statement required.
3. In fact according to this program, Karel has no 3. In fact according to this program, Karel has no idea whether he has reached the opening or not. idea whether he has reached the opening or not.
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
How does Karel know when the opening is How does Karel know when the opening is reached? reached?
How does Karel know when he has completed How does Karel know when he has completed his walk through the maze?his walk through the maze?
We, the programmer, could count the number of We, the programmer, could count the number of intersections, but that sort of misses the point. intersections, but that sort of misses the point.
What statement do we use to allow Karel to What statement do we use to allow Karel to make a decision? make a decision?
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
We need to create a decision (IF) that asks We need to create a decision (IF) that asks essentially “Am I done?”essentially “Am I done?” How can Karel know when to stop? How can Karel know when to stop? How does Karel know Karel is at the opening?How does Karel know Karel is at the opening?
Remember Karel can test any of the following conditionsRemember Karel can test any of the following conditionsfront-is-clearfront-is-clear front-is-blockedfront-is-blocked
left-is-clearleft-is-clear left-is-blockedleft-is-blocked
right-is-clearright-is-clear right-is-blockedright-is-blocked
next-to-a-beepernext-to-a-beeper not-next-to-a-beepernot-next-to-a-beeper
any-beepers-in-beeper-bagany-beepers-in-beeper-bag no-beepers-in-beeper-no-beepers-in-beeper-bagbag
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
We are looking for a test thatWe are looking for a test thatrecognizes that when the recognizes that when the
openingopening is to Karel’s is to Karel’s RightRight, , then he is done.then he is done.
Remember our choices:Remember our choices:front-is-clearfront-is-clear
front-is-blockedfront-is-blockedleft-is-clearleft-is-clear
left-is-blockedleft-is-blockedright-is-clearright-is-clear
right-is-blockedright-is-blockednext-to-a-beepernext-to-a-beeper
not-next-to-a-beepernot-next-to-a-beeperany-beepers-in-beeper-bagany-beepers-in-beeper-bag
no-beepers-no-beepers-in-beeper-bagin-beeper-bag
Copyright © 2008 by Helene G. Kershner
Opening
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
When the right-is-clear Karel is done.When the right-is-clear Karel is done.
We could write the following:We could write the following: If right-is-blocked thenIf right-is-blocked then
BeginBegin
-- Move through the maze-- Move through the maze
End;End;
But,But, How do we move through the maze? How do we move through the maze?
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
Earlier we solved moving through the maze using the Earlier we solved moving through the maze using the following IF statement:following IF statement:
This is how we move throught the maze, one intersection at This is how we move throught the maze, one intersection at a time. a time.
If front-is-clear then If front-is-clear then beginbegin move;move;endend
elseelsebeginbegin turnleft;turnleft;end;end;
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
For Karel to travel through the maze and do all the For Karel to travel through the maze and do all the checking himself, the following questions need to be checking himself, the following questions need to be asked ? asked ? Am I done yet?Am I done yet? Do I move forward?Do I move forward? Do I turn left?Do I turn left?
Karel’s code would need to look like this:Karel’s code would need to look like this:
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
If right-is-blocked thenIf right-is-blocked thenbeginbegin If front-is-clear then If front-is-clear then
beginbeginmove;move;
endend elseelse beginbegin
turnleft;turnleft; end;end;
end;end;
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
VersionVersion 4 4: Using nested Ifs, : Using nested Ifs, how might we solve our how might we solve our original problem? original problem?
Using Iterate, we still need Using Iterate, we still need to “guess” in advance to “guess” in advance how many intersections how many intersections Karel needs to go Karel needs to go through.through.
Iterate 40 timesIterate 40 timesbeginbegin If right-is-blocked thenIf right-is-blocked then beginbegin If front-is-clear then If front-is-clear then
beginbegin move;move;
endend elseelse
beginbegin turnleft;turnleft;
end;end; end;end;end;end;
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
What happens when Karel reaches the opening in the What happens when Karel reaches the opening in the Maze?Maze?
Karel knows not to turnKarel knows not to turn Karel knows not to move aheadKarel knows not to move ahead
BUT, Karel keeps repeating his tests because BUT, Karel keeps repeating his tests because the ITERATE statement tells him to keep the ITERATE statement tells him to keep testing. testing.
So, while Karel has correctly solved the required problem So, while Karel has correctly solved the required problem he has not done so efficiently. he has not done so efficiently.
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
The While Instruction or While LoopThe While Instruction or While Loop With ITERATE, the programmer needs to determine in With ITERATE, the programmer needs to determine in
advance of running the program how many time Karel advance of running the program how many time Karel will perform an activitywill perform an activity
Karel uses the WHILE instruction whenever a task is to Karel uses the WHILE instruction whenever a task is to be repeated an undetermined number of times.be repeated an undetermined number of times.
The WHILE instruction essentially combines an IF with The WHILE instruction essentially combines an IF with an Iterate. This allows Karel to determine, without an Iterate. This allows Karel to determine, without human intervention, when the required task has been human intervention, when the required task has been accomplished. accomplished.
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
The While Instruction or While LoopThe While Instruction or While Loopwhile <while <testtest> DO> DO
beginbegin
<<instruction(s)instruction(s)>;>;
endend
Ex: Ex: while front-is-clear DO while front-is-clear DO
beginbegin
putbeeper;putbeeper;
move;move;
end;end;
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
Our maze problem is perfectly suited to using While. Our maze problem is perfectly suited to using While. Remember, a while loop essentially combines a decision Remember, a while loop essentially combines a decision
(IF) with a repeat (Iterate) statement. (IF) with a repeat (Iterate) statement. Karel is already testing for the ending condition (right-is-Karel is already testing for the ending condition (right-is-
blocked) and doing so iteratively or repeatively. blocked) and doing so iteratively or repeatively. We can replace both the Iterate statements and the IF We can replace both the Iterate statements and the IF
test with a while statement. test with a while statement. A while statement is often called a While Loop because A while statement is often called a While Loop because
like the Iterate statement it repeats a set of instructions.like the Iterate statement it repeats a set of instructions.
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
Version 4Version 4::Iterate 40 timesIterate 40 timesbeginbegin If right-is-blocked thenIf right-is-blocked then beginbegin If front-is-clear then If front-is-clear then
beginbegin move;move;
endend elseelse
beginbegin turnleft;turnleft;
end;end; end;end;end;end;
Version 5Version 5::While right-is-blocked DOWhile right-is-blocked DO beginbegin If front-is-clear then If front-is-clear then
beginbegin move;move;
endend elseelse
beginbegin turnleft;turnleft;
end;end; end;end;
Be sure to match the Begin/End Be sure to match the Begin/End statementsstatements
Copyright © 2008 by Helene G. Kershner
Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While
NoteNote: Looking at the : Looking at the code, we can see that code, we can see that it will solve more than it will solve more than just our starting maze.just our starting maze.
This code will work on This code will work on any “left-handed” any “left-handed” maze. maze.
HINT:HINT: The basic The basic solution for this maze solution for this maze works for your final works for your final project! project!
Copyright © 2008 by Helene G. Kershner