1 karel_if_part1 conditional statements flavor 1: if ( ) { } for now: these are method invokations...

21
karel_IF_part1 1 Conditional Statements Flavor 1: if ( <some boolean expression> ) { <some instruction list> } For now: these are method invokations (see next slide)

Upload: amber-ogrady

Post on 26-Mar-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 1

Conditional Statements

Flavor 1:

if ( <some boolean expression> )

{

<some instruction list>

}

For now: these are method invokations (see next slide)

Page 2: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 2

IF

if ( )

{

}

frontIsClear();

nextToABeeper();

nextToARobot();

facingNorth();

facingSouth();

facingEast();

facingWest();

anyBeepersInBeeperBag();

Robot

“predicates”

either True or False

Page 3: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 3

What Does it Mean?

public boolean frontIsClear()

• HELP - The word void has been replaced by the word boolean. This Means this method returns either true or false.

• If there is NO wall in between the Robot and the corner directly in front, this method returns true.

• Otherwise (there is a wall), this method returns false.

Page 4: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 4

Method Definition

public boolean nextToABeeper();

• If there is a (one or more) Beeper on the Robot’s current corner, this method returns true.

• Otherwise (NO Beeper current corner), this method returns false.

• That is, returns true if safe for Robot to pickBeeper

Page 5: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 5

Method Definition

public boolean nextToARobot();

• If there is another (one or more) Robot on the Robot’s current corner, this method returns true.

• Otherwise (NOT another Robot on current corner), this method returns false.

Page 6: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 6

Method Definition

public boolean anyBeepersInBeeperBag();

• If this Robot has any (one or more) beepers in its beeper bag, this method returns true.

• Otherwise (NO beepers in beeper bag), this method returns false.

• That is, returns true if safe for Robot to putBeeper

Page 7: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 7

Method Definition

public boolean facingNorth();

• If this Robots is facing North (up), this method returns true.

• Otherwise (facing South, East or West ), this method returns false.

Page 8: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 8

Method Definition

public boolean facingSouth();

• If this Robots is facing South (down), this method returns true.

• Otherwise (facing North, East or West ), this method returns false.

Page 9: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 9

Method Definition

public boolean facingEast();

• If this Robots is facing East (to the right), this method returns true.

• Otherwise (facing North, South or West ), this method returns false.

Page 10: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 10

Method Definition

public boolean facingWest();

• If this Robots is facing West (to the left), this method returns true.

• Otherwise (facing North, South or East ), this method returns false.

Page 11: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 11

Indenting by example• How to properly indent while writing if structures by example!

public void someMethod()

{

if ( <someBooleanExpression> )

{

codeGoesHere();

allAdditionalCodeLinesUp();

soAllFirstLettersAreInTheSameColumn();

}

afterClosingBracketNextMethodCallGoesHere();

untilAnotherIfStatementIsEncountered();

}

Page 12: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 12

A word about indentingHow to properly indent while writing if structures!

• Every time an if is used, each line after the curly bracket, {, should be spaced over to the right a constant amount (say 3 spaces).

– To simplify, the first letter of each method/line-of-code should be under the ( in the if statement.

• When the if is terminated ( closing curly bracket } ), the following lines of code are moved left by the same number of spaces used above (3 was used above).

– To simplify, the first letter of each method/line-of-code should be under the i in the if statement.

Please review previous slide

Page 13: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 13

A Final Indenting exampleif ( frontIsClear())

{

move();

turnLeft();

}

if ( frontIsClear())

{

move();

turnLeft();

if ( frontIsClear())

{

move();

}

}

turnLeft();

Page 14: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 14

Sample Codeif (karel.frontIsClear() ) // if (frontIsClear())

{

karel.move(); // no danger of hitting wall

}

if ( karel.anyBeepersInBeeperBag() ){ karel.putBeeper(); // no danger of error}

Page 15: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 15

Sample Code for Karel10if (frontIsClear() ) // if (frontIsClear())

{

move(); // no danger of hitting wall

}

if (anyBeepersInBeeperBag() ){

putBeeper(); // no danger of error}

Page 16: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 16

Boolean Operators

Robot (Java) (&&, ||, !) – ! Not– && And– || Or

Page 17: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 17

! (not)Write this method/** * @returns true if there is a wall * directly in front of the Robot, * false otherwise. */

public boolean isFrontBlocked(){

// enter code

Page 18: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 18

What Now?How does the Robot tell if the front direction is blocked?

• The front being blocked is the opposite of front is clear.

• We can write isFrontBlock by using the opposite of isFrontClear.

• Therefore isFrontBlock, looks like:

public boolean isFrontBlock(){ return !isFrontClear(); }

Page 19: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 19

Order of Operations

• () - Parenthesis

• ! - not

• && and

• || - orA && B || C != A && ( B || C )

Page 20: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 20

Your First Assignment

• In assignment Karel03, you knew that every corner contained a beeper.

• In Karel10, Swiper the Fox has stolen Beepers from several corners in the world.

• Your assignment is to implement a new BeeperSweeper Robot that only picks beepers on corners with beepers.

See handout (Karel_10_part1_if.doc) for more details

Page 21: 1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)

karel_IF_part1 21

Your Second Assignment• In this assignment, your Robot will follow a secret path

by obeying the following rules.

1. The path is 41 steps long. The final corner will have will have a wall on three sides and a Beeper on the corner.

2. The Robot should step according to the following rule.1. If the Robot’s path is blocked by a wall, the Robot should turn left. (There

will not be a wall blocking the move after turning left).2. If the Robot’s is on a corner with a beeper, the Robot should turn right.

(There will not be a wall blocking the move after turning right).3. If the path is not blocked and no beeper was on the corner, move forward

one corner.

*See handout (Karel_11_part1_if.doc) for more details