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

Module 3 - Program Flow Control

Uploaded by

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

Module 3 - Program Flow Control

Uploaded by

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

CSIT 121 – Computer

Programming

Module 2 – Program
Flow Control
Jasmin Tumulak Estudillo, MSIT
Lecturer
Conditional
s

2
Boolean Value
◉ A Boolean value is either true or false.
◉ In Python, the two Boolean values are True and False (the
capitalization must be exactly as shown), and the Python
type is bool.

3
Boolean expression
◉ A Boolean
expression is an
expression that
evaluates to
produce a result
which is a Boolean
value.

4
Logical operators
◉ There are three logical operators, and, or, and not
◉ Example:
◉ X=5
◉ x > 0 and x < 10 = True

5
Truth Table

6
Conditional Execution
◉ If-else syntax

7
Conditional Execution
◉ If statement syntax:
if Boolean
expression:
statement
if true

8
Chained Conditional
◉ If-elif statement syntax

9
Chained Conditional
◉ Example Grade Range Letter Grade
Write a Python program that 100 – 95 A
accepts the student’s grade points
90-94 B
and displays the corresponding
letter grade. 80-89 C

70-79 D

60-69 E

Below 60 F

10
Nested Conditionals
◉ Nested If statement syntax

11
Logical opposites
◉ Each of the six relational
operators has a logical
opposite.

12
Logical opposites

13
Short Hand

This technique is known as Ternary Operators, or Conditional Expressions.

14
Iterations

15
Short Hand

16
Python Loops
◉ Python has two primitive loop commands:
a. while loops
b. for loops

17
The while loop

18
break statement
◉ With the break statement
we can stop the loop even
if the while condition is
true.

19
continue statement
◉ With the continue
statement we can stop the
current iteration, and
continue with the next:

20
for loop
◉ A for loop is used for
iterating over a sequence
(that is either a list, a
tuple, a dictionary, a set,
or a string).

21
The range() Function
◉ To loop through a set of code a
specified number of times, we
can use the range() function,
◉ The range() function returns a
sequence of numbers, starting
from 0 by default, and increments
by 1 (by default), and ends at a
specified number.

22
The range() Function
◉ To loop through a set of code a
specified number of times, we
can use the range() function,
◉ The range() function returns a
sequence of numbers, starting
from 0 by default, and increments
by 1 (by default), and ends at a
specified number.

Note that range(6) is not the values


of 0 to 6, but the values 0 to 5.

23
Using the start parameter
◉ The range() function
defaults to 0 as a starting
value, however it is
possible to specify the
starting value by adding a
parameter: range(2, 6),
which means values from
2 to 6 (but not including
6):

24
Using the increment
parameter
◉ The range() function
defaults to increment the
sequence by 1, however it
is possible to specify the
increment value by adding
a third parameter:
range(2, 30, 3):

25
Else in For Loop
◉ The else keyword in a for
loop specifies a block of
code to be executed when
the loop is finished:

26
References

◉ How to Think Like a Computer Scientist — How to Think Like a


Computer Scientist: Learning with Python 3. Openbookproject.net.
(2012). Retrieved 5 April 2022, from
http://openbookproject.net/thinkcs/python/english3e/index.html.

◉ Python Tutorial. W3schools.com. (2022). Retrieved 6 April 2022,


from https://www.w3schools.com/python/

27

You might also like