Conditional Statements - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Conditional Statements

Description:

Indenting by example. How to properly indent while writing if structures by example! ... A Final Indenting example. if ( frontIsClear()) move(); turnLeft(); if ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 21
Provided by: davidw114
Category:

less

Transcript and Presenter's Notes

Title: Conditional Statements


1
Conditional Statements
  • Flavor 1
  • if ( ltsome boolean expressiongt )
  • ltsome instruction listgt

For now these are method invokations (see next
slide)
2
IF
predicates either True or False
frontIsClear() nextToABeeper() nextToARobot() f
acingNorth() facingSouth() facingEast() facingW
est() anyBeepersInBeeperBag()
  • if ( )

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

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

7
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
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
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
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
Indenting by example
  • How to properly indent while writing if
    structures by example!
  • public void someMethod()
  • if ( ltsomeBooleanExpressiongt )
  • codeGoesHere()
  • allAdditionalCodeLinesUp()
  • soAllFirstLettersAreInTheSameColumn()
  • AfterClosingBracketNextMethodCallGoesHere()
  • untilAnotherIfStatementIsEncountered()

12
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
A Final Indenting example
  • if ( frontIsClear())
  • move()
  • turnLeft()
  • if ( frontIsClear())
  • move()
  • turnLeft()
  • if ( frontIsClear())
  • move()
  • turnLeft()

14
Sample Code
  • if (karel.frontIsClear() ) // if
    (frontIsClear())
  • karel.move() // no danger of hitting wall

if ( karel.anyBeepersInBeeperBag()
) karel.putBeeper() // no danger of error
15
Boolean Operators
  • Robot (Java) (, , !)
  • ! Not
  • And
  • Or

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

17
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()

18
Order of Operations
  • () - Parenthesis
  • ! - not
  • and
  • - or
  • A B C ! A ( B C )

19
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

20
Your Second Assignment
  • In this assignment, our 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.
  • If the Robots path is blocked by a wall, the
    Robot should turn left and move. (There will not
    be a wall blocking the move after turning left).
  • If the Robots is on a corner with a beeper, the
    Robot should turn right and move. (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
Write a Comment
User Comments (0)
About PowerShow.com