1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for...

31
1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A class

Transcript of 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by and modified for...

Page 1: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

1

Karel – Chapter 6Instructions That Repeat

Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A class

Page 2: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

2

Control structures

Iteration Conditional

Methods Sequential

IterationGraphic Organizer

Types of loops

Java statement

Types of control structures

Syntax

Examples:

Page 3: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

3

Iteration (Loops)

• Loops repeat a set of instructions• Two types of loops:

– Definite loops ( for loops ) perform instructions an explicit number of times

– Indefinite loops ( while loops ) perform instructions an indefinite number of times (until a certain test fails)

Page 4: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

4

Definite vs. Indefinite Loop?

A new class you’re writing has a method with the following signature:public void move (int numMoves)

– Should it be implemented with a definite loop or an indefinite loop? Justify your answer.

– Could it be implemented with the other type of loop? If so, should we? Is it a good idea or bad idea?

Page 5: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

5

Control structures

Iteration Conditional

Definite Loops

Methods Sequential

IterationGraphic Organizer

Indefinite Loops

whilefor

Types of loops

Java statement

Types of control structures

Syntax

Examples:

Page 6: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

6

for Loops

General syntax:

for ( <initialize variable>; <test>; <increment> ){

<instruction set>}

Page 7: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

7

for Loops (cont.)

• <initialize variable> sets a variable/identifier to a certain value (variable is usually count, i, j)

• <test> is a test that is evaluated each time the body is about to be executed (when false, the loop is exited)

• <increment> changes the loop control variable (usually i++ or i--). The incrementing is done at the bottom of the loop.

• i++ means “add 1 to i”. • i-- means “subtract 1 from i”.

Page 8: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

8

for Loops (cont.)

• Example:for (int i=1; i<=3; i++){

move();putBeeper();

}

• This causes the robot to move and place a beeper down on 3 different corners

Note: Write this on the board so that we can refer to it on next slide

Page 9: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

9

for Loops (cont.)

Flow of for loops:1. Initialization statement executes2. If test is true, proceed to next step; if

test is false, go to step 63. Instructions in body of loop are executed4. Increment statement executes5. Return to step 26. Program proceeds to statements after

loop

Page 10: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

10

Control structures

Iteration Conditional

Definite Loops

Methods Sequential

IterationGraphic Organizer

Indefinite Loops

initialize variable test increment

Types of loops

Types of control structures

Syntax

Examples: for ( int i=1; i<=3; i++ ){ move(); putBeeper();}

whilefor Java statement

Page 11: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

11

Move a specified number of times

• Example:

public void move (int numMoves){ for (int i=1; ___________; i++) {

super.move(); }

}

• This causes the robot to move a specified number of times

for (int i=1; i<=numMoves; i++)

Page 12: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

12

Try this yourself• Write a for loop to pick up a beeper

(if there is one) on the four street corners directly in front of the robot:

for (int i=1; i<=4; i++) { move(); if ( nextToABeeper() ) pickBeeper(); }

Page 13: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

13

BeeperSquare Exercise

Page 14: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

14

BeeperSquare Exercise

• Create a BeeperSquare robot class that extends SmartBot or Robot (you may want to use some of its methods)

• Create a method named drawSquare() that uses at least one “for” loop (two “for” loops would be better) that will create a square with 6 beepers on each side. Use a single robot to do this. The end result should be a square containing 20 beepers (only one beeper on each street corner).

• Create a client program that will test this new class. Try constructing two robots and drawing 2 separate squares in the world (overlapping the two).

Page 15: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

15

while Loop Situation

• Team up with your neighbor for a couple minutes and discuss a situation that would necessitate that you use a while (indefinite) loop to solve the problem

– Note: We might use one or two of these as examples later on when we are discussing

while loops.

Page 16: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

16

while Loops

• General form:while ( <test> ){

<instruction list>}

• Loop continues until test is false

Page 17: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

17

while Loops (cont.)

• Example:while (frontIsClear()){

move();}

• Causes the robot to move continuously until there is a wall in front of it

Page 18: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

18

while Loops (cont.)

Flow of while loops:1. If test is true, proceed to step 2; if

test is false, go to step 42. Instructions in body of loop are

executed3. Go to step 14. Statements after loop are executed

Page 19: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

19

Control structures

Iteration Conditional

Definite Loops

Methods Sequential

IterationGraphic Organizer

Indefinite Loops

initialize variable test increment test

Types of loops

Types of control structures

Syntax

Examples: for ( int i=1; i<=3; i++ ){ move(); putBeeper();}

while ( frontIsClear() ){ move();}

