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 – 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 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.
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.
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.
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.
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.
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.
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.
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.
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by and modified for Mr. Smith’s AP Computer.
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.
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)
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 6: Loops.
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
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.
Copyright © 2008 by Helene G. Kershner
Karel J Robot Chapter 6.
CS 106A, Lecture 2 Programming with Karel
Java Programming: Guided Learning with Early Objects
MSIS 655 Advanced Business Applications Programming
SSEA Computer Science: CS106A
Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab
Chapter 6: Repetition Statements
CH5 – Conditional Statements
Nested If Statements While Loops
FLUENCY WITH INFORMATION TECNOLOGY
More Loops Topics Counter-Controlled (Definite) Repetition
Repetition Statements (Loops) - 2
Iteration (Loops) Loops repeat a set of instructions
The structure of programming
More Loops Topics Counter-Controlled (Definite) Repetition
Thinking procedurally
Review of Previous Lesson
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Looping and Repetition
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(); } 2

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

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) 4

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

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-- ) 6

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

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

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. 9

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

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

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

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

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. 14

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. 15

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. 16

Remember to ITCH Initialize Test CHange 17

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 18

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

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

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

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 22

Solution 23

Solution 24

Solution 25

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

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

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. 28

Solution: Constructor and Main Method 29

Solution: toBeeper() method 30

Solution: The World 31