1 karel_part5_loops Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit (known)

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.
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.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Copyright © Texas Education Agency, Computer Programming For Loops.
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.
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)
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.
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.
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.
3. Controlling Program Flow Methods, parameters, and return values Boolean expressions Conditional branching Loops.
1 Karel J Robot OOP approach to learning computer science “Its study involves development of the ability to abstract the essential features of a problem.
CPS120 Introduction to Computer Science Iteration (Looping)
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Ch. 2 1 Karel – Primitive Instructions Basic tools with which all problems are solved (analogies: carpentry, geometry) –move() –turnLeft() –putBeeper()
Karel J. Robot Chapter 6 Instructions That Repeat.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
1 karel_part3_ifElse Conditional Statements or ELSE if ( ) { } else { }
Application development with Java Lecture 6 Rina Zviel-Girshin.
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 Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for Mr. Smith’s AP Computer Science.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Karel the Robot – Review Primitive Commands move pickbeeper putbeeper turnleft Turnoff Karel’s program statements are separated by a semicolon (;) Copyright.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
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
Karel – Primitive Instructions
Lecture 4b Repeating With Loops
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)
Administrative Matters
Programming Logic and Design Fourth Edition, Comprehensive
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 – Primitive Instructions
Karel J Robot.
Karel J Robot Chapter 6.
CS 106A, Lecture 2 Programming with Karel
Java Programming: Guided Learning with Early Objects
Iteration with While You can say that again.
Karel the Robot – Making Decisions
Outline Altering flow of control Boolean expressions
LRobot Game.
Unit 1 Test 1 Redo Friday at 8am here or Friday 4th BLOCK in Math Lab
Chapter 6: Repetition Statements
Repetition Statements (Loops) - 2
Iteration (Loops) Loops repeat a set of instructions
Karel – Primitive Instructions
Presentation transcript:

1 karel_part5_loops Iteration (Loops) Loops repeat a set of instructions Two types of loops: –Definite loops ( for ) perform instructions explicit (known) number of times. –Indefinite loops ( while ) perform instructions an indefinite (unknown) number of times (until boolean test fails).

2 karel_part5_loops for Loops General form: for ( ; ; ) { }

3 karel_part5_loops for Loops (cont.) sets a variable/identifier to a certain value (variable is usually count, i, 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-- )

4 karel_part5_loops i++? What does i++ mean –i++ is the same as i = i + 1; That is not true you say. –This does not imply i = i + 1, it increases the value of i by 1 –E.g., if i has the value 10, then i++ changes the value of i to 11

5 karel_part5_loops i--? What does i-- mean –i-- is the same as i = i - 1; That is not true you say. –This does not imply i equals i - 1, it decreases the value of i by 1 –E.g., if i has the value 10, then i-- changes the value of i to 9

6 karel_part5_loops for Loops (cont.) Example: for (int x=1; x <= 5; x++) { karel.move(); } This causes k arel to move 5 times

7 karel_part5_loops 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 6 3.Instructions in body of loop are executed 4.Increment statement executes 5.Return to step 2 6.Program proceeds to statements after loop

8 karel_part5_loops while Loops General form: while ( ) { } What do you know here? – Test = false Loop continues until test is false

9 karel_part5_loops while Loops (cont.) Example: while (karel.frontIsClear()) { karel.move (); } What do you know? – karel is facing a wall Causes k arel to move continuously until there is a wall in front of it

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

11 karel_part5_loops Infinite Loops In a for or 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

12 karel_part5_loops while Loops (cont.) Write a method that turns the Robot Left until the Robot is facing North. public void faceNorth() { while (! facingNorth() ) turnLeft(); } * note: the number of left turns needed is unknown

13 karel_part5_loops Examples Pick up seven beepers. public void pick7Beepers() { // what type of loop? }

14 karel_part5_loops Examples Pick up seven beepers. public void pick7Beepers() { for(int j = 1; j <= 7; j++) pickBeeper(); }

15 karel_part5_loops Another Examples Move forward until next to another Robot. public void moveToRobot() { // what type of loop }

16 karel_part5_loops Another Examples Move forward until next to another Robot. public void moveToRobot() { while (! nextToARobot() ) move(); }

17 karel_part5_loops One Last Example Create a method that moves the Robot forward a specific number of times. public void moveForward(int numMoves) { for(int j = 1; j <= numMoves; j++) move(); }

18 karel_part5_loops Comments on Previous Example public void moveForward(int numMoves) { for(int j = 0; j < numMoves; j++) move(); } numMoves is a parameter provided by the client. Sample calls: moveForward(11); // moves the robot forward 11 times moveForward(7); // moves the robot forward 7 times moveForward(15+7); // moves the robot forward 22 times moveForward(15/4+7); // moves the robot forward 10 times moveForward(num); // moves the robot forward num times

19 karel_part5_loops One More Time Create a method that has the Robot put a specific number of Beepers. public void put_N_Beepers(int numBeeps) { for(int j = 1; j <= numBeeps; j++) putBeepers(); }

20 karel_part5_loops Nested Loops Like nested if’s, it is possible to have a loop inside a loop for(int j = 0; j < 3; j++) { for(int k = 0; k < 2; k++) System.out.println(j + “,“ + k); } Generates the following output: Note: Each pair of numbers are on separate lines 0,0 0,1 1,0 1,1 2,0 2,1

21 karel_part5_loops Nested Loops - Again for(int j = 0; j < 2; j++) { int k = 0; while(k < 4) { System.out.println(j + “,“ + k); k++; } Generates the following output: Note: Each pair of numbers are on separate lines 0,0 0,1 0,2 0,3 1,0 1,1 1,2 1,3

22 karel_part5_loops Nested Loops – Again 2 for(int j = 0; j < 5; j++) { int k = 0; while(k < 3) { k++; System.out.println(j + “,“ + k); } Generates the following output: Note: Each pair of numbers are on separate lines 0,1 0,2 0,3 1,1 1,2 1,3 2,1 2,2 2,3 3,1 3,2 3,3 4,1 4,2 4,3

23 karel_part5_loops Nested Loops – With Functions Suppose public int calculate(int a, int b) { return 2 * a - b + 7; } then for(int j = 0; j < 3; j++) { int k = 0; while(k <= 3) { k++; System.out.println( calculate(j, k) ); } Generates the following output: Note: Each number is on a separate line. 6, 5, 4, 3, 8, 7, 6, 5, 10, 9, 8, 7, 6

24 karel_part5_loops Your First Assignment Implement methods declared in TreasureSeekerBot class. Invoke MainDriver1 to test your implementation. If you correctly implement each method, each Robot will perform a specific task. You will then implement one additional method required by MainDriver2 allowing a Robot to following a specific series of commands to find the Hidden treasure! See handout ( Karel_part5_loops.doc ) for details.

25 karel_part5_loops Your Second Assignment Your assignment is to implement the NewAndImprovedBeeperSweeperRobot Robot that picks ALL beepers on every corner in a room. Invoke MainDriver1 to test your implementation. If you correctly implement the class, the Robot will collect ALL beepers from every corner in the Room. The are also different in this assignment. The Rooms will no longer be a constant size. All rooms will 6 wide, but the length will be unknown. –Did you hear me say while loop? In MainDriver2, you will construct Robots to collect All beepers from ALL corner in ALL rooms! See handout ( Karel_part5-1_loops.doc ) for details.

26 karel_part5_loops Your Third Assignment Your assignment is to do lab 16. All three (oops that is four) parts! See handout ( Karel_part6.doc ) for details.