Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.

Slides:



Advertisements
Similar presentations
1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
Advertisements

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.
Copyright, Joseph Bergin
Nested If Statements While Loops
1 karel_part4_functions.ppt Functions Functions return values or Objects. –Using a function allows the programmer to focus on other task. –Using a function.
Chapter 3 Extending the Robot Programming Language.
Programming in Jessica By Joaquin Vila Prepared by Shirley White Illinois State University Applied Computer Science Department.
Karel The Robot Nested If Statements While Loops Copyright © 2008 by Helene G. Kershner.
Introduction to Computer Science Instructions that Repeat –iterate instruction –while instruction A Large Program written using Top-Down Design Unit 4.
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.
Introduction to Computer Science Returned Values Conditionally Executing Instructions –if instruction –if/else instruction Unit 3.
Extending the Robot Programming Language In the Robot world 1 mile = 8 blocks Suppose we want a robot to run a marathon (26+ miles)? Does our program have.
Chapter 5 Conditionally Executing Instructions
1 karel_part5_loops Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit (known)
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.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
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.
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.
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.
1 Ch. 6 Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit number of times.
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.
Karel J. Robot Chapter 6 Instructions That Repeat.
1 Chapter 5 - IF CH5 – Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)
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.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
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.
1 Karel J. Robot Chapter 5 Conditionally Executing Instructions.
Karel J Robot Chapter 5.
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.
Chapter 3 Extending the Robot Programming Language.
Recursion Powerful Tool
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.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Introduction To Repetition The for loop
Eric Roberts and Jerry Cain
The switch Statement, and Introduction to Looping
Copyright © 2008 by Helene G. Kershner
Chapter 5: Control Structures II
Loop Structures.
Copyright © 2008 by Helene G. Kershner
Karel J Robot.
karel_part4_functions_2
Karel J Robot Chapter 6.
CS 106A, Lecture 2 Programming with Karel
Chapter 5: Loops and Files.
Repetition-Counter control Loop
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Karel the Robot – Making Decisions
slides courtesy of Eric Roberts
Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab
Chapter 6: Repetition Statements
CH5 – Conditional Statements
The Java switch Statement
Nested If Statements While Loops
Repetition Statements (Loops) - 2
Iteration (Loops) Loops repeat a set of instructions
Presentation transcript:

Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field By defining new instructions we have been able to minimize repetitive instructions runMarathon();

Loops for known number of repetitions There are times when you know how many times an instruction needs to be repeated… turnRight() is always 3 left turns moveMile() is always 8 moves() Method turnRight() with a loop: void turnRight() { for (int count = 0; count < 3; count++) { turnLeft(); }

FOR loop: General form for (int count = 0; count < <numberOfTimes>; count++) { <instruction-list>; } int count: count is the name of a variable that stores integers (whole numbers); it’s initial value is 0 count < <numberOfTimes> : as long as the current value of count is less than the number of loops we want, we keep looping; count will be equal to the number of loops we wanted when the loop is finished count++: we increment (add 1 to) the value of count <instructions>: Any valid robot instructions, including other loops

FOR loop: Other examples void moveMile() { for (int count = 0; count < 8; count++) { move(); } Harvester Karel = new Harvester(2, 2, East, 0); for (int count = 0; count < 3; count++) { Karel.pickTwoRows();

FOR loop: What does this do? void whatDoIDo() { for (int count = 0; count < 4; count++) { for (int count2 = 0; count2 < 6; count2++) { move(); } turnLeft();

Loops for an unknown number of repetitions Suppose we know we want a robot to repeat a series of instructions, but we do not know how many times… Task: There is a beeper somewhere on the same street in front of the robot, but we do not know exactly where… How does the robot find the beeper? FOR loop requires an upper limit (and there is no guarantee the beeper will fall within the range) IF statements are a one-time execute statement We need something to combine the testing ability of the IF statement with the iteration of a FOR loop

WHILE loop: General form while ( <test> ) { <instruction-list>; } Reserved word while starts the instruction, parentheses enclose the <test> , and braces enclose the <instruction-list>; in the usual way <test>: The same conditions used in the IF instructions A robot starts the loop by checking the <test> in it’s current situation If TRUE, the robot executes the <instruction-list>; and rechecks the situation If FALSE, the robot executes instructions that follow the WHILE loop

WHILE loop: find the beeper class BeeperFinder extends Robot { // usual constructor // assumes no intervening wall segments void findBeeper() { while (! nextToABeeper()) { move(); }

WHILE loop: Clear a corner A robot is on a corner that may or may not have beepers on it. The robot is to make sure that the corner is clear of all beepers // fill in the blanks void clearCorner() { while ( _____________________) { _____________________; }

Build a WHILE loop: 4 step process When a robot has to do something an unknown number of times (clear a corner of beepers) Step 1: Identify the one test that must be true when the robot is finished with the loop ! nextToABeeper() Step 2: Use the opposite form of the test nextToABeeper() Step 3: Within the WHILE, make progress toward completion of the WHILE pickBeeper() Step 4: Do whatever you need to do before or after the loop to complete solving the problem For this problem, nothing else needs to be done

Clear all beepers to a wall A robot is somewhere in the world facing south. One beeper is on each corner between the robot’s current position and the southern boundary wall. There is no beeper on the corner the robot is currently standing. Write a new method, clearAllBeepersToTheWall(), to pick up all of the beepers Download BeeperSweeper Demo

Is the loop correct? We cannot test all possible solutions We have to convince ourselves the method is correct Step 1: Show the method works correctly when the initial situation results in the test being false Step 2: Each time the loop body executes, the robot’s new situation is simpler and similar to the original situation

Errors to avoid with WHILE loops The fence post problem In order to have five sections of fence, how many fence posts do we need? We often encounter the fence post problem when working with WHILE loops Re-examine BeeperSweeper, in which the robot has a beeper on the corner he starts from

Errors to avoid with WHILE loops Infinite Execution Look at the following loop while ( facingNorth()) { pickBeeper(); move(); } Think of step 3 in building loops (Within the WHILE, make progress toward completion of the WHILE). Is this happening here? What will happen to the robot is he is not facing north? If he is facing north?

Errors to avoid with WHILE loops When is the test of a WHILE loop checked? while (nextToABeeper()) { pickBeeper(); move(); } If there is more than one beeper on a corner, will the robot pick them all before moving on? The robot checks the <test> only before it executes the <instruction-list>, not after each instruction in the list The body of a while is like a fence section, and the test is the fence post. The number of test evaluations is always one more then the number of executions of the body of the WHILE instruction

Nested WHILE loops Look again at BeeperSweeper Instead of just one beeper per corner, there can be any number (possibly 0, but less than infinite) beepers on a corner

WHILE and IF instructions What will happen with this code fragment? if (facingSouth()) { while (! facingSouth()) { turnLeft(); } Conflicting tests

WHILE and IF instructions What will happen with this code fragment? while (! frontIsClear() ) { if (frontIsClear()) { move(); } else { turnLeft(); } Unnecessary test

WHILE and IF instructions What will happen with this code fragment? while (nextToABeeper() ) { if (nextToABeeper()) { pickBeeper(); } Redundant test

When to use repeating instructions