210 likes | 287 Views
Conditional Statements. Flavor 1: if ( <some boolean expression> ) { <some instruction list> }. For now: these are method invokations (see next slide). “predicates” either True or False. IF. frontIsClear(); nextToABeeper(); nextToARobot(); facingNorth(); facingSouth();
E N D
Conditional Statements Flavor 1: if ( <some boolean expression> ) { <some instruction list> } For now: these are method invokations (see next slide) karel_IF_part1
“predicates” either True or False IF frontIsClear(); nextToABeeper(); nextToARobot(); facingNorth(); facingSouth(); facingEast(); facingWest(); anyBeepersInBeeperBag(); if ( ) { … } Robot 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. karel_IF_part1
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 karel_IF_part1
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. 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 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. 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. 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. 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. karel_IF_part1
Indenting by example • How to properly indent while writing if structures by example! public void someMethod() { if ( <someBooleanExpression> ) { codeGoesHere(); allAdditionalCodeLinesUp(); soAllFirstLettersAreInTheSameColumn(); } afterClosingBracketNextMethodCallGoesHere(); untilAnotherIfStatementIsEncountered(); } 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 karel_IF_part1
A Final Indenting example if ( frontIsClear()) { move(); turnLeft(); } if ( frontIsClear()) { move(); turnLeft(); if ( frontIsClear()) { move(); } } turnLeft(); 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 } karel_IF_part1
Sample Code for Karel10 if (frontIsClear() ) // if (frontIsClear()) { move(); // no danger of hitting wall } if (anyBeepersInBeeperBag() ) { putBeeper(); //no danger of error } karel_IF_part1
Boolean Operators Robot (Java) (&&, ||, !) • ! Not • && And • || Or karel_IF_part1
! (not) Write this method /** * @returns true if there is a wall * directly in front of the Robot, * false otherwise. */ public boolean isFrontBlocked() { // enter code 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(); } karel_IF_part1
Order of Operations • () - Parenthesis • ! - not • && and • || - or A && B || C != A && ( B || C ) 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 karel_IF_part1
Your Second Assignment • In this assignment, your Robot will follow a secret path by obeying the following rules. • The path is 41 steps long. The final corner will have will have a wall on three sides and a Beeper on the corner. • The Robot should step according to the following rule. • 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). • 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). • 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 karel_IF_part1