Download presentation
Presentation is loading. Please wait.
Published byAntony Wood Modified over 9 years ago
1
1 Karel – Chapter 5 Conditionally Executing Instructions Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com
2
2 Paying Attention? For the last several slides, we’ve been using a new robot class. By now you’ve probably figured out that our Inheritance Structure looks like this: UrRobot Robot SmartBot What annoying thing (should have) happened to you while coding the last few examples? Yep, you wrote (or wanted to) turnAround() and maybe even turnRight() AGAIN! ANNOYING! Solution(s)? BetterRobot
3
3 What if there is only one statement in the THEN clause? if ( frontIsClear()) { move(); } is the same as ….. if ( frontIsClear()) move(); if ( frontIsClear()) { move(); turnLeft(); } is NOT the same as ….. if ( frontIsClear()) move(); turnLeft();
4
4 Nested IF (IF statement inside an IF statement) if ( frontIsClear()) { move(); if ( nextToABeeper()) { pickBeeper(); } }
5
5 public boolean exactlyOneBeeperOnCorner() { if (nextToABeeper()) { pickBeeper(); if (nextToABeeper()) { putBeeper(); return false; } putBeeper(); return true; } return false; }
6
6 0 Beepers 1 Beeper 2 Beepers public boolean exactlyOneBeeperOnCorner() { if (nextToABeeper()) { pickBeeper(); if (nextToABeeper()) { putBeeper(); return false; } putBeeper(); return true; } return false; } Check the lines of code that would execute for each scenario
7
7 0 Beepers 1 Beeper 2 Beepers public boolean exactlyOneBeeperOnCorner() { if (nextToABeeper()) { pickBeeper(); if (nextToABeeper()) { putBeeper(); return false; } putBeeper(); return true; } return false; } Check the lines of code that would execute for each scenario
8
8 Boolean Operators Java uses same boolean operators as C++ ( &&, ||, !) && means AND || means OR ! means NOT Example: if (! frontIsClear() || facingSouth()) { turnLeft(); } move();
9
9 if (frontIsClear() && nextToABeeper()) frontIsClear() nextToABeeper()true truefalse falsetruefalse if (frontIsClear() || nextToABeeper()) frontIsClear() nextToABeeper()true truefalse falsetruefalse result true false result true false
10
10 IF - ELSE Version 2: if ( ) { } else { }
11
11 IF - ELSE Example: if ( beeperIsToLeft() ) { turnLeft(); move(); pickBeeper(); } else { move(); }
12
12 Practice Using && and || Write this method which could be put in SmartBot /* returns true if there is at least one beeper on both sides of bot, false otherwise */ public boolean beeperOnLeftAndRight() { }
13
13 Create a single followWallRight() method to handle each of these situations. Hint: Before coding, look at the four situations and see what is the same and different for each. Start with the initial situation for each robot. How could you use an if statement to determine if they are in a specific situation? This can be done with an if statement that includes nested if statements. Initial Situation End Situation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.