0% found this document useful (0 votes)
22 views19 pages

Day 3

The document provides information on control structures in programming, specifically selection structures. It discusses three components of selection structures: (1) a condition to be tested, (2) a statement to be performed if the condition is satisfied, and (3) a statement to be performed if the condition is not satisfied. Examples of selection structures include single alternative selection using an if statement and multiple true selection using if, elif, and else statements. The document also covers compound conditions that have more than one conditional expression connected by and or or.

Uploaded by

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

Day 3

The document provides information on control structures in programming, specifically selection structures. It discusses three components of selection structures: (1) a condition to be tested, (2) a statement to be performed if the condition is satisfied, and (3) a statement to be performed if the condition is not satisfied. Examples of selection structures include single alternative selection using an if statement and multiple true selection using if, elif, and else statements. The document also covers compound conditions that have more than one conditional expression connected by and or or.

Uploaded by

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

TOT – Python

Programming
Essentials
DAY 3 – Control
Structures (Selection)
Topics
01 Control Structures
02 Type Structures
03 Sequence
04 Selection
05 Conditional Operators
06 Compound Conditions
Control Structures

A control structure (or flow of control) is a block of programming that


analyses variables and chooses a direction in which to go based on given
parameters.

The basic decision-making process in programming and flow of control


determines how a computer program will respond when given certain
conditions and parameters.
Types of control structures
Sequential
Sequential execution is when statements are executed
one after another in order. You don't need to do anything
more for this to happen.

Selection
Selection used for decisions, branching – choosing
between 2 or more alternative paths.

Iteration
Repetition used for looping, i.e. repeating a
piece of code multiple times in a row.
Types of Structure {}
Sequence
Please enter three
numbers
The simplest structure

Find the sum of the Control flows from one statement to the next
three numbers

Statements are executed sequentially in the order in which


they appear in the program
Divide the sum by 3
to get the average

Entry Exit
Example: Sequence
A program that computes and prints the net pay of an employee
contains the following code:

grossPay = 20000 # 1st statement to be executed


netPay = grossPay – 4000 # 2nd statement to be executed
print(“The net pay is: “ + netPay); # 3rd statement to be executed
Selection
The selection statements are also known as decision making statements
or branching statements.

Provides a choice between two alternatives

3 Components of the structure


▪ A condition to be tested
▪ The statement to be performed if the condition is satisfied
(process A)
▪ The statement to be performed if the condition is not satisfied
(process B)
Selection
Entry to the structure is through the condition.

Exit is through the execution of process A or process B


Selection
Program code for single alternative selection:

if condition:
statement1

if condition:
statement1
statement2

where condition is a Boolean expression or Boolean variable


Selection
Program code for multiple true selection:

if condition:
statement1
elif condition:
statement2
else:
statement3
Conditional Operators
❑Equals: a == b
❑Not Equals: a != b
❑Less than: a < b
❑Less than or equal to: a <= b
❑Greater than: a > b
❑Greater than or equal to: a >= b
Some Common Errors
Using > instead of >=

Example: “Write a program that would give customers over age 60 a discount
of 20%”

❑ The phrase “over age 60” is not clear. Does it include the age 60 or not?
Clarify what it means.

❑ Similar phrases that have unclear meaning are: “not more than”, “at
least”, “not under”.
Day 3
Activity 1
● Write a program that will compute Name: Vinz
for the students average Math: 80
● The user will input the following: Science: 90
❑ Name English: 85
❑ Math Average: 85.0
❑ Science
❑ English POSSIBLE OUTPUT:
● If the average is equal and above Output 1:
75 status is Passed else You Congratulations! You passed
Failed the semester the semester. But you need
to re-enroll English subject

Output 2:
Sorry, You Failed the
semester

Day 3 Activity 1
Compound CoNditions
❑Have more than 1 conditional expression
❑The result of the compound expression depends on the
individual result of each condition

Format:
(condition1) and (condition2)
(condition1) or (condition2)
Day 3
Activity 2
● Write a program that will check the employees
years in service and office.
● The user will input number for years and in
service and the following offices (IT, ACCT, HR)
● Check the following conditions

Day 3 Activity 2
Built-in Methods
❑ isupper() - Returns True if all characters in the string
are upper case
❑ islower() - Returns True if all characters in the string
are lower case
❑ isdigit() - Returns True if all characters in the string
are digits
❑ isalpha() - Returns True if all characters in the string
are in the alphabet

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy