Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 karel_IF_part1 Conditional Statements Flavor 1: if ( ) { } For now: these are method invokations (see next slide)"— Presentation transcript:

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

2 2 karel_IF_part1 IF if ( ) { … } frontIsClear(); nextToABeeper(); nextToARobot(); facingNorth(); facingSouth(); facingEast(); facingWest(); anyBeepersInBeeperBag(); Robot predicates either True or False

3 3 karel_IF_part1 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.

4 4 karel_IF_part1 Method Definition public boolean nextToABeeper(); If there is a (one or more) Beeper on the Robots 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

5 5 karel_IF_part1 Method Definition public boolean nextToARobot(); If there is another (one or more) Robot on the Robots current corner, this method returns true. Otherwise (NOT another Robot on current corner), this method returns false.

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

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

8 8 karel_IF_part1 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.

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

10 10 karel_IF_part1 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.

11 11 karel_IF_part1 Indenting by example How to properly indent while writing if structures by example! public void someMethod() { if ( ) { codeGoesHere(); allAdditionalCodeLinesUp(); soAllFirstLettersAreInTheSameColumn(); } afterClosingBracketNextMethodCallGoesHere(); untilAnotherIfStatementIsEncountered(); }

12 12 karel_IF_part1 A word about indenting How 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

13 13 karel_IF_part1 A Final Indenting example if ( frontIsClear()) { move(); turnLeft(); } if ( frontIsClear()) { move(); turnLeft(); if ( frontIsClear()) { move(); } turnLeft();

14 14 karel_IF_part1 Sample Code if (karel.frontIsClear() ) // if (frontIsClear()) { karel.move();// no danger of hitting wall } if ( karel.anyBeepersInBeeperBag() ) { karel.putBeeper();// no danger of error }

15 15 karel_IF_part1 Sample Code for Karel10 if (frontIsClear() ) // if (frontIsClear()) { move();// no danger of hitting wall } if (anyBeepersInBeeperBag() ) { putBeeper();// no danger of error }

16 16 karel_IF_part1 Boolean Operators Robot (Java) (&&, ||, !) –! Not –&& And –|| Or

17 17 karel_IF_part1 ! (not) Write this method /** true if there is a wall * directly in front of the Robot, * false otherwise. */ public boolean isFrontBlocked () { // enter code

18 18 karel_IF_part1 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(); }

19 19 karel_IF_part1 Order of Operations () - Parenthesis ! - not && and || - or A && B || C != A && ( B || C )

20 20 karel_IF_part1 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

21 21 karel_IF_part1 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 Robots 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 Robots 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


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

Similar presentations


Ads by Google