Conditions and if/else - PowerPoint PPT Presentation

About This Presentation
Title:

Conditions and if/else

Description:

Conditions and if/else. Conditions. score 90. Evaluates to true (1) or false (0) ... Statements MUST be indented. if condition: statements. if age = 16: ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 18
Provided by: csUs7
Learn more at: https://www.cs.usfca.edu
Category:

less

Transcript and Presenter's Notes

Title: Conditions and if/else


1
Conditions and if/else
2
Conditions
  • score gt 90
  • Evaluates to true (1) or false (0)
  • Generally
  • variable operator variable
  • variable operator constant

3
Comparison Operators
  • lt
  • gt
  • lt
  • gt
  • NOT the same as
  • !

4
Examples
  • x5 y8 z5 MAX10 initials
  • xlty
  • ygtMAX
  • xltz
  • zgtMAX
  • initialr
  • x!z

5
Logical Operators
  • and
  • or
  • not
  • x5 y8 z5 MAX10 initials
  • xlty and zgtMAX
  • xlty or zgtMAX
  • not(xgty)

6
Precedence
  • function calls
  • unary operators and binary power (-, )
  • /
  • -
  • lt lt gt gt !
  • not
  • and
  • or

7
Short-Circuit Evaluation
  • Stop evaluation when true/false value is
    determined
  • x6 y9
  • xgt2 or y gt 13
  • xlt2 and ygt13

8
Logical Assignment and Negation
  • in_range (xgt0 and xlt10) 1 if x between 1-10,
    0 otherwise
  • in_range 0ltxlt10 Java does not allow this!!!
  • same_initials (first_initialSand
    last_initialR)
  • not_same_initials not(first_initialSand
    last_initialR)
  • not_same_initials (first_initial!S or
    last_initial!R)

9
DeMorgans Theorem
  • not(a and b) gt (not(a) or not(b))
  • not(a or b) gt (not(a) and not(b))

10
Exercises
  • Determine the results of the following statements
    given a6 b9 c12 d-7 e0 f12
  • print a gt d
  • print c lt f
  • print d gt e
  • print c f
  • print c f
  • print c gt b and e gt f
  • print c gt b or e gt f
  • print a or e
  • print e and a

11
if Statement
  • Statements MUST be indented
  • if condition
  • statements
  • if age gt 16
  • print You can get a drivers license.
  • if age gt 21
  • print You can purchase alcohol.
  • print You can gamble.
  • if age gt 16 and age lt 21
  • print You can drive but you cannot gamble.

12
if/else Statement
  • if condition
  • statements
  • else
  • statements
  • if grade gt 60
  • print You passed the class.
  • print Next up, CS112.
  • else
  • print Sorry, you did not pass.
  • print Try again next semester.

13
Nested if Statements
  • if condition
  • if condition
  • statement
  • else
  • statement
  • else
  • statement
  • if grade gt 60
  • print "You passed the class."
  • if grade gt 90
  • print "You passed with an A!"
  • else
  • print "Sorry, you did not pass."

14
Example
  • if num gt 0 and num lt 10
  • print Your number is between 1 and 10
  • else
  • if num gt 10
  • print Your number is too high
  • else
  • print Your number is too low

15
Chained Conditionals
  • if num gt 0 and num lt 10
  • print Your number is between 1 and 10
  • else
  • if num gt 10
  • print Your number is too high
  • else
  • print Your number is too low
  • if num gt 0 and num lt 10
  • print Your number is between 1 and 10
  • elif num gt 10
  • print Your number is too high
  • else
  • print Your number is too low

16
Example
  • if grade gt 60
  • print "You passed the class."
  • if grade gt 90
  • print "You passed with an A!"
  • else
  • print "Sorry, you did not pass.
  • Does this work???
  • if grade gt 60
  • print "You passed the class."
  • elif grade gt 90
  • print "You passed with an A!"
  • else
  • print "Sorry, you did not pass."

17
Using Functions
  • def getGrade(score)
  • if score gt 90
  • return A
  • elif score gt 80
  • return B
  • elif score gt 70
  • return C
  • elif score gt 60
  • return D
  • else
  • return F
Write a Comment
User Comments (0)
About PowerShow.com