Nested If Statements While Loops

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
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.
Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.
Autonomy using Encoders Intro to Robotics. Goal Our new task is to navigate a labyrinth. But this time we will NOT use motor commands in conjunction with.
Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner.
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.
Introduction to Computer Science Instructions that Repeat –iterate instruction –while instruction A Large Program written using Top-Down Design Unit 4.
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.
In.  This presentation will only make sense if you view it in presentation mode (hit F5). If you don’t do that there will be multiple slides that are.
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: LeftSpinngingRobot, RightSpinningRobot, GuardRobot, etc)
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
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.
Moving Around in Scratch The Basics… -You do want to have Scratch open as you will be creating a program. -Follow the instructions and if you have questions.
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_part2_Inheritance Extending Robots Tired of writing turnRight every time you start a new karel project. How do we avoid re-writing code all the.
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.
Introduction to OOP in VB.NET using Robots ACSE Conference, Nov 2004 Michael Devoy Monsignor Doyle C.S.S., Cambridge
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.
Karel J. Robot Chapter 6 Instructions That Repeat.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
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.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Compiler Errors Syntax error Lexical Can not resolve/find symbol Can not be applied Execution error Oh wait, a run time error Intent error It ran, but.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
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.
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
Repetition Structures Chapter 9
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
Algorithm The key is the step-by-step instructions.
Karel the Robot – Making Decisions
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Looping and Random Numbers
Loops CIS 40 – Introduction to Programming in Python
Algorithm and Ambiguity
Algorithm The key is the step-by-step instructions.
Nested If Statements While Loops
Java Programming Loops
CSE 111 Karel the Robot.
IF 1-Jul-19.
Starter Look at the hand-out.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Nested If Statements While Loops 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 only knew how to define-new-instructions and had defined define-new-instruction turnright as begin turnleft; end; move; turnright; 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? We could use an IF statement, and an ITERATE statement. iterate 4 times begin if front-is-blocked then turnright; end; iterate 5 times move; Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While Version 3: How do we get Karel to move around the maze without the programmer needing to know anything about the maze except that Karel is always making right turns? The programmer doesn’t know if the maze is 3-squares/side or 50-squares/side. It could even be a rectange instead of a square. What if we don’t even know how many sides the maze has so we couldn’t use an iterate command? HINT! Project 2 REMEMBER: Karel CAN ask complex questions. Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While Version 3: Karel can ask: If front-is-blocked then begin turnright; end else move; 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 turnright 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 30 times begin if front-is-blocked then turnright; end else move; 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 does Karel know Karel is at the opening? Remember Karel can test any of the following conditions front-is-clear front-is-blocked left-is-clear left-is-blocked 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

Karel the Robot: Nested Ifs and While We are looking for a test that recognizes that when the opening is to Karel’s Left, then he is done. Remember our choices: front-is-clear front-is-blocked left-is-clear left-is-blocked 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 Opening Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While When the right-is-clear Karel is done. We could write the following: If left-is-blocked then begin -- Move through the maze end; -- When left is clear DONE 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-blocked then begin turnright; end else move; 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? Do I move forward? Do I turn right? Karel’s code would need to look like this: Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While IF left-is-blocked then begin if front-is-blocked then turnright; end else move; end; These are called NESTED IF statements. One IF is nested inside another IF. Copyright © 2008 by Helene G. Kershner

Karel the Robot: Nested Ifs and While Version 4: Using Nested If statements, 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 30 times begin If left-is-blocked then If front-is-blocked then turnright; end else move; end; NO Semicolon 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 move ahead 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 <test> DO begin <instruction(s)>; end Ex: while front-is-clear DO putbeeper; 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 (when left is no longer blocked) and doing so iteratively or repetitively. 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 30 times begin IF left-is-blocked then if front-is-blocked then turnright; end else move; end; Version 5: While left-is-blocked DO begin If front-is-blocked then turnright; end else move; 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 “right-handed” maze. HINT: The basic solution for this maze works for your final project! Copyright © 2008 by Helene G. Kershner