Decision Making CMSC 201 Chang (rev. 2015-02-05)

advertisement

Decision Making

CMSC 201

Chang (rev. 2015-02-05)

Overview

Today we will learn about:

Boolean expressions

Decision making

Boolean Expressions

Boolean expressions evaluate to True or False .

Boolean variables have their own 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(bool1, bool2, bool3)

Prints:

False True True

Boolean Logic

Boolean operators and or not combine boolean values

and

bool1 = ( a and b )

Value of a

True

True

False

False

Value of b

True

False

True

False

Value of bool1

True

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

True

True

False

False

Value of b

True

False

True

False

Value of bool1

True

True

True

False

For (a or b) to be true, at least one of a and b must be true.

bool1 = (not a)

Value of a

True

False

Value of bool1

False

True not a is the opposite of a

not

Value of a

True

True

True

True

False

False

False

False

Complex Expressions

bool1 = a and (b or c)

Value of b

True

True

False

False

True

True

False

False

Value of c

True

False

True

False

True

False

True

False

Value of bool1

True

True

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 whole expression is False and does not depend on the value of (b or c) .

Python will realize this, and if a is False and will not evaluate (b or c) . Try: bool1 = a and print("Hello")

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 line-2 line-3 line-4 line-5

Condition is false

Example

number = int(input("Enter a number ")) if number > 0: print("You entered a positive number") print("This part always execute")

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("Enter a number: ")) b = int(input("Enter another number: ")) if a == b: print("Equal") if a > b: print("Greater") if a < b: print("Less than")

Else

a = int(input("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("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.

line-1 if someBoolean: line-2 line-3 else: line-4 line-5 line-6

Condition is true line-1

Else

line-2 line-3 line-4 line-5

Condition is false

Line-6

Elif

Cascading if statements: 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, and

• a < 0

Elif

line-1 if someBoolean: line-2 line-3 elif someOtherBoolean: line-4 line-5 line-6 someBoolean is true line-1 line-2 line-3 someBoolean is false AND someOtherBoolean is true 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

inputNum = int(input("Enter a number")) if inputNum < 0: if inputNum % 2 == 0: print("Number is even") else: print("Number is odd") elif inputNum > 0: print(inputNum ** 0.5)

Download