Lab 3 2 Select Iteration Struct
Lab 3 2 Select Iteration Struct
Technology II
Laboratory No 3 Part II
Selection & Iteration Structures
SELECTION STRUCTURES REVIEW
allow programs to follow different courses of
action or execute different tasks depending
on the data values
< less than
> greater than
RELATIONAL
== equal to
OPERATORS
<= less than or equal to
>= greater than or equal to
!= not equal to
IF STATEMENTS REVIEW
if expression:
statement(s)
statement(s)
IF STATEMENTS
Example:
a = 1
if a==1:
print(”a equals to 1”);
x = 41
if x > 10:
print("Above ten,")
if x > 20:
print("and also above 20!")
else:
print("but not above 20.")
IF – ELIF - STATEMENTS
if b==0:
print("b is 0")
elif b==1:
print("b is 1")
elif b=>2:
print("b is greater than 2")
else:
print("b value is negative")
AND LOGICAL OPERATOR
The and keyword is a logical operator, and is
used to combine conditional statements:
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
OR LOGICAL OPERATOR
The or keyword is a logical operator, and is
used to combine conditional statements:
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
THE PASS STATEMENT
if statements cannot be empty, but if you for some reason
have an if statement with no content, put in the pass
statement to avoid getting an error.
a = 33
b = 200
if b > a:
pass
LOOP (ITERATION) STATEMENTS
• for
Syntax:
while booleanExpression:
statement(s)
Examples of a WHILE statement
i = 0
while i < 3:
print(i)
i+=1
Examples of a WHILE statement
while True:
statement
statement
The BREAK statement
i = 1
while i < 6:
print(i)
if i == 3:
break
i += 1
The CONTINUE statement
i = 0
while i < 6:
i += 1
if i == 3:
continue
print(i)
The ELSE statement
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
FOR STATEMENT
The for statement is Python’s multipurpose loop controller. It
is used for repeating code a known number of times.
This is less like the for keyword in other programming
languages, and works more like an foreach iterator
method as found in other object-orientated programming
languages.
Syntax:
for variable in range:
statement(s)
Example of a FOR loop in string
Strings are iterable objects, they contain a
sequence of characters:
for x in "banana":
print(x)
Output:
Examples of a FOR statement
BREAK
With the break statement we can stop the loop
before it has looped through all the items.
for x in range(6):
print(x)
Examples of a FOR statement
RANGE function
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).
for x in "banana":
if x == "a": print(x)
for x in range(6):
print(x)
else:
print("Finally finished!")
Nested FOR statement
A nested loop is a loop inside a loop.
The "inner loop" will be executed one time for each
iteration of the "outer loop".
Example: Print each adjective for every fruit.
for x in adj:
for y in fruits:
print(x, y)
PASS statement in FOR statement
for loops cannot be empty, but if you for some
reason have a for loop with no content, put in
the pass statement to avoid getting an error.]
Example:
i = 5
while i < 15:
print(i)
i = i+3
ORAL
EXERCISE
SHORT REVIEW
to be written on a board
ORAL
EXERCISE
SHORT REVIEW
to be written on a board
i = 0
while (i <= 20):
print(i, ’, ’)
i = i*i + 1
ORAL
EXERCISE
SHORT REVIEW
to be written on a board
ORAL
EXERCISE
QUESTIONS
ORAL
EXERCISE
QUESTIONS
i = 0
while (i < 2)
print(i)
i=i+1
ORAL
EXERCISE
QUESTIONS
for i in range(0,5):
print(i & ’;’)
ORAL
EXERCISE
PROBLEM 1
to be executed in Spyder environment and to
be presented to the teacher in the classroom
BOARD
EXERCISE
PROBLEM 2
to be executed in Spyder environment and to
be presented to the teacher in the classroom
PROGRAMMING
EXERCISE