L-8 Loops in Python - Copy
L-8 Loops in Python - Copy
Statements
Ans- Sometimes, there is a situation when the control of the program needs to be transferred out of the
loop body,even if all the values of the iterations of the loop have not been completed.For this
purpose,jumping statements are used in Python.
Ans- 1.The for statement is used to repeat an instruction or a set of instructions a fixed number of times
whereas, the while statement is used to repeat a set of instructions until a condition evaluates to
true.When the condition becomes false, the control comes out of the loop.
2. Syntax:
For loop
for<variable> in <iterator>:
Statements
While loop
While(test expression):
Statements
for Loop
for a in range(1,6):
print(a)
While loop
i=1
whilei<=5:
print(i)
i=i+1
else:
Ans- 1. The break is a keyword in Python which is used for bringing the program control out of the
loop.The break statement halts the execution of a loop and program flow switches to the statement
after the loop, whereas incontinue statement, control of the program jumps to the beginning of the loop
for next iteration, skipping the execution of rest of the statement of the loop for the current iteration.
# loop statement
break
# loop statements
continue
if number == 7:
break
print(‘Out of loop’)
Output
Number is: 0
Number is: 1
Number is: 2
Number is: 3
Number is: 4
Number is: 5
Number is: 6
Out of loop
if number == 7:
continue
Output-
Number is: 0
Number is: 1
Number is: 2
Number is: 3
Number is: 4
Number is: 5
Number is: 6
Number is: 8
Number is: 9
Number is: 10
Number is: 11
Out of loop