04 - Boolean Logic and Decision Structures
04 - Boolean Logic and Decision Structures
The synchronous sessions are recorded (audiovisual recordings). The students are not required to keep their
cameras on during class.
The audiovisual recordings, presentations, readings and any other works offered as the course materials aim
to support remote and online learning. They are only for the personal use of the students. Further use of
course materials other than the personal and educational purposes as defined in this disclaimer, such as
making copies, reproductions, replications, submission and sharing on different platforms including the digital
ones or commercial usages are strictly prohibited and illegal.
The persons violating the above-mentioned prohibitions can be subject to the administrative, civil, and
criminal sanctions under the Law on Higher Education Nr. 2547, the By-Law on Disciplinary Matters of Higher
Education Students, the Law on Intellectual Property Nr. 5846, the Criminal Law Nr. 5237, the Law on
Obligations Nr. 6098, and any other relevant legislation.
The academic expressions, views, and discussions in the course materials including the audio-visual
recordings fall within the scope of the freedom of science and art.
Assign 5 to x
Assign 5 to y
Check : Is x equal to y?
Output : True
Assign 4 to z
Check : Is x equal to z?
Output : False
Logical Operators
Operator Meaning
If both operands must be True for the compound expression to be
and
True
or If any of the operands is True then the compound expression is True
Reverses the logical value of the operand. If the operand is True then
not the operator returns False. If the operand is False then the operator
returns True.
X Y X or Y X and Y X not X
False False False False False True
False True True False True False
True False True False
True True True True
2nd line
3rd line
Cold True
outside?
It is said that the action
Wear a coat
False is 'conditionally executed'
True colon is
Condition required
if code block
False block of
statements
indentation is required
Pseudocode
Get the first test score
Get the second test score
sequential
Get the third test score statement
(last lecture)
Calculate their average (their sum divided by 3)
Display the average
conditional
If the average is greater than or equal to 80
statement
Display the congratulation message (new part)
True
condition
1 1
if condition1: Check condition1 if condition1: Check condition1
statement If True execute this block and statement If False ignore this block and
statement 2 ignore the rest of the structure statement execute the else block
… …
else: else:
statement statement
statement statement 2
… …
Control jumps here to the statement Control jumps here to the statement
3 following the if-else statement 3 following the if-else statement
if temperature < 0:
print(‘Cold’)
Align if and else statements print(‘Turn up the heat!’) The statements in each
else: blockmust be
print(‘Warm’) consistently indented
print(‘Pass the sunscreen’)
Important rules:
§ if clause and else clause must be aligned
§ The statements following the if clause and else clause must be consistently indented
CASE SENSITIVE!!
DO NOT USE
true - false
Capitalize the first letter
SAME
§ You write an if (or if-else) statement in the statement block of another if (or else)
§ They are used to test more than one condition
§ Write a program that asks the user to enter an integer, and displays whether it is zero, a
positive number, or a negative number
Control jumps here to the statement Control jumps here to the statement
3 following the if-else statement 4 following the if-else statement
§ Write a program that asks the user to enter an integer, and displays whether it is zero, a
positive number, or a negative number
N Output
0
-10 C EXAMPLE
0
100
90 A
50
101
91 D
E
0
-10
-20 B
-20
Excercise
§ What is the output of the following program for different values of N?
N Output
hello
4 40
16 100
comp 125
15 10
50 500
505