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

Control Structures

Uploaded by

Catherine Benban
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Control Structures

Uploaded by

Catherine Benban
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Programming Essential Course

Types of Control Structures


Selection
Selection Structure (Decision or Condition)

● Provides a choice between two or more


alternative paths depending on the result of a
condition
A
Python Conditions and if statements a = 25
if a == 25:
Relational Operators:
• Equal to a == b
print(“a is equal to 25”)
• Not Equal a != b B
• Less than a<b
• Less than or equal to a <= b a = 25
• Greater than a>b if a != 5:
• Greater than or equal to a >= b print(“a is not equal to 5”)
E C
a=5 a=2
if a <= 5: if a < 5:
print(“a is less than or equal to 5”) print(“a is less than 5”)
F D

a=7 a=5
if a >= 5: if a > 4:
print(“a is greater than or equal to 5”) print(“a is greater than 4”)
Simple if-else statement

A B
number = 4 number = -2
if number < 0: if number < 0:
print(“Negative”) print(“Negative”)
else: else:
print(“Positive”) print(“Positive”)
Output: Positive Output: Negative
C D
number = 5 number = 4
if number%2== 0: if number%2 == 0:
print(“Even”) print(“Even”)
else: else:
print(“Odd”) print(“Odd”)
Output: Odd Output:
Sample Problem
Sample Runs
Write a program that will compute for the student’s
Enter Math: 96
average grade and will display the Status of the student.
● The user will input the following: Enter Science: 90
○ Math (float) Enter English: 89
○ Science (float) Average Grade: 91.67
○ English (float) Congratulations!
● Display the average grade in 2 decimal places You passed the semester.”
● if the average grade >= 75, display
Congratulations! Enter Math: 75
You passed the semester Enter Science: 74
else display Enter English: 70
You failed the semester Average Grade: 73
You failed the semester.”
Ladderized if statement
Output: Freezing Point
temp = 0
if temp > 100:
print(“Steam”)
elif temp == 100:
print(“Boiling Point”)
elif temp >= 1:
print(“Water”)
elif temp == 0:
print(“Freezing Point”)
elif temp < 0:
print(“Ice”)
Exercise
Create a program using ladderized-if statement to display student’s
remarks based on the average grade.
Average Grade Remarks Sample Runs:
Enter average grade: 96
95-100 Excellent Excellent
-----------------------------------
90-94 Outstanding Enter average grade: 90
85-89 Very Good Outstanding
-----------------------------------
80-84 Good Enter average grade: 73
75-79 Poor Failed
-----------------------------------
Below 75 (0-74) Failed Enter average grade: 101
Grade out of range
-----------------------------------
Enter average grade: -1
Grade out of range
Nested if statement

A age = 59
if age >= 1:
if age >=150:
print(“Age out of range”)
elif age >=60:
print(“Senior Citizen”)
else:
print(“Not a Senior Citizen”)
else:
print(“Age out of range”)
Compound if statement
grade = 84
Compound Condition
if grade >= 95 and grade <=100:
● Have more than 1 conditional
expression print(“Excellent”)
● The result of the compound elif grade >= 90 and grade <= 94:
expression depends on print(“Outstanding”)
the individual result of
elif grade >= 85 and grade <= 89:
each condition
print(“Very Good”)
Truth table of logical operator and elif grade >= 80 and grade <= 84:
print(“Good”)
Condition 1 Condition 2 Result
elif grade >=75 and grade <= 79:
false false false
print(“Poor”)
false true false
elif grade < 75:
true false false
true true true print(“Failed”)
Compound if statement
Compound Condition
color = ‘Y’
● Have more than 1 conditional
expression if color == ‘y’ or color == ‘Y’:
● The result of the compound print(“Yellow”)
expression depends on elif color == ‘b’ or color ==‘B’:
the individual result of
print(“Blue”)
each condition
elif color == ‘r’ or color ==‘R’:
print(“Red”)
Truth table of logical operator or
else:
Condition 1 Condition 2 Result
print(“Invalid Color”)
false false false
false true true
true false true
true true true
Sample Problem
Write a program that will compute for the student’s average grade and will display the Status of the student.
● The user will input the following:
○ Math (float)
○ Science (float)
○ English (float)
● Display the average grade in 2 decimal places
● if the average grade >= 75 and all the subject grades >= 75, display
Congratulations!
You passed the semester
● if the average grade >= 75 but if there is/are one/two subject grade(s) below 75, display
Congratulations!
You passed the semester,
but you need to retake the following subject(s):
-list of failed subject(s)
● if the average grade < 75, display
“You failed the semester”
Sample Run: Sample Run: Sample Run:
Enter Math: 96 Enter Math: 84 Enter Math: 84

Enter Science: 90 Enter Science: 74 Enter Science: 74

Enter English: 89 Enter English: 80 Enter English: 72

Average Grade: 91.67 Average Grade: 79.33 Average Grade: 76.67

Congratulations! Congratulations! Congratulations!

You passed the semester You passed the semester, You passed the semester,

but you need to retake the but you need to retake the
following subject(s): following subject(s):

- Science” - Science

- English”
Sample Run:
Enter Math: 74

Enter Science: 74

Enter English: 74

Average Grade: 74

You failed the semester


Exercise
Write a program that will check the employees’ years in Sample Run:
service and office.
Enter years in service: 15

● The user will input number of years in service and the Enter office: IT

following offices: IT, ACCT, HR. Amount given: 10 000


● Check the following conditions given on the table.
Sample Run:
● The program should display the amount
Enter years in service: 8
Office Years in Service
More than or equal to 10 years Below 10 years Enter office: HR

IT 10 000 5 000 Amount given: 7 500

ACCT 12 000 6 000


HR 15 000 7 500
Problem:
Write a program that accepts three Sample Run
integers. The program should Enter integer1: 5
determine the highest and the Enter integer2: 7
lowest integers. Enter integer3: 3
The program should display the sum Lowest: 3
Highest: 7
of the lowest and highest integers on Sum: 10
the screen. You can use any selection
control structures.
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
isupper() - Returns True if all characters in the isdigit() - Returns True if all characters in the
string are upper case string are digits

txt = "HELLO" txt = “12"

if txt.isupper(): if txt.isdigit():
print("Uppercase") print(Digit")
else: else:
print("Not uppercase") print(“String")

isalpha() - Returns True if all characters in the


islower() - Returns True if all characters in the
string are in the alphabet
string are lower case

txt = "HELLO" txt = “university"

if txt.islower(): if txt.isalpha():
print(“Lowercase") print(“In the alphabet")
else: else:
print("Not lowercase") print(“Not in the alphabet")

You might also like