BSCS1350-4
BSCS1350-4
1
Lecture Outline
• The if Statement
• The if-else Statement
• Comparing Strings
• Nested Decision Structures and the if-elif-else Statement
• Logical Operators
• Boolean Variables
• Conditional Expressions
• Assignment Expressions and the Walrus Operator
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
The if Statement (cont’d.)
• In flowchart, diamond represents true/false condition that
must be tested
• Actions can be conditionally executed
• Performed only when a condition is true
• Single alternative decision structure: provides only one
alternative path of execution
• If condition is not true, exit the structure
• Another name for this is: (Simple decision structure)
The if Statement (cont’d.)
The if Statement (cont’d.)
• Python syntax:
if condition:
Statement
Statement
• First line known as the if clause
• Includes the keyword if followed by condition
• The condition can be true or false
• When the if statement executes, the condition is tested, and if it is true
the block statements are executed. otherwise, block statements are
skipped
Boolean Expressions and
Relational Operators
• Boolean expression: expression tested by if statement to determine if
it is true or false
• Example: a > b
• true if a is greater than b; false otherwise
• Relational operator: determines whether a specific relationship exists
between two values
• Example: greater than (>)
Boolean Expressions and
Relational Operators (cont’d.)
• >= and <= operators test more than one relationship
• It is enough for one of the relationships to exist for the expression
to be true
• == operator determines whether the two operands are equal to one
another
• Do not confuse with assignment operator (=)
• != operator determines whether the two operands are not equal
Boolean Expressions and
Relational Operators (cont’d.)
Boolean Expressions and Relational Operators
(cont’d.)
• Using a Boolean expression with the > relational operator
Boolean Expressions and
Relational Operators (cont’d.)
• Any relational operator can be used in a decision block
• Example: if balance == 0
• Example: if payment != balance
• It is possible to have a block inside another block
• Example: if statement inside a function
• Statements in inner block must be indented with respect to the
outer block
Single-Line if Statements
An if statement can be written on a single line if it executes
only one statement.
• Python syntax:
if condition: statement
• Example:
if score > 59: print('You passed!')
Lecture Outline
• The if Statement
• The if-else Statement
• Comparing Strings
• Nested Decision Structures and the if-elif-else Statement
• Logical Operators
• Boolean Variables
• Conditional Expressions
• Assignment Expressions and the Walrus Operator
The if-else Statement
• Dual alternative decision structure: two possible paths of execution
– One is taken if the condition is true, and the other if the condition
is false
• Syntax: if condition:
statements
else:
other statements
• if clause and else clause must be aligned
• Statements must be consistently indented
The if-else Statement (cont’d.)
The if-else Statement (cont’d.)
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
Comparing Strings (cont’d.)
Lecture Outline
• The if Statement
• The if-else Statement
• Comparing Strings
• Nested Decision Structures and the if-elif-else Statement
• Logical Operators
• Boolean Variables
• Conditional Expressions
• Assignment Expressions and the Walrus Operator
Nested Decision Structures and the if-elif-
else Statement
• A decision structure can be nested inside another decision structure
• Commonly needed in programs
• Example:
• Determine if someone qualifies for a loan, they must meet two
conditions:
• Must earn at least $30,000/year
• Must have been employed for at least two years
• Check first condition, and if it is true, check second condition
Nested Decision Structures and
the if-elif-else Statement (cont’d.)
24
Lecture Outline
• The if Statement
• The if-else Statement
• Comparing Strings
• Nested Decision Structures and the if-elif-else Statement
• Logical Operators
• Boolean Variables
• Conditional Expressions
• Assignment Expressions and the Walrus Operator
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
The and Operator
• Takes two Boolean expressions as operands
• Creates compound Boolean expression that is true only when both
sub expressions are true
• Can be used to simplify nested decision structures
• Truth table for Expression Value of the
Expression
the and operator
false and false false
false and true false
true and false false
true and true true
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 Expression Value of the
the or operator Expression
43