Q1. What are the loop control statements in Python?
Python has break, continue, and pass. break terminates the loop entirely. continue skips the rest of the current iteration and moves to the next. pass does nothing and is a placeholder. Example:
for i in range(10):
if i == 5: break
if i % 2 == 0: continue
print(i)Q2. How does break differ from continue?
break exits the loop entirely, and no further iterations are executed. continue jumps to the next iteration, skipping any code after it in the current iteration. Both are used to control loop flow based on conditions.
Q3. What is the else clause in loops?
Loops can have an else block that executes when the loop completes normally (without break). Example:
for n in range(2, 10):
for x in range(2, n):
if n % x == 0: break
else:
print(n, "is prime")Q4. Can you nest loops in Python?
Yes, loops can be nested. Control statements affect only the innermost loop they are in. Example:
for i in range(3):
for j in range(2):
print(i,j)Q5. What is the difference between for and while loop?
for is used for iterating over a sequence (fixed number of iterations) or when you know the range. while is used when the number of iterations is unknown and depends on a condition. For loops are generally safer for iterating over collections.
