Iteration Loops Loops repeat a set of instructions

  • Slides: 26
Download presentation
Iteration (Loops) • Loops repeat a set of instructions • Two types of 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). karel_part 5_loops 1

for Loops • General form: for ( <initialize variable>; <Boolean test>; <increment> ) {

for Loops • General form: for ( <initialize variable>; <Boolean test>; <increment> ) { <instruction set> } karel_part 5_loops 2

for Loops (cont. ) • <initialize variable> sets a variable/identifier to a certain value

for Loops (cont. ) • <initialize variable> sets a variable/identifier to a certain value (variable is usually count, i, j) • <Boolean test> is a test that is evaluated each time the body is about to be executed (when false, loop is exited) • <increment> changes the loop control variable (usually i++ or i--) karel_part 5_loops 3

i++? • What does i++ mean – i++ is the same as i =

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 karel_part 5_loops 4

i--? • What does i-- mean – i-- is the same as i =

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 karel_part 5_loops 5

for Loops (cont. ) • Example: for (int x=1; x <= 5; x++) {

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

for Loops (cont. ) Flow of for loops: 1. Initialization statement executes 2. If

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 karel_part 5_loops 7

while Loops • General form: while ( <boolean test> ) { <instruction list> }

while Loops • General form: while ( <boolean test> ) { <instruction list> } What do you know here? – Test = false • Loop continues until test is false karel_part 5_loops 8

while Loops (cont. ) • Example: while (karel. front. Is. Clear()) { karel. move

while Loops (cont. ) • Example: while (karel. front. Is. Clear()) { karel. move (); } • What do you know? – karel is facing a wall • Causes karel to move continuously until there is a wall in front of it karel_part 5_loops 9

while Loops (cont. ) Flow of while loops 1. If test is true, proceed

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 karel_part 5_loops 10

Infinite Loops • In a for or while loop, it is possible for the

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 karel_part 5_loops 11

while Loops (cont. ) • Write a method that turns the Robot Left until

while Loops (cont. ) • Write a method that turns the Robot Left until the Robot is facing North. public void face. North() { while (! facing. North() ) turn. Left(); } * note: the number of left turns needed is unknown karel_part 5_loops 12

Examples • Pick up seven beepers. public void pick 7 Beepers() { // what

Examples • Pick up seven beepers. public void pick 7 Beepers() { // what type of loop? } karel_part 5_loops 13

Examples • Pick up seven beepers. public void pick 7 Beepers() { for(int j

Examples • Pick up seven beepers. public void pick 7 Beepers() { for(int j = 1; j <= 7; j++) pick. Beeper(); } karel_part 5_loops 14

Another Examples • Move forward until next to another Robot. public void move. To.

Another Examples • Move forward until next to another Robot. public void move. To. Robot() { // what type of loop } karel_part 5_loops 15

Another Examples • Move forward until next to another Robot. public void move. To.

Another Examples • Move forward until next to another Robot. public void move. To. Robot() { while (! next. To. ARobot() ) move(); } karel_part 5_loops 16

One Last Example • Create a method that moves the Robot forward a specific

One Last Example • Create a method that moves the Robot forward a specific number of times. public void move. Forward(int num. Moves) { for(int j = 1; j <= num. Moves; j++) move(); } karel_part 5_loops 17

Comments on Previous Example public void move. Forward(int num. Moves) { for(int j =

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

One More Time • Create a method that has the Robot put a specific

One More Time • Create a method that has the Robot put a specific number of Beepers. public void put_N_Beepers(int num. Beeps) { for(int j = 1; j <= num. Beeps; j++) put. Beepers(); } karel_part 5_loops 19

Nested Loops • Like nested if’s, it is possible to have a loop inside

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 karel_part 5_loops 2, 0 2, 1 20

Nested Loops - Again for(int j = 0; j < 2; j++) { int

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 karel_part 5_loops 1, 1 1, 2 1, 3 21

Nested Loops – 2 Again for(int j = 0; j < 5; j++) {

Nested Loops – 2 Again 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 karel_part 5_loops 22

Nested Loops – With Functions Suppose public int calculate(int a, int b) { return

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 karel_part 5_loops 23

Your First Assignment • Implement methods declared in Treasure. Seeker. Bot class. • Invoke

Your First Assignment • Implement methods declared in Treasure. Seeker. Bot class. • Invoke Main. Driver 1 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 Main. Driver 2 allowing a Robot to following a specific series of commands to find the Hidden treasure! • See handout (Karel_part 5_loops. doc) for details. karel_part 5_loops 24

Your Second Assignment • Your assignment is to implement the New. And. Improved. Beeper.

Your Second Assignment • Your assignment is to implement the New. And. Improved. Beeper. Sweeper. Robot that picks ALL beepers on every corner in a room. • Invoke Main. Driver 1 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 Main. Driver 2, you will construct Robots to collect All beepers from ALL corner in ALL rooms! • See handout (Karel_part 5 -1_loops. doc) for details. karel_part 5_loops 25

Your Third Assignment • Your assignment is to do lab 16. • All three

Your Third Assignment • Your assignment is to do lab 16. • All three (oops that is four) parts! • See handout (Karel_part 6. doc) for details. karel_part 5_loops 26