0% found this document useful (0 votes)
15 views

Week 3 - Conditional Statement

This document covers decision structures in Python including if, if-else, and nested if-elif-else statements. It discusses Boolean expressions and logical operators. Examples are provided to demonstrate different decision structures and exercises are included for practice.

Uploaded by

Rani Muniandy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Week 3 - Conditional Statement

This document covers decision structures in Python including if, if-else, and nested if-elif-else statements. It discusses Boolean expressions and logical operators. Examples are provided to demonstrate different decision structures and exercises are included for practice.

Uploaded by

Rani Muniandy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Decision Structures and Boolean

Logic
Week 3

3-1
Topics
• The if Statement
• The if-else Statement
• Comparing Strings
• Nested Decision Structures and the if-elif-else
Statement
• Logical Operators
• Boolean Variables
• Turtle Graphics: Determining the State of the Turtle

3-2
The if Statement (1 of 4)
• 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

3-3
The if Statement (2 of 4)
• 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

3-4
The if Statement (3 of 4)

Figure 3-1 A simple decision structure

3-5
The if Statement (4 of 4)
• 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

3-6
Boolean Expressions and Relational
Operators (1 of 5)
• 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

3-7
Boolean Expressions and Relational
Operators (2 of 5)
• >= 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

3-8
Boolean Expressions and Relational
Operators (3 of 5)
Table 3-2 Boolean expressions using relational operators
Expression Meaning
x > y Is x greater than y?
x < y Is x less than y?
x >= y Is x greater than or equal to y?
x <= y Is x less than or equal to y?
x == y Is x equal to y?
x != y Is x not equal to y?

3-9
Boolean Expressions and Relational
Operators (4 of 5)
• Using a Boolean expression with the > relational
operator

Figure 3-3 Example decision structure

3 - 10
Boolean Expressions and Relational
Operators (5 of 5)
• 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

3 - 11
Example

3 - 12
The if-else Statement (1 of 3)
• 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

3 - 13
The if-else Statement (2 of 3)

Figure 3-5 A dual alternative decision structure

3 - 14
The if-else Statement (3 of 3)

Figure 3-6 Conditional execution in an if-else statement

3 - 15
Example – IF- ELSE

3 - 16
Nested Decision Structures and the
if-elif-else Statement (1 of 3)
• 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

3 - 17
Nested Flow chart

3 - 18
Nested Decision Structures and the
if-elif-else Statement (2 of 3)

Figure 3-12 A nested decision structure

3 - 19
Nested Decision Structures and the
if-elif-else Statement (3 of 3)
• Important to use proper indentation in a nested
decision structure
– Important for Python interpreter
– Makes code more readable for programmer
– Rules for writing nested if statements:
 else clause should align with matching if clause
 Statements in each block must be consistently indented

3 - 20
The if-elif-else Statement (1 of 3)
• if-elif-else statement: special version of a
decision structure
– Makes logic of nested decision structures simpler to
write
 Can include multiple elif statements
– Syntax: if condition_1:
statement(s)
elif condition_2:
statement(s) Insert as many elif clauses
elif condition_3: as necessary.
statement(s)
else
statement(s)

3 - 21
The if-elif-else Statement (2 of 3)
• Alignment used with if-elif-else statement:
– if, elif, and else clauses are all aligned
– Conditionally executed blocks are consistently indented
• if-elif-else statement is never required, but logic
easier to follow
– Can be accomplished by nested if-else
 Code can become complex, and indentation can cause
problematic long lines

3 - 22
The if-elif-else Statement (3 of 3)

Figure 3-15 Nested decision structure to determine a grade

3 - 23
Example

3 - 24
Exercise 1


Write a Python program that defines a variable temperature
and assigns it a numeric value representing the
temperature in Celsius.

The program should then check whether the temperature is
above, below, or equal to 25 degrees Celsius.
– If the temperature is above 25, it should print "It's hot!".
– If the temperature is below 25, it should print "It's cold!".
– If the temperature is exactly 25, it should print "It's just
right!".

3 - 25
Exercise 2


Write a Python program that takes an integer input from the
user and determines whether it is positive, negative, or
zero.
– If the input is positive, the program should print "Positive number".
– If the input is negative, it should print "Negative number".
– If the input is zero, it should print "Zero".

3 - 26
Exercise 3 – Nested IF Else


Write a Python program that checks whether a student has
passed or failed a course based on their exam scores.

The program should first ask the user to enter the exam
score.
 If the score is greater than or equal to 60, the program should check
if it's greater than or equal to 90. If so, it should print "Excellent!
You've passed with distinction!".
 If the score is between 60 and 90 (inclusive), it should print
"Congratulations! You've passed!".
 If the score is less than 60, it should print "Sorry, you've failed.
Better luck next time!".

3 - 27
Exercise 4


Write a Python program to determine the discount a customer receives
based on their total purchase amount.

The program should ask the user to enter the total purchase amount.
– If the purchase amount is greater than or equal to $100, the customer
receives a 10% discount.
– If the purchase amount is between $50 (inclusive) and $100 (exclusive), the
customer receives a 5% discount.
– If the purchase amount is less than $50, the customer receives no discount.
– After determining the discount, the program should calculate and display
the discounted amount and the final amount the customer needs to pay

3 - 28
Expected Output

3 - 29
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
– Determining the state of the turtle in Turtle Graphics
3 - 30

You might also like