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

Slides:



Advertisements
Similar presentations
Karel – Making More Complex Decisions IF / THEN / ELSE IF THEN BEGIN Instructions END ELSE BEGIN Instructions END Do these when test = False Do these when.
Advertisements

1 of 3 Karel Karel is an educational programming language for beginners, created by Richard E. Pattis (currently at Pace University, NY). Pattis used the.
Copyright, Joseph Bergin
Nested If Statements While Loops
Week 5: Loops 1.  Repetition is the ability to do something over and over again  With repetition in the mix, we can solve practically any problem that.
Karel’s Sensory Equipment This PPT originated with Dr. Untch and Dr. Hankins Modifications have been made by Dr. Cripps.
Karel the Robot -- ITERATE Problem Statement: Karel is told to “take a walk around the block!” Revise Algorithm: Define move ahead 5 streets Define turnright.
Karel The Robot In the beginning… software. Karel the Robot  All robots are controlled by software  Artificially intelligent robots that can “think”
Conditionals How do we solve tasks in which every particular of a task is not specifically known? – A robot needs the ability to survey its immediate environment.
Robot? What’s a Robot? Introducing Karel-the-Robot.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Karel JRobot Karel is an educational programming language for beginners, created by Richard E. Pattis (currently at Pace University, NY). Pattis used the.
Chapter 5 Conditionally Executing Instructions
Karel J Robot An introduction to BlueJ and Object- Oriented Programming.
1 Ch. 7 Recursion similar to iteration in that you repeatedly do a little bit of the task and then “loop” again and work on a smaller piece - eventually.
Maze Challenge Maze Challenge activity > TeachEngineering.org
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
Karel the Robot A Gentle Introduction to the Art of Programming.
Programming Errors Lexical errors – occur whenever Karel reads a word that is not in his vocabulary. Example in English: We are asking directions and instead.
CPS120 Introduction to Computer Programming The Programming Process.
Intermediate 2 Software Development Process. Software You should already know that any computer system is made up of hardware and software. The term hardware.
Recursion – means to recur or to repeat – A different way to get a robot to repeat an action A programming language that allows recursive definitions (and.
Karel J. Robot Tool for learning OOP (Lecture covers Ch. 1 and 2)
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
15-100: Introduction to Programming w/ Java * Ananda Gunawardena -- Lecture – School of Computer Science – Phone : (x81559) – Office: Wean Hall.
Chapter 5.  We’ve been using UrRobot – knows only 5 things  Let’s now use a new type of Robot: Robot!  i.e. – public class MileMover extends Robot.
Extending Karel’s Vocabulary This PPT originated with Dr. Judy Hankins Modifications have been done by Dr. Untch & Dr. Cripps.
Programming in Karel Eric Roberts CS 106A January 6, 2016.
Sequencing How to get better. Getting better – Level 3 Putting Instructions in a Sequence You can put instructions into a sequence You understand that.
Karel J. Robot Chapter 6 Instructions That Repeat.
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
BEGINNER FLL PROGRAMMING WORKSHOP BY DROIDS ROBOTICS & EV3LESSONS.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
PYTHON WHILE LOOPS. What you know While something is true, repeat your action(s) Example: While you are not facing a wall, walk forward While you are.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
1 Chapter 5 Karel J Robot 2 Chapter 5 Chapter 5 Conditional Statements Flavor 1: if ( ) { } For now: these are method invocations (see next slide)
Karel J. Robot Chapter 6 Instructions That Repeat.
LET’S FINISH - TODAY’S OBJECTIVE: Create an algorithm to direct a human “robot” from one part of the room to another. Create an algorithm to direct a human.
CS 106A, Lecture 3 Problem-solving with Karel
Mile-long hurdle race Suppose that we want to program Karel to run a one-mile long hurdle race, where vertical wall sections represent hurdles. The hurdles.
In the beginning… software
Eric Roberts and Jerry Cain
Copyright © 2008 by Helene G. Kershner
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
Copyright © 2008 by Helene G. Kershner
Karel – Primitive Instructions
Karel J Robot.
CS 106A, Lecture 2 Programming with Karel
Programming – Touch Sensors
Karel the Robot – Making Decisions
SSEA Computer Science: CS106A
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Line Following Behavior
Looping and Random Numbers
Loops CIS 40 – Introduction to Programming in Python
slides courtesy of Eric Roberts
Algorithm and Ambiguity
Nested If Statements While Loops
Java Programming Loops
Python While Loops.
Starter Look at the hand-out.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

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

Karel the Robot: Nested Ifs and While Five Block Square Maze  Problem statement; Karel needs to move through the five block square maze and stop when he gets to the opening.  Define Output: Karel stops when he locates the opening in the maze.  Define Input. Karel is at the beginning of a 5-block square maze, facing east. Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While Version 1:  If we didn’t know more than the primitive instructions we would do something like:  Move;  Turnleft;  Move;  Turnleft;  … Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While Version 2: How do we get Karel to move around the maze with less of the programmer’s help? What question would Karel need to ask to determine IF it were time to turn left? If front-is-clear then begin begin move; move; end endelse begin begin turnleft; turnleft; end; end; Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While  What happens when we enter that code?  Karel moves exactly one block. Why?  Because, we only told Karel to move one time or turnleft one time. Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While Version 3: How do we get Karel to move a bunch of times? Iterate 40 times begin If front-is-clear then If front-is-clear then begin begin move; move; end endelse begin begin turnleft; turnleft; end; end;end; Karel certainly moves around the maze when we combine the iterate command with the IF statement. Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While Are we solving the problem we were asked to solve? Problem statement; Karel needs to move through the five block square maze and stop when he gets to the opening. Reading the problem statement carefully you’ll notice we are NOT solving the right problem. Why? Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While Well, 1. Karel stops when the iterate command finishes, wherever he happens to be. 2. Karel does not stop when he reaches the opening as the problem statement required. 3. In fact according to this program, Karel has no idea whether he has reached the opening or not. Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While  How does Karel know when the opening is reached?  How does Karel know when he has completed his walk through the maze?  We, the programmer, could count the number of intersections, but that sort of misses the point.  What statement do we use to allow Karel to make a decision? Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While  We need to create a decision (IF) that asks 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 conditions front-is-clearfront-is-blocked left-is-clearleft-is-blocked right-is-clearright-is-blocked next-to-a-beepernot-next-to-a-beeper any-beepers-in-beeper-bagno-beepers-in-beeper-bag Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While We are looking for a test that Right recognizes that when the opening is to Karel’s Right, then he is done. Remember our choices: front-is-clear front-is-blocked left-is-clear left-is-blocked right-is-clear right-is-clear right-is-blocked next-to-a-beeper not-next-to-a-beeper any-beepers-in-beeper-bag no-beepers-in-beeper-bag Copyright © 2008 by Helene G. Kershner Opening

Karel the Robot: Nested Ifs and While When the right-is-clear Karel is done. We could write the following: If right-is-blocked then Begin -- Move through the maze End; But, How do we move through the maze? Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While Earlier we solved moving through the maze using the following IF statement: This is how we move throught the maze, one intersection at a time. If front-is-clear then If front-is-clear thenbegin move; move;end else elsebegin turnleft; turnleft;end; Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While  For Karel to travel through the maze and do all the checking himself, the following questions need to be 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: Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While If right-is-blocked then begin If front-is-clear then If front-is-clear then begin beginmove; end end else else begin begin turnleft; turnleft; end; end; Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While Version 4: Using nested Ifs, how might we solve our original problem? Using Iterate, we still need to “guess” in advance how many intersections Karel needs to go through. Iterate 40 times begin If right-is-blocked then If right-is-blocked then begin begin If front-is-clear then If front-is-clear then begin begin move; move; end end else else begin begin turnleft; turnleft; end; end; end; Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While What happens when Karel reaches the opening in the Maze? Karel knows not to turn Karel knows not to turn Karel knows not to move ahead Karel knows not to move ahead BUT, Karel keeps repeating his tests because the ITERATE statement tells him to keep testing. BUT, Karel keeps repeating his tests because the ITERATE statement tells him to keep testing. So, while Karel has correctly solved the required problem he has not done so efficiently. Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While The While Instruction or While Loop  With ITERATE, the programmer needs to determine in advance of running the program how many time Karel will perform an activity  Karel uses the WHILE instruction whenever a task is to be repeated an undetermined number of times.  The WHILE instruction essentially combines an IF with an Iterate. This allows Karel to determine, without human intervention, when the required task has been accomplished. Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While The While Instruction or While Loop while DO begin begin ; ; end end Ex: while front-is-clear DO begin putbeeper; putbeeper; move; move;end; Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While  Our maze problem is perfectly suited to using While.  Remember, a while loop essentially combines a decision (IF) with a repeat (Iterate) statement.  Karel is already testing for the ending condition (right-is- blocked) and doing so iteratively or repeatively.  We can replace both the Iterate statements and the IF test with a while statement.  A while statement is often called a While Loop because like the Iterate statement it repeats a set of instructions. Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While Version 4: Iterate 40 times begin If right-is-blocked then If right-is-blocked then begin begin If front-is-clear then If front-is-clear then begin begin move; move; end end else else begin begin turnleft; turnleft; end; end; end; Version 5: While right-is-blocked DO begin begin If front-is-clear then If front-is-clear then begin begin move; move; end end else else begin begin turnleft; turnleft; end; end; Be sure to match the Begin/End statements Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While Note: Looking at the code, we can see that it will solve more than just our starting maze.  This code will work on any “left-handed” maze. HINT: The basic solution for this maze works for your final project! Copyright © 2008 by Helene G. Kershner