computer science 101 if statement. python relational operators

12
Computer Science Computer Science 101 101 If Statement If Statement

Upload: bertram-mcbride

Post on 18-Jan-2018

215 views

Category:

Documents


0 download

DESCRIPTION

Primitive Conditions Conditions are expressions which evaluate to true or false. Conditions are expressions which evaluate to true or false. One kind of condition is the comparison of two numerical values of the same type. One kind of condition is the comparison of two numerical values of the same type. Examples: Examples: Payrate > 10 Payrate > 10 (x+10) == (y*z -8) (x+10) == (y*z -8)

TRANSCRIPT

Page 1: Computer Science 101 If Statement. Python Relational Operators

Computer Science Computer Science 101101

If StatementIf Statement

Page 2: Computer Science 101 If Statement. Python Relational Operators

Python Relational Operators Python Relational Operators Operator Meaning

< Less than> Greater than

== Equal<= Less than or equal>= Greater than or equal!= Not equal

Page 3: Computer Science 101 If Statement. Python Relational Operators

Primitive ConditionsPrimitive Conditions ConditionsConditions are expressions which are expressions which

evaluate to evaluate to truetrue or or falsefalse..

One kind of condition is the One kind of condition is the comparison of two numerical values comparison of two numerical values of the same type.of the same type.

Examples:Examples: Payrate > 10Payrate > 10 (x+10) == (y*z -8)(x+10) == (y*z -8)

Page 4: Computer Science 101 If Statement. Python Relational Operators

The If-StatementThe If-Statement The syntax for the If-Statement isThe syntax for the If-Statement is

if <condition> :if <condition> : <list of statements> <list of statements>

• <condition> would be replaced by actual <condition> would be replaced by actual condition, etc.condition, etc.

• The colon is requiredThe colon is required

• The list of statements, must be indented – The list of statements, must be indented – part of the syntax for Pythonpart of the syntax for Python

Page 5: Computer Science 101 If Statement. Python Relational Operators

The If-StatementThe If-Statement

The semantics are as expected – The semantics are as expected –

• the condition is evaluatedthe condition is evaluated

• if the condition is true, the list of if the condition is true, the list of statements is executedstatements is executed

Page 6: Computer Science 101 If Statement. Python Relational Operators

If-Statement SemanticsIf-Statement Semantics

T

F

Cond

Page 7: Computer Science 101 If Statement. Python Relational Operators

If-Statement examplesIf-Statement examples if yearsWorked > 10 :if yearsWorked > 10 :

bonus = 1000 bonus = 1000

if age >= 65 :if age >= 65 : total = 0.85 * total total = 0.85 * total numSeniors = numSeniors + 1 numSeniors = numSeniors + 1

Page 8: Computer Science 101 If Statement. Python Relational Operators

The If-else-StatementThe If-else-Statement The syntax for the If-Else-Statement isThe syntax for the If-Else-Statement is

if <condition> : if <condition> : <list of statements> <list of statements>

else : else : <list of statements> <list of statements>

Note colon after elseNote colon after else The semantics are as expected - The semantics are as expected -

• the condition is evaluatedthe condition is evaluated• if the condition is true, then the first list of if the condition is true, then the first list of

statements is executed; otherwise, the statements is executed; otherwise, the second list is executedsecond list is executed

Page 9: Computer Science 101 If Statement. Python Relational Operators

If-Else-Statement examplesIf-Else-Statement examples if yearsWorked > 10 :if yearsWorked > 10 : bonus = 1000bonus = 1000

else : else : bonus = 500 bonus = 500

if age >= 65 :if age >= 65 : price = 0.85 * price price = 0.85 * price numSeniors = numSeniors + 1 numSeniors = numSeniors + 1 else : else : nonSeniors = nonSeniors + 1 nonSeniors = nonSeniors + 1

Page 10: Computer Science 101 If Statement. Python Relational Operators

Python SessionPython Session

Page 11: Computer Science 101 If Statement. Python Relational Operators

Python Session (cont)Python Session (cont)

Page 12: Computer Science 101 If Statement. Python Relational Operators