Lecture04 05
Lecture04 05
Lecture 4-5
Decision Structures and Boolean Logic
2
Topics
• The if Statement
• The if-else Statement
• Comparing Strings
• Nested Decision Structures and the if-elif-else Statement
• Logical Operators
• Boolean Variables
3
The if Statement
• Control structure: logical design that controls order in
which set of statements execute
• Sequence structure: set of statements that execute in
the order they appear
• Decision structure: specific action(s) performed only if a
condition exists
• Also known as selection structure
4
Example
11
True
if total >= 10000: total >= 10000
print total
14
15
16
Exercise
• Write an if statement that assigns 20 to the variable y,
and assigns 40 to the variable z if the variable x is
greater than 100.
22
Exercise
• Write a program that reads in a positive integer n from
the user and then prints whether n is even or odd.
Hint: An even number is a multiple of 2. Any multiple of 2
leaves a remainder of zero when divided by 2.
An example run of the program is shown below.
Enter a positive integer: 101
101 is odd
23
24
Comparing Strings
• Strings can be compared using the == and != operators
• String comparisons are case sensitive
• Strings can be compared using >, <, >=, and <=
• Compared character by character based on the ASCII values
for each character
• If shorter word is substring of longer word, longer word is
greater than shorter word
25
ASCII Chart
26
Logical Operators
• Logical operators: operators that can be used to create
complex Boolean expressions
• and operator and or operator: binary operators, connect
two Boolean expressions into a compound Boolean
expression
• not operator: unary operator, reverses the truth of its
Boolean operand
40
The or Operator
• Takes two Boolean expressions as operands
• Creates compound Boolean expression that is true when
either of the sub expressions is true
• Can be used to simplify nested decision structures
• Truth table for the or operator
Expression Value of the Expression
Short-Circuit Evaluation
• Short circuit evaluation: deciding the value of a
compound Boolean expression after evaluating only one
sub expression
• Performed by the or and and operators
• For or operator: If left operand is true, compound
expression is true. Otherwise, evaluate right operand
• For and operator: If left operand is false, compound
expression is false. Otherwise, evaluate right operand
43
if x < 10 or x > 20
print('The value is outside the acceptable range.')
46
47
48
Boolean Variables
• Boolean variable: references one of two values, True
or False
• Represented by bool data type
• Commonly used as flags
• Flag: variable that signals when some condition exists in a
program
• Flag set to False à condition does not exist
• Flag set to True à condition exists
49
or
51
52
Summary
• This chapter covered:
• Decision structures, including:
• Single alternative decision structures
• Dual alternative decision structures
• Nested decision structures
• Relational operators and logical operators as used in creating
Boolean expressions
• String comparison as used in creating Boolean expressions
• Boolean variables
53
Exercise
• Write a program that asks the user to enter 3 integers.
The program should display the maximum number.
An example run of the program is shown below.
Enter integer #1: 10
Enter integer #2: 35
Enter integer #3: 9
The maximum number is 35
54
55
Exercise
56