240 likes | 255 Views
Conditionals. How do we solve tasks in which every particular of a task is not specifically known? A robot needs the ability to survey its immediate environment and make decisions about what to do next The IF and the IF/ELSE provide robots with decision making abilities
E N D
Conditionals • How do we solve tasks in which every particular of a task is not specifically known? • A robot needs the ability to survey its immediate environment and make decisions about what to do next • The IF and the IF/ELSE provide robots with decision making abilities • Robot programs contain several different kinds of instructions • Messages to robots, either primitives or new instructions • Constructors (specifications on how to build a robot) • Control statements: IF and IF/ELSE are the first examples of these
The IF instruction • General form if ( <test> ) { <instruction-list> } • Robot determines whether <test> is true or false • <instruction-list> is a sequence of any valid robot commands that will be performed when the robot determines the <test> is true • Nothing will happen when the <test> is false
Example of IF statement // in a method // in a main task block if (nextToABeeper()) if (Karel.nextToABeeper()) { { pickBeeper(); Karel.pickBeeper(); } } turnLeft(); Karel.turnLeft(); • A beeper will be picked up only if there is a beeper on the same corner as the robot • Dependent upon the results of the test • The robot will turn left, regardless of whether a beeper is on the corner or not • Independent of the results of the test
Conditions that a robot can test class Robot extends ur_Robot { boolean frontIsClear(); boolean nextToABeeper(); boolean nextToARobot(); boolean facingNorth(); boolean facingSouth(); boolean facingEast(); boolean facingWest(); boolean anyBeepersInBeeperBag(); }
Class Robot • Inherits all instructions from ur_Robot • We will use the Robot class as our base robot from now on • The new methods of the class are predicates • Preceded by the boolean return type • These methods return either true or false • (functions such as move() or turnLeft() have a void return type, because they return no information to the robot; merely cause the robot to behave in a particular way)
Negative conditions? • Suppose we want a negative form of a predicate? • We can precede a predicate with the negative operator ! if (! Karel.next-To-A-Beeper()) { // stuff }
New predicate instructions • We could create a new subclass of the robot class and provide a new predicate method class myRobot extends Robot { // assume constructor boolean frontIsBlocked() { return ! frontIsCLear(); } // more stuff }
RETURN statement • In the block of the definition of a new predicate we need to indicate what value is to be returned • We need to use the reserved word return, followed by an expression • In a predicate method the value of the expression must be true or false
Use of the new predicate void task() { myRobot R = new myRobot(1, 1, North, 0); if (R.frontIsBlocked()) { // stuff } // and so on }
Walls to the right or left? • Suppose we needed to determine if there is a wall to the right or left? boolean leftIsClear() { turnLeft(); if (frontIsClear()) { turnRight(); // assume definition return true; // method terminates here } turnRight(); return false; } • Return statement immediately terminates the method
Sparse Harvester Demo • The field to be harvested has been hit by a storm, so that not every corner has a beeper. Let us use conditionals to modify Harvester to solve this problem Download SparseHarvester demo will not run correctly because of an error shutoff
For practice – two methods • faceNorthIfFacingSouth() • faceNorth()
The IF/ELSE instruction • General form if ( <test> ) { // note lack of semicolon <instruction-list-1> } else { // note lack of semicolon <instruction-list-2> } • Robot determines whether <test> is true or false • <instruction-list-1> is a sequence of any valid robot commands that will execute when the robot determines the <test> is true • <instruction-list-2> will execute when the <test> is false
Racer Robot • A hurdle jumping race A robot will run a mile. The course may or may not have an obstacle (wall) in the path. If the robot encounters a wall, it will go around (hurdle) the wall. Otherwise it will continue on it’s path… Download Racer demo
Nested IF Instructions • Written with an IF instruction nested inside the THEN or ELSE clause of another IF • As an example: if ( <test> ) { if ( <test> ) { <instruction-list-1> } else { <instruction-list-2> } } else { <instruction-list-2> }
The Replanter Robot The field to be harvested has either 0, 1 or 2 beepers on each corner. Our robot must make sure that each corner has only one and only one beeper on each corner Download Replanter demo
When to Use an IF Instruction • The IF instruction allows a robot to decide whether to execute or skip entirely the block of instructions within the THEN clause • The IF/ELSE instruction allows a robot to decide whether to execute or skip the block of instructions within the THEN clause or the ELSE clause • Nesting these instructions allows robots to make more complex choices
Transformations for Simpler IF Instructions • Transformations can make our programs smaller, simpler, more logical, and more readable. • Two program fragments that result in exactly the same behavior is execution equivalent turnLeft(); putBeeper(); putBeeper(); turnLeft(); • We can create an execution equivalent IF/ELSE from another by replacing <test> with it’s opposite and interchanging the THEN and ELSE clauses
Test Reversal if (frontIsClear()) { if (!frontIsClear()){ move(); jumpHurdle(); } else { } else { jumpHurdle(); move(); } } Used to solve the following difficulty if (<test>) { if (! <test>) { doNothing(); <instruction>; } else { } <instruction>; } // doNothing() is a method that // literally does nothing
Bottom Factoring if (<test>) { if (<test>){ <instruction1>; <instruction1>; <instruction3>; } else { } else { <instruction2>; <instruction2>; } <instruction3>; <instruction3>; } • These two IF statements are execution equivalent
Top Factoring if (<test>) { <instruction1>; <instruction1>; if (<test>){ <instruction2>; <instruction2>; } else { } else { <instruction1>; <instruction3>; <instruction3>; } } • These two IF statements are execution equivalent
Analyze these statements if (nextToABeeper()) { move(); move(); if (nextToABeeper()){ turnLeft(); turnLeft(); } else { } else { move(); turnRight(); turnRight(); } } • Are these two IF statements are execution equivalent? • General rule: top factor only when test conditions between the original and the factored versions of the instructions do not change
Redundant Test Factoring if (facingWest()) { if (facingWest()){ move(); move(); if(facingWest()){ turnLeft(); turnLeft(); } } } • The nested IF on the left is redundant and not needed • Potential difficulty: intervening instructions might change Karel’s position in an unknown way