whilefor Java statement

Page 20: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

20

Building a While LoopThe following is a more systematic way of creating while loops. This

will be extremely helpful to you at various times in the course and on the AP exam.

Example: the robot moves until it comes to a beeper and then picks it up

• 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 <test> for the loop. You just need to negate the entire thing. (note: Now and again you might find that “DeMorgan-izing” your result is helpful when answering AP-like questions.)

• Step 3 – make progress toward the goal (completion of the loop) within the loop

nextToABeeper()

while ( ! nextToABeeper() )

move();

Page 21: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

21

Building a while Loop (cont’d)

• Step 4 – Do whatever is required before and/or after the loop to ensure that we solve the given problem

• Putting it all together using this 4-step approach:while ( ! nextToABeeper() ){ move();}pickBeeper();

• Another Example: Pick all beepers from a corner without knowing how many there are.

pickBeeper();

Page 22: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

22

while Loop Exercise

• Back on an earlier slide, you drew up a situation that would best be solved using a while loop - now write the code to solve your problem

• If you did not come up with a problem, use the following situation:– The robot should lay down a single beeper

on each street corner in a straight line in the direction it is facing until it comes to a wall. In this case it should stop and turn off.

– To test this, create a robot on street corner (4, 8) which is facing west, and has 10 beepers in its beeper bag.

Page 23: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

23

DeMorgan’s Law

• De Morgan's laws are rules relating the logical operators “and" and “or" in terms of each other via negation.

• In another form:– NOT (a AND b) = (NOT a) OR (NOT b) – NOT (a OR b) = (NOT a) AND (NOT b)

• In java, this corresponds to:– ! (a && b) is the same as !a || !b – ! (a || b) is the same as !a && !b

Page 24: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

24

DeMorgan’s Law

• Useful in Step 2 for negating the end situation

• We want to loop until a wall is in front AND a beeper is on the current corner! frontIsClear() && nextToABeeper()

• Use DeMorgan’s Law to negate this. Negate the two predicates and replace && with || :while ( frontIsClear || !nextToABeeper() )

Page 25: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

25

Let’s build a while loopMove a robot until it is not on a corner with any other robots and there is a wall on the left, then pick up the beeper that will be waiting there (the “hard” part is getting the condition correct without using a trial-and-error method – you don’t get to run any programs during our tests nor on the AP exam – also, you’ll code your labs faster if you have a way to ensure correct conditions EVERY time – without the crutch of running your code)

You may use && and || for this exercise. Use the 4-step process now…

while (???) { ???}

Page 26: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

26

You try the while condition

1) What is true when the loop is finished?!nextToARobot() && !leftIsClear()

2) Use the opposite form of step 1’s result as the <test> for the loopwhile (nextToARobot() || leftIsClear())

3) Make progress toward the goal within loopmove();

4) Do whatever is required before and/or after the loop to ensure that we solve the given problempickBeeper();

Page 27: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

27

You try the while condition

You may use && and || for this

Write a loop that moves a robot forward until it is either next to a robot, next to a beeper, or there is no wall on its right

while ( ??? ) {

???}

Page 28: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

28

You try the while condition

1) What is true when the loop is finished?nextToARobot() || nextToABeeper() || rightIsClear()

2) Use the opposite form of step 1’s result as the <test> for the loop

while (!nextToARobot() && !nextToABeeper() && !rightIsClear())

3) Make progress toward the goal within loopmove();

4) Do whatever is required before and/or after the loop to ensure that we solve the given problem

Nothing required

Page 29: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

29

Fence Post Problem• This is a situation where we must perform some (but

not all) of the statements in the loop either before or after the loop to solve the given problem. This is because the instructions in the loop do not entirely solve the problem for either the first time through the loop or the last time through the loop.

• This is called the Fence Post Problem because if we order 3 fence sections, we actually need 4 fence posts.

• What if we have five beepers in a row. The robot starts on the same corner as the first beeper. It needs to pick up all 5 beepers and it needs to end up at the corner of the last beeper. How would we go about doing this?

for (int i=1; i<=4; i++) { pickBeeper(); move();}pickBeeper();

Page 30: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

30

Fence Post Problemp

ick

Be

ep

er(

)

pic

kB

ee

pe

r()

pic

kB

ee

pe

r()

pic

kB

ee

pe

r()

pic

kB

ee

pe

r()

move() move() move() move()

Page 31: 1 Karel – Chapter 6 Instructions That Repeat Note: Original slides provided by  and modified for Mr. Smith’s AP Computer Science.

31

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 with a memory error