karel the robot nested if statements while loops copyright © 2008 by helene g. kershner

24
Karel The Robot Karel The Robot Nested If Statements Nested If Statements While Loops While Loops Copyright © 2008 by Helene G. Kershner

Upload: cindy-degon

Post on 01-Apr-2015

222 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner

Karel The RobotKarel The Robot

Nested If StatementsNested If Statements

While LoopsWhile Loops

Copyright © 2008 by Helene G. Kershner

Page 2: Karel The Robot Nested If Statements While 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

Page 3: Karel The Robot Nested If Statements While Loops 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 only knew how to If we only knew how to

define-new-instructions define-new-instructions and had defined and had defined

define-new-instruction turnright as begin

turnleft;turnleft;turnleft;

end;

move;move;move;move;move;move;move;move;move;move;turnright;turnright;move;move;move;move;move;move;move;move;move;move;turnleft;turnleft;……

Copyright © 2008 by Helene G. Kershner

Page 4: Karel The Robot Nested If Statements While Loops 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? We could use an IF statement, and an ITERATE statement.We could use an IF statement, and an ITERATE statement. iterate 4 timesiterate 4 times beginbegin if front-is-blocked thenif front-is-blocked then beginbegin turnright;turnright; end;end; iterate 5 timesiterate 5 times beginbegin move;move; end;end; end;end;

Copyright © 2008 by Helene G. Kershner

Page 5: Karel The Robot Nested If Statements While Loops 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 around the maze without the How do we get Karel to move around the maze without the programmer needing to know anything about the maze programmer needing to know anything about the maze except that Karel is always making right turns? except that Karel is always making right turns?

The programmer doesn’t know if the maze is The programmer doesn’t know if the maze is 3-squares/side or 50-squares/side. It could even be a 3-squares/side or 50-squares/side. It could even be a rectange instead of a square. rectange instead of a square.

What if we don’t even know how many sides the maze has What if we don’t even know how many sides the maze has so we couldn’t use an iterate command? so we couldn’t use an iterate command? HINT! Project 2HINT! Project 2

REMEMBER: Karel CAN ask complex questions. REMEMBER: Karel CAN ask complex questions.

Copyright © 2008 by Helene G. Kershner

Page 6: Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While

Version 3Version 3::Karel can ask:Karel can ask:

If front-is-blocked then If front-is-blocked then

beginbegin

turnright;turnright;

endend

elseelse

beginbegin

move;move;

end;end;

Copyright © 2008 by Helene G. Kershner

Page 7: Karel The Robot Nested If Statements While Loops 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 turnright one time. time or turnright one time.

Copyright © 2008 by Helene G. Kershner

Page 8: Karel The Robot Nested If Statements While Loops 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 30 times

begin

if front-is-blocked then

begin

turnright;

end

else

begin

move;

end;

end;

Karel certainly moves around the maze when we combine the iterate Karel certainly moves around the maze when we combine the iterate command with the IF statement. command with the IF statement.

Copyright © 2008 by Helene G. Kershner

Page 9: Karel The Robot Nested If Statements While Loops 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

Page 10: Karel The Robot Nested If Statements While Loops 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

Page 11: Karel The Robot Nested If Statements While Loops 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

Page 12: Karel The Robot Nested If Statements While Loops 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

Page 13: Karel The Robot Nested If Statements While Loops 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 LeftLeft, , 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

Page 14: Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner

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 left-is-blocked thenIf left-is-blocked then

beginbegin

-- Move through the maze-- Move through the maze

end;end;

-- When left is clear DONE-- When left is clear DONE

But,But, How do we move through the maze? How do we move through the maze?

Copyright © 2008 by Helene G. Kershner

Page 15: Karel The Robot Nested If Statements While Loops 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-blocked then begin turnright; end else begin move; end;

Copyright © 2008 by Helene G. Kershner

Page 16: Karel The Robot Nested If Statements While Loops 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 right?Do I turn right?

Karel’s code would need to look like this:Karel’s code would need to look like this:

Copyright © 2008 by Helene G. Kershner

Page 17: Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While

IF left-is-blocked thenIF left-is-blocked then

beginbegin

if front-is-blocked thenif front-is-blocked then

beginbegin

turnright;turnright;

endend

elseelse

beginbegin

move;move;

end;end;

end;end;

Copyright © 2008 by Helene G. Kershner

These are called

NESTED IF statements.

One IF is nested inside another IF.

Page 18: Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While

VersionVersion 4 4: Using Nested If : Using Nested If statements, how might statements, how might we solve our original we solve our original problem? 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 30 timesIterate 30 timesbeginbegin If left-is-blocked thenIf left-is-blocked then beginbegin If front-is-blocked then If front-is-blocked then

beginbegin turnright;turnright;

endend elseelse

beginbegin move;move;

end;end; end;end;end;end;

Copyright © 2008 by Helene G. Kershner

NO Semicolon

Page 19: Karel The Robot Nested If Statements While Loops 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

Page 20: Karel The Robot Nested If Statements While Loops 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

Page 21: Karel The Robot Nested If Statements While Loops 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

Page 22: Karel The Robot Nested If Statements While Loops 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 (when Karel is already testing for the ending condition (when

left is no longer blocked) and doing so iteratively or left is no longer blocked) and doing so iteratively or repetitively. repetitively.

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

Page 23: Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While

Version 4: iterate 30 times begin IF left-is-blocked then begin if front-is-blocked then begin turnright; end else begin move; end; end; end;

Version 5Version 5::

WhileWhile left-is-blocked left-is-blocked DODO beginbegin If front-is-blocked then If front-is-blocked then beginbegin turnright;turnright; endend elseelse beginbegin move;move; end;end; end;end;

Be sure to match the Be sure to match the Begin/End statementsBegin/End statements

Copyright © 2008 by Helene G. Kershner

Page 24: Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and WhileKarel the Robot: Nested Ifs and While

NoteNote: Looking at the code, : Looking at the code, we can see that it will we can see that it will solve more than just our solve more than just our starting maze.starting maze.

This code will work on This code will work on any “right-handed” maze. any “right-handed” maze.

HINT:HINT: The basic solution for The basic solution for this maze works for your this maze works for your final project! final project!

Copyright © 2008 by Helene G. Kershner