Example 1:
count = 0
while count < 10:
print(The count is, count)
count = count + 1
Nested loops
- They are loops within a loop.
Example 5:
2. For loop
- A loop that iterates over items in a sequence
whether a list or a string. It also must have an
initialization, condition, and increment.
for i in range(0,3):
for j in range(0,4):
print(j, end="")
General Format:
for variable in sequence:
block_of_code
A sequence can be defined by a range.
Using the range function:
The range function is used to iterate over
a sequence of numbers.
Example 2:
for i in range(0,10):
print(Counting from:, i)
print()
The end= parameter suppresses new lines. print() is the
same as \n.
Tracing example 5:
i
j
0
0
1
0
2
0
while True:
print(Hindi na ako aasa.)
i = 0
Example 8:
while i < 5:
Mixing conditional statements and loops
Conditional statements (if, if-else, nester if-else,
and if-elif-else) can be combined with loops (for or while)
to create a program that can solve a specific problem.
if i == 3:
i = i + 1
continue
Example 6:
print(i)
num = int(input("Enter range: "))
i = i + 1
for i in range(1, num):
if i % 2 == 0:
print(i,"is an even number")
else:
print(i,"is an odd number")