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

[WEEK 3]-LECTURE-3- Decision Logic

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

[WEEK 3]-LECTURE-3- Decision Logic

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Lecture Three

Decision Structures & Boolean Logic


The CSC213 Team
Topics

▪ The if Statement
▪ The if-else Statement
▪ Comparing Strings
▪ Nested Decision Structures and the if-elif-else
Statement
▪ Logical Operators
▪ Boolean Variables
Topics | Progress

▪ The if Statement
▪ The if-else Statement
▪ Comparing Strings
▪ Nested Decision Structures and the if-elif-else
Statement
▪ Logical Operators
▪ Boolean Variables
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 ()

▪ 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
The if Statement
The if Statement
▪ 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

▪ >= 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
Boolean Expressions and Relational Operators

▪ Using a Boolean expression with the > relational


operator
Boolean Expressions and Relational Operators

▪ 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
Topics | Progress

▪ The if Statement
▪ The if-else Statement
▪ Comparing Strings
▪ Nested Decision Structures and the if-elif-else
Statement
▪ Logical Operators
▪ Boolean Variables
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
The if-else Statement
Topics | Progress

▪ The if Statement
▪ The if-else Statement
▪ Comparing Strings
▪ Nested Decision Structures and the if-elif-else
Statement
▪ Logical Operators
▪ Boolean Variables
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
Topics | Progress

▪ The if Statement
▪ The if-else Statement
▪ Comparing Strings
▪ Nested Decision Structures and the if-elif-else
Statement
▪ Logical Operators
▪ Boolean Variables
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

▪ 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
The if-elif-else Statement
▪ 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 condition1
statements
elif condition2
statements
else
statements
The if-elif-else Statement

▪ 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
Topics | Progress

▪ The if Statement
▪ The if-else Statement
▪ Comparing Strings
▪ Nested Decision Structures and the if-elif-else
Statement
▪ Logical Operators
▪ Boolean Variables
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
Expression Value of the Expression
▪ Truth table for false and false false
the and operator 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 Expression
false and false false
the or operator false and true true
true and false true
true and true true
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
The not Operator

▪ Takes one Boolean expressions as operand and


reverses its logical value
▪ Sometimes it may be necessary to place parentheses
around an expression to clarify to what you are applying
the not operator
▪ Truth table for the not operator
Expression Value of the Expression
true false
false true
Checking Numeric Ranges with Logical
Operators

▪ To determine whether a numeric value is within a


specific range of values, use and
Example: x >= 10 and x <= 20
▪ To determine whether a numeric value is outside of
a specific range of values, use or
Example: x < 10 or x > 20
Topics | Progress

▪ The if Statement
▪ The if-else Statement
▪ Comparing Strings
▪ Nested Decision Structures and the if-elif-else
Statement
▪ Logical Operators
▪ Boolean Variables
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
Bit.ly/CSC213QUIZ-2 | Book Club Points

▪ Serendipity Booksellers has a book club that awards points to its customers
based on the number of books purchased each month. The points are awarded
as follows:
▪ If a customer purchases 0 books, he or she earns 0 points.
▪ If a customer purchases 1 book, he or she earns 5 points.
▪ If a customer purchases 2 books, he or she earns 15 points.
▪ If a customer purchases 3 books, he or she earns 30 points.
▪ If a customer purchases 4 or more books, he or she earns 60 points.
▪ Write a program that asks the user to enter the number of books that he or she
has purchased this month and displays the number of points awarded.

36
Summary
▪ This lecture 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
Algorithm Workbench

▪ 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.
▪ Write an if-else statement that assigns 0 to the variable b if the
variable a is less than 10. Otherwise, it should assign 99 to the
variable b.
▪ Write an if-else statement that displays 'Speed is normal' if the
speed variable is within the range of 24 to 56. If the speed
variable’s value is outside this range, display 'Speed is abnormal'.

38
Roman Numerals
▪ Write a program that prompts the user to
enter a number within the range of 1
through 10.
▪ The program should display the Roman
numeral version of that number.
▪ If the number is outside the range of 1
through 10, the program should display
an error message.
▪ The following table shows the Roman
numerals for the numbers 1 through 10:

39
Mass and Weight
▪ Scientists measure an object’s mass in kilograms and its weight
in newtons. If you know the amount of mass of an object in
kilograms, you can calculate its weight in newtons with the
following formula:
▪ weight = mass x 9.8
▪ Write a program that asks the user to enter an object’s mass, and
then calculates its weight. If the object weighs more than 1,000
newtons, display a message indicating that it is too heavy. If the
object weighs less than 10 newtons, display a message
indicating that it is too light.
40
Magic Dates

▪ The date June 10, 1960, is special because when it is written in


the following format, the month times the day equals the year:
▪ 6/10/60
▪ Design a program that asks the user to enter a month (in numeric
form), a day, and a two- digit year. The program should then
determine whether the month times the day equals the year. If so,
it should display a message saying the date is magic. Otherwise,
it should display a message saying the date is not magic.

41
Color Mixer
▪ The colors red, blue, and yellow are known as the primary colors
because they cannot be made by mixing other colors. When you mix
two primary colors, you get a secondary color, as shown here:
▪ When you mix red and blue, you get purple.
▪ When you mix red and yellow, you get orange.
▪ When you mix blue and yellow, you get green.
▪ Design a program that prompts the user to enter the names of two
primary colors to mix. If the user enters anything other than “red,” “blue,”
or “yellow,” the program should display an error message. Otherwise,
the program should display the name of the secondary color that
results.
42
Change for a 100 Naira Game

▪ Create a change-counting game that gets the user to enter the


number of Naira notes required to make exactly one hundred
naira.
▪ The program should prompt the user to enter the number of five
nairas, ten nairas, twenty nairas, and fifty nairas.
▪ If the total value of the naira notes entered is equal to one hundred
naira, the program should congratulate the user for winning the game.
▪ Otherwise, the program should display a message indicating whether
the amount entered was more than or less than one hundred naira.

43
Software Sales
▪ A software company sells a package
that retails for $99. Quantity discounts
are given according to the following
table:
▪ Write a program that asks the user to
enter the number of packages
purchased. The program should then
display the amount of the discount (if
any) and the total amount of the
purchase after the discount.
44
Time Calculator
▪ Write a program that asks the user to enter a number of seconds,
and works as follows:
▪ There are 60 seconds in a minute. If the number of seconds entered by
the user is greater than or equal to 60, the program should display the
number of minutes in that many seconds.
▪ There are 3,600 seconds in an hour. If the number of seconds entered
by the user is greater than or equal to 3,600, the program should display
the number of hours in that many seconds.
▪ There are 86,400 seconds in a day. If the number of seconds entered by
the user is greater than or equal to 86,400, the program should display
the number of days in that many seconds.
45

You might also like