Download presentation
Presentation is loading. Please wait.
Published byBertram McBride Modified over 9 years ago
1
Computer Science 101 If Statement
2
Python Relational Operators
3
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)
4
The If-Statement The syntax for the If-Statement is if : The syntax for the If-Statement is if : would be replaced by actual condition, etc. would be replaced by actual condition, etc. The colon is requiredThe colon is required The list of statements, must be indented – part of the syntax for PythonThe list of statements, must be indented – part of the syntax for Python
5
The If-Statement The semantics are as expected – The semantics are as expected – the condition is evaluated the condition is evaluated if the condition is true, the list of statements is executed if the condition is true, the list of statements is executed
6
If-Statement Semantics T F Cond
7
If-Statement examples if yearsWorked > 10 : bonus = 1000 if yearsWorked > 10 : bonus = 1000 if age >= 65 : total = 0.85 * total numSeniors = numSeniors + 1 if age >= 65 : total = 0.85 * total numSeniors = numSeniors + 1
8
The If-else-Statement The syntax for the If-Else-Statement is if : else : The syntax for the If-Else-Statement is if : else : Note colon after else Note colon after else The semantics are as expected - The semantics are as expected - the condition is evaluated the condition is evaluated if the condition is true, then the first list of statements is executed; otherwise, the second list is executed if the condition is true, then the first list of statements is executed; otherwise, the second list is executed
9
If-Else-Statement examples if yearsWorked > 10 : if yearsWorked > 10 : bonus = 1000 else : bonus = 500 bonus = 1000 else : bonus = 500 if age >= 65 : price = 0.85 * price numSeniors = numSeniors + 1 else : nonSeniors = nonSeniors + 1 if age >= 65 : price = 0.85 * price numSeniors = numSeniors + 1 else : nonSeniors = nonSeniors + 1
10
Python Session
11
Python Session (cont)
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.