Control Structures
Control Structures
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
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
● The user will input number of years in service and the Enter office: IT
if txt.isupper(): if txt.isdigit():
print("Uppercase") print(Digit")
else: else:
print("Not uppercase") print(“String")
if txt.islower(): if txt.isalpha():
print(“Lowercase") print(“In the alphabet")
else: else:
print("Not lowercase") print(“Not in the alphabet")