This document discusses for loops in Python and provides examples of using if statements, else clauses, and nested loops within for loops. It also discusses break and continue statements and how they can alter normal loop flow. Specifically, it shows that an else block after a for loop will execute if there is no break statement in the loop. It provides code examples of for loops with if/break statements and else blocks to illustrate when the else block does and does not execute. It also shows examples of nested for loops and using break/continue statements to terminate or skip iterations in a loop.
08448380779 Call Girls In Civil Lines Women Seeking Men
6 Iterative Statements.pptx
1. If statement within a for loop
• Inside a for loop, you can use if statements as
well.
2. Python code to illustrate for statement with else clause
# Here, else block is gets executed because break statement does
not executed
for item in 'Python':
if item == 'x':
break
print(item)
else:
print('else block says for is successfully completed!')
print('Job is done!!')
for statement with 'else' clause in Python
3. # Here, else block does not gets executed because
break statement terminates the loop.
for item in 'Python':
if item == 'y':
break
print(item)
else:
print('else block says for is successfully completed!')
print('Job is done!!')
Python code to illustrate for statement with else
clause
4. # Python program to demonstrate
# for-else loop
for i in range(1, 4):
print(i)
else: # Executed because no break in for
print("No Breakn")
for i in range(1, 4):
print(i)
break
else: # Not executed as there is a break
print("No Break")
5. digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")
6. Python programming language allows to use one loop
inside another loop.
Following section shows few examples to illustrate the
concept.
Syntax:
for iterator_var in sequence:
for iterator_var in sequence:
statements(s)
statements(s)
Nested Loops:
7. In Python, break and continue statements can alter the flow of
a normal loop.
Loops iterate over a block of code until the test expression is
false,but sometimes we wish to terminate the current iteration or
even the whole loop without checking test expression.
The break and continue statements are used in these cases.
What is the use of break and continue in Python?