Decision Logic

Download Report

Transcript Decision Logic

Decision Making
CMSC 201
Overview
Today we will learn about:
• Boolean expressions
• Decision making
Boolean Expressions
Last class, we discussed expressions that evaluate to a
number. But we’re also aware of values that evaluate to
true or false, called booleans.
Boolean variables have their own series of operators.
a = True
b = False
c = 10 > 4
d = someVar == someOtherVar
Boolean Math Operators
We can use the following mathematical operators when
constructing boolean expressions:
Operator
Meaning
==
Checks if two things are equal
!=
Checks if two things are NOT
equal
>
Greater than
>=
Greater than or equal to
<
Less than
<=
Less than or equal to
Example
a=4
b=5
c=3
bool1 = a == b
bool2 = c < b
bool3 = c != a
print(a, b, c)
Prints:
False
True
True
Boolean Logic
There are also three boolean operators, and, or, and not.
These are ways of combining different boolean values.
And
bool1 = a and b
Value of a
Value of b
Value of bool1
True
True
True
True
False
False
False
True
False
False
False
False
For “a and b” to be true, both a and b must be true.
Or
bool1 = a or b
Value of a
Value of b
Value of bool1
True
True
True
True
False
True
False
True
True
False
False
False
For “a or b” to be true, either a OR b must be true.
Not
bool1 = not a
Value of a
Value of bool1
True
False
False
True
Not a returns the opposite boolean from a
Complex Expressions
We can combine these operators however we like!
bool1 = a and (b or c)
Python first computes (b or c) then ands the result with a.
Value of a
Value of b
Value of c
Value of bool1
True
True
True
True
True
True
False
True
True
False
True
True
True
False
False
False
False
True
True
False
False
True
False
False
False
False
True
False
False
False
False
False
Short Circuit Evaluation
Notice that in the expression:
bool1 = a and (b or c)
If a is false, the rest of the expression doesn’t matter.
Python will realize this, and if a is false won’t bother with
the rest of the expression.
Practice
a=4
b=5
c=6
d = True
e = False
bool1 = d and (a > b)
bool2 = (not d) or (b != c)
bool3 = (d and (not e)) or (a > b)
bool4 = (a % b == 2) and ((not d) or e)
Numbers and Booleans
What about this?
a=4
b = True
c = a and b
print(c)
Prints:
True
Numbers and Booleans
Python accepts anything that is non-zero as True (there
are some exceptions, but we’ll get into those later).
So technically you can use any integer as a boolean
expression.
Decision Making
Why do we care so much about booleans?
If Statements
An if statement only executes if a given boolean
expression evaluates to True.
if booleanExpression:
line-1
line-2
line-3
line-4
Anything indented in after the if statement executes if and
only if booleanExpression == True
If Statements
line-1
if booleanExpression:
line-2
line-3
line-4
line-5
This code would produce the following flowchart structure:
Condition is true
line-1
Condition is false
line-2
line-3
line-4
line-5
Example
number = int(input(“Please enter a number”))
if number > 0:
print(“You entered a positive number”)
print(“This part always executes”)
Vocab
A block is an indented section of your code.
A conditional is the boolean expression in an if
statement.
If statements are a type of control structure, since it
controls the flow of your code.
Nested If Statements
We can also “nest” if statements.
line-1
if someCondition:
if somethingElse:
line-2
else:
line-3
else:
line-4
Exercise
Write a code snippet that asks for two numbers for the
user. If they are equal, it should print out “Equal”, if the
first is greater than the second, it should print out
“Greater”, and if the second is greater than the first it
should print out “Less than”
Exercise
a = int(input(“Please enter a number: ”))
b = int(input(“Please enter another number: ”))
if a == b:
print(“Equal”)
if a > b:
print(“Greater”)
if a < b:
print(“Less than”)
Else
a = int(input(“Please enter a number: “)
if a > 0:
print(“a is greater than zero!”)
if a <= 0:
print(“a is less than or equal to zero!”)
This pattern, where you have an if statement, followed by
an if statement that is the complete opposite, happens so
often it has a special keyword.
Else
a = int(input(“Please enter a number: “)
if a > 0:
print(“a is greater than zero!”)
else:
print(“a is less than or equal to zero!”)
The “else” keyword says that if the first if statement
doesn’t execute, the else will.
Else
line-1
if someBoolean:
line-2
line-3
else:
line-4
line-5
line-6
Condition is true
line-1
line-2
line-3
line-4
line-5
Condition is false
Line-6
Elif
Imagine we have the following situation: we have an if
statement, and if that if statement DOESN’T execute, we
want another if statement to be evaluated.
if a > 0:
print(“A is positive”)
else:
if a < 0:
print(“A is negative”)
Elif lets us combine that if and that else.
Elif
if a > 0:
print(“A is positive”)
elif a < 0:
print(“A is negative”)
Now the elif statement will only execute if:
• The first statement DOES NOT execute
• a<0
Elif
line-1
if someBoolean:
line-2
line-3
elif someOtherBoolean:
line-4
line-5
line-6
someBoolean is true
line-1
someBoolean is false AND
someOtherBoolean is true
line-2
line-3
line-4
line-5
someBoolean is false AND
someOtherBoolean is false
Line-6
Exercise
Request an input from the user. If it’s positive, print out
the square root. If it’s negative, print out whether it’s even
or odd.
Exercise
import math
inputNum = int(input(“Please enter a number”))
if inputNum < 0:
if inputNum % 2 == 0:
print(“Number is even”)
else:
print(“Number is odd”)
elif inputNum > 0:
print(inputNum ** .5)