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