Lecture-12 Selection_Boolean_ 24-25
Page 1: Introduction
COMP101 Introduction to Programming 2024-25
Lecture 12: Selection and Boolean
Page 2: Boolean Type
Data Types in Programming
Programming languages feature various data types, which may vary in number.
Common data types:
Integer
Float
String
Boolean (bool)
Boolean Types:
Always evaluate to True or False.
Utilize comparison operators and Boolean operators:
Comparison operators: ==, != or <>, >, <, >=, <=
Boolean operators: and, or, not
Used to create compound conditions.
Page 3: Comparison Operators
Function: Evaluation in selection statements returns True or False.
Types of Comparison Operators:
==: Equal to (2 == 2 is True)!=or<>: Not equal to (2 != 3 is True)>: Greater than (2 > 1 is True)<: Less than (2 < 3 is True)>=: Greater than or equal to (2 >= 2 is True)<=: Less than or equal to (2 <= 2 is True)
Page 4: Comparison Operators: Opposites
Opposite Operators:
==has an opposite of!=>has an opposite of<=<has an opposite of>=
Page 5: Boolean & Comparison Operators: Precedence
Precedence Table (BODMAS):
Highest: ( ) ** * / % + -
Comparison Operators: <, <=, >, >=, ==, !=
Boolean Operators: not, and, or (lowest precedence)
Page 6: Boolean Operators: and
Characteristics:
Evaluates two inputs (a, b):
A binary operator.
Evaluates to True if both a and b are True.
Truth Table:
T, T -> T
T, F -> F
F, T -> F
F, F -> F
Page 7: Boolean Operators: and (Range Test)
Single Input Evaluation:
Can evaluate input ranges, e.g.,
if (a > 3 and a < 300).Correct syntax requires testing both limits clearly.
Range Limits: 4 to 299 when using
(a > 3 and a < 300).
Page 8: Boolean Operators: and (Specification Testing)
Ineffective Tests:
Take care when specifying numeric ranges.
Clarify logic in code to avoid confusion regarding limits.
Example of clearer specification:
if (mark >= 50 and mark <= 64).
Page 9: Boolean Operators: and (Compound Expression)
Avoid Compound Expressions:
Can lead to long, complicated condition checks.
Usage: Favor clarity with simple conditional logic structures like if-elif-else.
Page 10: Boolean Operators: or
Characteristics:
Evaluates inputs (a, b):
A binary operator.
Executes if either a or b is True.
Truth Table:
T, T -> T
T, F -> T
F, T -> T
F, F -> F
Page 11: Boolean Operators: or (Single Input)
Single Input Evaluation:
Example:
if (a < 0 or a > 120).Test returns True for either condition being satisfied.
Ensure clarity, avoiding missing inputs like
if (a < 0 or > 120).
Page 12: Boolean Operators: not
Characteristics:
Unary operator, evaluates a single input (a).
Recommended to use sparingly for clarity.
Truth Table:
T -> F
F -> T
Page 13: Boolean Operators: not (Pass Logic)
Example with pass/fail Logic:
Using
if not mark < 40:vs.if mark >= 40:for improved readability.Better clarity by writing tests in positive terms.
Page 14: Comparison Operators: not (Syntax Errors)
Input Testing Example:
Syntax error handling when applying
notdirectly.Equivalent clearer version using
!=:if x != 5:.
Page 15: Boolean Operators: Short-Circuit Evaluation
Efficiency in Evaluation:
for 'and': If a is False, no need to check b.
for 'or': If a is True, b is not evaluated.
Page 16: Summary
General Guidelines:
Avoid intricate tests and excessive use of
not.Favor simple, readable conditions.
Utilize if-elif-else for clarity over complex compound expressions.
Page 17: Python if-elif-else Example
Code Structure Demonstration:
Clear hierarchical structure of if statements.
Example shows logical flow for month evaluation.
Page 18: Python if-elif-else Robustness
Robust Code Example:
Clear handling of different month cases.
Shows effective elimination of false cases for improved logic.