Karel J. Robot Chapter 6 Instructions That Repeat.

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.
Karel – Chapter 6 Instructions That Repeat
Karel J Robot Chapter 6.
CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
Introduction to Computer Science Instructions that Repeat –iterate instruction –while instruction A Large Program written using Top-Down Design Unit 4.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
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.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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)
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.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CPS120 Introduction to Computer Science Iteration (Looping)
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.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
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.
Loops George Mason University. Loop Structure Loop- A structure that allows repeated execution of a block of statements Loop body- A block of statements;
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
CPS120 Introduction to Computer Science Iteration (Looping)
Looping ROBERT REVAES. Logical Operators  && AND  Both have to be true for it to evaluate to be true.  || OR  One or the other has to be true for.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
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.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
While ( number
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!
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
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.
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.
Chapter 4 Repetition Statements (loops)
Copyright © 2008 by Helene G. Kershner
Programming Logic and Design Fourth Edition, Comprehensive
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Loops We have already seen instances where a robot needs to repeat instructions to perform a task turnRight(); moveMile(); Harvesting beepers in a field.
karel_part4_functions_2
Karel J Robot Chapter 6.
CS 106A, Lecture 2 Programming with Karel
Karel the Robot – Making Decisions
Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab
FLUENCY WITH INFORMATION TECNOLOGY
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Iteration (Loops) Loops repeat a set of instructions
Presentation transcript:

Karel J. Robot Chapter 6 Instructions That Repeat

Task: Have Karel run a mile long hurdle race Old Way public void runRace() { jumpHurdle(); jumpHurdle(); }

Task: Have Karel run a mile long hurdle race Old Way: public void runRace() { jumpHurdle(); jumpHurdle(); } New Way: public void runRace() { for (int i=1; i<=8; i++) { jumpHurdle(); }

Iteration (Loops) Loops repeat a set of instructions Two types of loops: Definite loops ( for ) perform instructions a definite number of times. Indefinite loops ( while ) perform instructions an indefinite number of times (until a certain test fails)

for Loops (Known number of iterations) General form: for ( ; ; ) { }

for Loops (cont.) sets a variable/identifier to a certain value (variable is usually count, i, or j) is a test that is evaluated each time the body is about to be executed (when false, loop is exited) changes the loop control variable (usually i++ or i-- )

for Loops (cont.) Example: for (int i=1; i<=8; i++) { jumpHurdle (); } This causes Karel to jump hurdle 8 times

for Loops (cont.) Example: for (int i=1; i<=5; i++) { jumpHurdle (); } This causes Karel to jump hurdle 5 times

for Loops (cont.) Flow of for loops: 1. Initialization statement executes. 2. If test is true, proceed to step 3; if test is false, go to step Instructions in body of loop are executed. 4. Increment statement executes. 5. Return to step Program proceeds to statements after loop.

Example: turnRight public void turnRight () { for (int i = 1; i <= 3; i++) { turnLeft(); } }

Example: Harvest a row of beepers public void harvestOneRow() { for ( int i=1; i<=5; i++) { move(); pickBeeper(); } }

while Loops (unknown number of iterations) General form: while ( ) { } Loop continues until test is false

while Loops (cont.) Example: while (frontIsClear ()) { move (); } Causes Karel to move continuously until there is a wall in front of it

while Loops (cont.) Flow of while loops 1. If test is true, proceed to step 2; if test is false, go to step Instructions in body of loop are executed. 3. Go to step Statements after loop are executed.

Building a While Loop Step 1 – Identify what is true when the loop is finished (this is always easier than determining what is false while it is still executing). Step 2 – Use the opposite form of step 1’s result as the for the loop.

Building a While Loop (cont’d) Step 3 – make progress toward the goal (completion of the loop) within the loop. Step 4 – Do whatever is required before and/or after the loop to ensure that we solve the given problem.

Remember to ITCH Initialize Test CHange

Avoid Infinite Loops In a for, while, or do-while loop, it is possible for the test to never be false When this happens, the loop continues infinitely Depending on the compiler, application, and other circumstances, an error may occur or the app may crash

Example of Infinite loop while (facingNorth()) { pickBeeper(); move(); }

Example public void clearCornerOfBeepers() { while (nextToABeeper()) { pickBeeper(); }

Example public void goToBeeper() { while (! nextToABeeper()) { move(); } }

Task: Karel is somewhere in the world facing south. One beeper is on each corner between Karel’s current position and the southern boundary wall. There is no beeper on the corner on which Karel is currently standing. Write a method that will pick up all of the beepers. Name the method clearAllBeepersToThe Wall

Nesting Loops One loop can be nested inside of another. Good nestingBad nesting

Example public void clearAllBeepersToTheWall() // corner can have more than one beeper { while (frontIsClear()) { while (nextToABeeper()) { pickBeeper(); } move(); } }

Task: A robot named Karel is facing south in the northwest corner of a room that has no doors or windows. Somewhere in the room, next to a wall, is a single beeper. Instruct Karel to find the beeper by writing a new method findBeeper.