1 / 18

CH5 – Conditional Statements

CH5 – Conditional Statements. Flavor 1: if ( <some boolean expression> ) { <some instruction list> }. For now: these are method invokations (see next slide). IF. “predicates” either Tor F. frontIsClear(); nextToABeeper(); nextToARobot(); facingNorth(); facingSouth(); facingEast();

libitha
Download Presentation

CH5 – Conditional Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CH5 – Conditional Statements Flavor 1: if ( <some boolean expression> ) { <some instruction list> } For now: these are method invokations (see next slide) Chapter 5 - IF

  2. IF “predicates” either Tor F frontIsClear(); nextToABeeper(); nextToARobot(); facingNorth(); facingSouth(); facingEast(); facingWest(); anyBeepersInBeeperBag(); if ( ) { … } Robot Let’s check the API to see our options Chapter 5 - IF

  3. Robot Class public class Robot extends UrRobot { public boolean frontIsClear() {…} public boolean nextToABeeper() {…} public boolean nextToARobot() {…} etc… } Now I have a brain! again, you don’t write this class Chapter 5 - IF

  4. Examples if ( karel.frontIsClear() ) { karel.move(); // no danger of hitting wall } if ( karel.anyBeepersInBeeperBag() ) { karel.putBeeper(); //no danger of error } Chapter 5 - IF

  5. Extending Robot public class SmartBot extends Robot { public boolean beeperIsToLeft() {…} public boolean twoBeepersOnCornerOrMore() {…} public void faceEast() {…} } Draw the Inheritance Hierarchy Chapter 5 - IF

  6. MUST put world back in initial situation that it was in BEFORE the method was invoked public boolean beeperIsToLeft() { turnLeft(); move(); if ( nextToABeeper() ) { turnLeft(); turnLeft(); move(); turnLeft(); return true; } turnLeft(); turnLeft(); move(); turnLeft(); return false; } Chapter 5 - IF

  7. you write twoBeepersOnCornerOrMore()it belongs to the SmartBot classnote: you may have to nest if statements Chapter 5 - IF

  8. you write faceEast()it belongs to the SmartBot class Chapter 5 - IF

  9. UrRobot Robot SmartBot Paying Attention? • For the last several slides, I’ve lead you somewhat astray. By now (I’m hoping) someone has pointed out that our Inheritance Structure looks likes this What annoying thing (should have) happened to you while coding the last few examples? Yep, you wrote (or wanted to) turnRight() and maybe even turnAround() AGAIN! ANNOYING! Solution(s)? Chapter 5 - IF

  10. Boolean Operators • Same as C++ (&&, ||, !) • Karel (Java) Robot (&&, ||, !) • if (! frontIsClear()) { turnLeft(); } move(); Chapter 5 - IF

  11. && and ||(no, I didn’t stutter) Write this method /** * @returns true if there is at least * one beeper on both sides of bot, * false otherwise */ public boolean isBeepOnLeft_And_BeepOnRight() Assume that the class you’re writing this for has SmartBot as its superclass (the one we just suggested). It may help to look at the inheritance tree again. Chapter 5 - IF

  12. IF - ELSE Flavor 2: if ( <boolean expression> ) { <statements> } else { <statements – somewhat different> } Chapter 5 - IF

  13. IF – ELSE Simplifications simplify: if ( frontIsClear() ) { return true; } else { return false; } Chapter 5 - IF

  14. Simplify if ( ! leftIsBlocked() ) { return true; } else { return false; } Chapter 5 - IF

  15. Simplify if ( leftIsBlocked() ) { return false; } else { return true; } Chapter 5 - IF

  16. move(); Simplify – bottom factoring move(); if ( facingSouth() ) { turnLeft(); } else { turnRight(); } if ( facingSouth() ) { turnLeft(); move(); } else { turnRight(); move(); } move(); move(); move(); move(); Chapter 5 - IF

  17. Simplify – top factoring if ( beeperOnLeft() ) { move(); turnLeft(); } else { move(); turnRight(); } Chapter 5 - IF

  18. being redundant again and again and againha ha if ( facingNorth() ) { move(); pickTwoBeepers(); if (facingNorth()) { turnLeft(); } } Chapter 5 - IF

More Related