Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

4.

1 Boolean
Expressions
SY 2020-2021 3rd Term
CS001L Computer Fundamentals and Programming 1
Laboratory
Objectives
This lesson aims to explain:
✓what a control structure is,
✓the difference between sequential,
selection, and iterative control,
✓the use of Boolean operators,
✓and the notion logically equivalent Boolean
expressions.
What is Control Structures?

A control statement is a statement that determines the


control flow of a set of instructions. A control structure is a
set of instructions and the control statements controlling
their execution.

three fundamental forms of control that programming languages


provide:

sequential control
selection control
iterative contro
Control Structures
Boolean Expressions
(Conditions)

The Boolean data type contains two Boolean


values, denoted as True and False in Python.
A Boolean expression is an expression that
evaluates to a Boolean value.
Boolean Expressions:
Relational Operators
The relational operators ==, !=, <,>,<=, => can
be applied to any set of values that has an
ordering.
Boolean Expressions:
Relational Operators
Try
this!
Membership Operators
Python provides membership operators in and
not in for determining if a specific value is in
(or not in) a given list of values.
Membership Operators
Try
this!
Boolean Operators
Boolean (logical) operators in Python are
denoted by and, or, and not.
Boolean Operators

1 <= num <= 10


1 <= num <=10 ➝ 1 <= 15 <= 10 ➝ True <= 10 ➝ ?!?

1 <=num and num <= 10


Try
this!
Operator Precedence and
Boolean Expressions
Operator Precedence and
Boolean Expressions
1

3
Operator Precedence and
Boolean Expressions
Try
this!
Short-Circuit (Lazy) Evaluation

In short-circuit (lazy) evaluation, the second


operand of Boolean operators and and or is
not evaluated if the value of the Boolean
expression can be determined from the first
operand alone.
Short-Circuit (Lazy) Evaluation

if n != 0 and 1/n < tolerance:

if n ! = 0:
if 1/n < tolerance:
Logically Equivalent Boolean
Expressions
Logically Equivalent Boolean
Expressions
Try
this!
End
Thanks!

You might also like