You are on page 1of 26

Control Structures

LECTURE 2
Selection

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Python Comparison Operators
Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
If Statement
a = 4
b = 8 If only one statement will be
if b > a: executed, it can be put on the
print("b is greater than a") same line as the if statement.

a = 4
b = 8
indentation if b > a: print("b is greater than a")

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Elif
▪Python's way of saying “if the previous conditions
were not true, then try this condition”
a = 4
b = 8
if b > a:
print("b is greater than a")
elif b < a:
print("b is less than a")

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Else
▪catches anything which isn't caught by the
preceding conditions
a = 4
b = 8
if b > a:
print("b is greater than a")
elif b < a:
print("b is less than a")
else:
print("a and b are equal")

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
If…else
▪if only one statement will be executed each for if
and for else, it can all be put on the same line:
a = 2
print("a is positive") if a > 0 else print("a is not positive")

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
If…else
Ternary Operators or Conditional Expressions
having multiple else statements on the same line

a = 3
b = 5
print("A") if a > b else print("=") if a == b else print("B")

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
If…else
▪and/or are used to combine conditional
statements
a = 5 a = 1
b = 1 b = 5
c = 10
c = 10
if a > b or c > a:
if a > b and c > a: print("At least one of the conditions is True")
print("Both conditions are True")

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Nested If
▪if statements inside if statements
x = 25

if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
pass Statement
▪pass is used in case an if statement has no
content
a = 3
b = 5

if b > a:
pass

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Exercises
1. Write a program that prompts the user to input a number.
The program should then output the number and a message
stating whether the number is positive, negative, or zero.
2. Write a program that prompts the user to input an integer.
The program should then output the number and a message
stating whether the number is divisible by 2 or not.

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Exercises
3. In a right triangle, the square of the length of one side is equal
to the sum of the squares of the lengths of the other two sides.
Write a program that prompts the user to enter the lengths of
three sides of a triangle and then outputs a message indicating
whether the triangle is a right triangle.

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Exercises
4. A box of cookies can hold 24 cookies and a container can
hold 75 boxes of cookies. Write a program that prompts
Sample Run:
the user to enter the total number of cookies. The program
then outputs the number of boxes and the number of
containers to ship the cookies. Note that each box must Enter number of cookies: 3000
contain the specified number of cookies and each
No. of Cookies: 3000
container must contain the specified number of boxes. If
the last box of cookies contains less than the number of No. of Boxes: 125
specified cookies, you can discard it, and output the No. of Containers: 1
number of leftover cookies. Similarly, if the last container Leftover Cookies: 0
contains less than the number of specified boxes, you can
discard it, and output the number of leftover boxes. Leftover Boxes: 50

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Repetition

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Loop Commands
✓ while loop
✓ for loop

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
while Loop
▪a set of statements can be
executed as long as a condition
is true
▪syntax:
while condition:
# body of while loop

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
while Loop
▪Ex:
i = 0
while i < 5:
print(i)
i += 1

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
for Loop
▪used for iterating over a
sequence (that is either a list, a
tuple, a dictionary, a set, or a
string)
▪syntax:
for val in sequence:
# statement(s)

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
for Loop
▪Ex:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Loops
▪break Statement
◦ stops the loop even if the while
condition is true
i = 0 for i in
while i < 5: range(5):
print(i) if i == 3:
if i == 3: break
break print(i)
i += 1

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Loops
▪continue Statement
◦ stops the current iteration, and
continues with the next
i = 0 for i in
while i < 5: range(5):
i += 1 if i == 3:
if i == 3: continue
continue print(i)
print(i)

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Loops
▪else Statement
◦ runs a block of code once when the condition is no longer true
i = 1 digits = [0, 1, 5]
while i < 5:
print(i) for i in digits:
i += 1 print(i)
else: else:
print("i is no longer less than 5") print("No items left.")

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
print()
▪full syntax

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Exercises
1. Write a program that prints the first 10
counting numbers as shown:

1 2 3 4 5 … 10

ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova
Exercises
2. Write a program that prints the first 10
powers of 2.
3. Write a program that prompts user to
enter 10 integers and outputs the sum of
the integers.
ECE115.2: Object Oriented Programming for ECE Prepared by: Engr. Ria Marie P. Cordova

You might also like