If statement within a for loop
• Inside a for loop, you can use if statements as
well.
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
# 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
# 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")
digits = [0, 1, 5]
for i in digits:
print(i)
else:
print("No items left.")
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:
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?
Syntax of break
break
The working of break statement in for loop and while
loop is shown below.
# Use of break statement inside the loop
for val in "string":
if val == "i":
break
print(val)
print("The end")
Example: Python break
break and continue Statements
The break statement, like in C, breaks out of the innermost
enclosing for or while loop.

6 Iterative Statements.pptx

  • 1.
    If statement withina for loop • Inside a for loop, you can use if statements as well.
  • 2.
    Python code toillustrate 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, elseblock 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 programto 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 languageallows 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, breakand 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?
  • 8.
  • 10.
    The working ofbreak statement in for loop and while loop is shown below.
  • 11.
    # Use ofbreak statement inside the loop for val in "string": if val == "i": break print(val) print("The end") Example: Python break
  • 12.
    break and continueStatements The break statement, like in C, breaks out of the innermost enclosing for or while loop.