Practice Questions — Loop Control - break, continue, pass
← Back to NotesTopic-Specific Questions
Question 1
Easy
What is the output of the following code?
for i in range(1, 6):
if i == 3:
break
print(i)The loop stops entirely when i reaches 3.
12Question 2
Easy
What is the output?
for i in range(1, 6):
if i == 3:
continue
print(i)continue skips only the current iteration, not the entire loop.
1245Question 3
Easy
What is the output?
for i in range(3):
pass
print("Done")pass does nothing. The loop runs but produces no output.
DoneQuestion 4
Easy
What is the output?
i = 10
while i > 0:
i -= 3
if i == 4:
break
print(i)Trace i: 10 -> 7 -> 4. What happens when i is 4?
4Question 5
Easy
What is the output?
for letter in "Hello":
if letter == "l":
continue
print(letter, end="")continue skips the current character. How many l's are in "Hello"?
HeoQuestion 6
Medium
What is the output?
for i in range(5):
if i == 3:
break
else:
print("Loop completed")
print("After loop")The else block runs only if the loop completes WITHOUT a break.
After loopQuestion 7
Medium
What is the output?
for i in range(5):
if i == 10:
break
else:
print("No break")
print("Done")Will i ever equal 10 in range(5)?
No breakDoneQuestion 8
Medium
What is the output?
for i in range(3):
for j in range(3):
if j == 1:
break
print(i, j)break exits only the inner loop. The outer loop continues.
0 01 02 0Question 9
Medium
What is the output?
for i in range(1, 6):
if i % 2 == 0:
continue
if i == 5:
break
print(i)Even numbers are skipped. The loop stops at 5.
13Question 10
Medium
What is the output?
count = 0
for i in range(10):
if i % 3 == 0:
continue
count += 1
print(count)continue skips multiples of 3. How many numbers from 0-9 are NOT multiples of 3?
6Question 11
Medium
What is the output?
while True:
print("Hello")
breakwhile True normally runs forever, but what does break do?
HelloQuestion 12
Hard
What is the output?
for i in range(3):
for j in range(3):
if i == j:
continue
print(i, j)continue skips when i equals j (the diagonal). All other pairs are printed.
0 10 21 01 22 02 1Question 13
Hard
What is the output?
for i in range(3):
for j in range(3):
if j == 2:
break
else:
print("Inner else for i =", i)The else belongs to the inner loop. Does the inner loop ever complete without break?
(No output - nothing is printed)
Question 14
Hard
What is the output?
n = 0
for i in range(1, 5):
for j in range(1, 5):
if i * j > 6:
break
n += 1
print(n)For each i, count how many j values give i*j <= 6 before break triggers.
10Question 15
Hard
What is the output?
for i in range(5):
if i < 2:
continue
if i > 3:
break
print(i, end=" ")continue skips 0 and 1. break stops at 4. What values remain?
2 3 Question 16
Medium
What is the difference between break and continue? Give a one-line example of each.
One stops the loop entirely, the other skips to the next iteration.
break exits the loop entirely; no more iterations run. continue skips the rest of the current iteration and moves to the next one. Example of break: in a loop searching for a name, if name == target: break stops searching once found. Example of continue: in a loop processing scores, if score < 0: continue skips invalid scores.Question 17
Hard
Explain the loop-else pattern. When does the else block run, and when is it skipped? Why is it useful?
Think about what happens when a search succeeds versus when it fails.
The else block on a loop runs only when the loop completes all iterations without encountering a
break. If break is executed, else is skipped. It is useful for search operations: use break when the target is found, and else handles the 'not found' case. This is cleaner than using a flag variable like found = False.Mixed & Application Questions
Question 1
Easy
What is the output?
for i in range(5):
if i == 0:
continue
print(i, end=" ")continue skips i = 0. All other values are printed.
1 2 3 4 Question 2
Easy
What is the output?
x = 1
while x <= 10:
if x == 6:
break
x += 1
print(x)Trace x: it increments until reaching 6, then break stops the loop.
6Question 3
Medium
What is the output?
text = "Python"
result = ""
for ch in text:
if ch in "aeiou":
continue
result += ch
print(result)continue skips vowels. Consonants are added to result.
PythnQuestion 4
Medium
What is the output?
nums = [2, 4, 6, 7, 8, 10]
for num in nums:
if num % 2 != 0:
print("Found odd:", num)
break
else:
print("All even")The loop looks for an odd number. Is there one in the list?
Found odd: 7Question 5
Medium
What is the output?
for i in range(4):
for j in range(4):
if j > i:
break
print("*", end="")
print()When j exceeds i, the inner loop breaks. How many stars per row?
**********Question 6
Medium
What is the output?
total = 0
i = 1
while i <= 10:
if i % 5 == 0:
i += 1
continue
total += i
i += 1
print(total)Multiples of 5 (5 and 10) are skipped. Sum the rest from 1 to 10.
40Question 7
Hard
What is the output?
for i in range(1, 4):
for j in range(1, 4):
if i + j == 4:
break
print(i, j)
else:
print("Row", i, "complete")The else belongs to the inner for loop. It runs only when the inner loop does not break.
1 11 22 1Question 8
Hard
What is the output?
for i in range(5):
if i == 2:
pass
if i == 3:
continue
if i == 4:
break
print(i)pass does nothing (i=2 still prints). continue skips i=3. break stops at i=4.
012Question 9
Hard
What is the output?
found = False
for i in range(2, 5):
for j in range(2, 5):
if i * j == 12:
found = True
break
if found:
break
print(i, j)This uses a flag to break out of both loops. When does i * j first equal 12?
3 4Question 10
Hard
What is the output?
for i in range(5):
for j in range(5):
if (i + j) % 2 == 0:
continue
if j == 3:
break
print(i, j, end=" ")
print()Even sums of i+j are skipped. If j reaches 3, the inner loop breaks.
0 1 1 0 1 2 2 1 3 0 3 2 4 1 Question 11
Hard
What is the output?
i = 0
while i < 10:
i += 1
if i % 2 == 0:
continue
if i > 7:
break
print(i, end=" ")Even numbers skip. Numbers above 7 trigger break. What odd numbers from 1-7 are printed?
1 3 5 7 Question 12
Medium
Why is pass necessary in Python? What happens if you write an empty loop without pass?
Python uses indentation for blocks. What happens when a block has no statements?
Python requires every code block (after
if, for, while, def, etc.) to have at least one statement. An empty block causes an IndentationError or SyntaxError. pass serves as a no-operation placeholder that satisfies this requirement. Example: for i in range(5): followed by nothing is a syntax error, but for i in range(5): pass is valid.Multiple Choice Questions
MCQ 1
What does the break statement do in a loop?
Answer: B
B is correct.
B is correct.
break immediately exits the loop. Option A describes continue. Options C and D do not exist as loop control behaviors in Python.MCQ 2
What does the continue statement do?
Answer: B
B is correct.
B is correct.
continue skips the rest of the current iteration's body and moves to the next iteration. Option A describes break. Option C describes pass. Option D is not what continue does.MCQ 3
What does pass do in Python?
Answer: C
C is correct.
C is correct.
pass is a null operation. It does nothing when executed. It is used as a placeholder when a statement is required syntactically but no action is needed.MCQ 4
Can break be used outside a loop?
Answer: C
C is correct.
C is correct.
break can only be used inside a for or while loop. Using it outside a loop raises SyntaxError: 'break' outside loop.MCQ 5
In nested loops, which loop does break exit?
Answer: C
C is correct.
C is correct.
break only exits the loop it is directly inside (the innermost loop containing the break). The outer loop continues its iterations. To exit multiple loops, use a flag variable.MCQ 6
When does the else clause of a for loop execute?
Answer: D
D is correct. The loop-else runs only when the loop finishes all iterations without a
D is correct. The loop-else runs only when the loop finishes all iterations without a
break. If break is executed (option C), the else is skipped. It is not triggered by errors (option B) and does not run on each iteration (option A).MCQ 7
What is the output of: for i in range(3): continue; print(i)?
Answer: B
B is correct. On every iteration,
B is correct. On every iteration,
continue executes, which skips print(i). No values are ever printed. The loop runs 3 times but produces no output because continue always skips the print. Note: using semicolons in Python is valid but not recommended.MCQ 8
What is the main danger of using continue in a while loop?
Answer: B
B is correct. If the counter update (like
B is correct. If the counter update (like
i += 1) is placed after the continue statement in a while loop, continue will skip it, and the loop variable never changes, causing an infinite loop. Always update the counter before continue.MCQ 9
Which statement is a valid placeholder for an empty code block?
Answer: C
C is correct.
C is correct.
pass is designed to be a no-operation placeholder. break (option A) exits a loop. continue (option B) skips an iteration. skip (option D) is not a Python keyword.MCQ 10
What happens if a for loop with an else clause iterates over an empty sequence?
Answer: B
B is correct. If the sequence is empty (like
B is correct. If the sequence is empty (like
for i in range(0)), the loop body never runs, and since no break was encountered, the else block runs. This catches many students off guard.MCQ 11
What is the output?
for i in range(3):
pass
print(i, end=" ")Answer: B
B is correct.
B is correct.
pass does nothing and execution continues to the next statement, which is print(i). So 0, 1, 2 are printed. pass does NOT skip the rest of the loop body like continue does.MCQ 12
How would you break out of two nested loops?
Answer: C
C is correct. Python does not support
C is correct. Python does not support
break 2 (option B) or multiple breaks on one line (option A). The standard approach is to set a flag (like found = True) before breaking the inner loop, then check the flag to break the outer loop. exit() (option D) terminates the entire program, not just the loops.MCQ 13
What is the output?
x = 0
for i in range(1, 5):
if i == 3:
break
x += i
else:
x += 100
print(x)Answer: C
C is correct. The loop adds i=1 (x=1) and i=2 (x=3), then breaks at i=3. Since break was executed, the else block (x += 100) is skipped. Final x = 3.
C is correct. The loop adds i=1 (x=1) and i=2 (x=3), then breaks at i=3. Since break was executed, the else block (x += 100) is skipped. Final x = 3.
MCQ 14
What is the output?
for i in range(5):
if i == 2:
continue
for j in range(5):
if j == 2:
break
print(i, j)Answer: A
A is correct. The outer continue skips i=2 entirely. For i=0,1,3,4: the inner loop runs and breaks at j=2. After the inner loop, print(i, j) runs. So 4 lines are printed: (0,2), (1,2), (3,2), (4,2). The inner break does not affect the outer loop.
A is correct. The outer continue skips i=2 entirely. For i=0,1,3,4: the inner loop runs and breaks at j=2. After the inner loop, print(i, j) runs. So 4 lines are printed: (0,2), (1,2), (3,2), (4,2). The inner break does not affect the outer loop.
MCQ 15
Which is NOT a valid use of pass?
Answer: D
D is correct (it is NOT a valid use).
D is correct (it is NOT a valid use).
pass does NOT exit a loop; it does nothing. If you want to exit a loop, use break. Options A, B, and C are all valid uses of pass as a placeholder for empty code blocks.MCQ 16
What is the output of: for i in range(3): break; print("After loop")?
Answer: B
B is correct. The break executes on the very first iteration (i=0), immediately exiting the loop. No values of i are printed. Then "After loop" is printed because it is outside the loop. Note: the semicolon puts two statements on one line.
B is correct. The break executes on the very first iteration (i=0), immediately exiting the loop. No values of i are printed. Then "After loop" is printed because it is outside the loop. Note: the semicolon puts two statements on one line.
MCQ 17
What is the output?
while False:
print("Inside")
else:
print("Else")Answer: B
B is correct. The while condition is False from the start, so the loop body never runs. However, since no break was encountered (the loop simply did not run), the else block executes. This is a subtle but important behavior.
B is correct. The while condition is False from the start, so the loop body never runs. However, since no break was encountered (the loop simply did not run), the else block executes. This is a subtle but important behavior.
MCQ 18
In this code, how many times does 'Hello' print?
for i in range(10):
if i % 3 == 0:
continue
if i == 8:
break
print("Hello")Answer: B
B is correct. i=0: skip (0%3==0). i=1: print. i=2: print. i=3: skip (3%3==0). i=4: print. i=5: print. i=6: skip (6%3==0). i=7: print. i=8: break. Total prints: i=1,2,4,5,7 = 5 times.
B is correct. i=0: skip (0%3==0). i=1: print. i=2: print. i=3: skip (3%3==0). i=4: print. i=5: print. i=6: skip (6%3==0). i=7: print. i=8: break. Total prints: i=1,2,4,5,7 = 5 times.
Coding Challenges
Challenge 1: First Negative Number
EasyGiven the list [5, 12, 8, -3, 9, -7, 4], write a program that finds and prints the first negative number. Stop searching after finding it.
Sample Input
(No input required)
Sample Output
First negative number: -3
Use a for loop with break.
numbers = [5, 12, 8, -3, 9, -7, 4]
for num in numbers:
if num < 0:
print("First negative number:", num)
breakChallenge 2: Print Without Vowels
EasyGiven the string "Ananya loves coding", write a program that prints the string with all vowels removed.
Sample Input
(No input required)
Sample Output
nny lvs cdng
Use a for loop with continue to skip vowels.
text = "Ananya loves coding"
for ch in text:
if ch.lower() in "aeiou":
continue
print(ch, end="")
print()Challenge 3: Sum Until Negative
MediumGiven the list [10, 25, 30, -5, 40, 50], write a program that calculates the sum of elements until a negative number is encountered. Stop at the negative number (do not include it).
Sample Input
(No input required)
Sample Output
Sum before negative: 65
Use a for loop with break.
numbers = [10, 25, 30, -5, 40, 50]
total = 0
for num in numbers:
if num < 0:
break
total += num
print("Sum before negative:", total)Challenge 4: Password Checker Simulation
MediumSimulate a password checker. The correct password is "mac2026". Use a while loop to allow up to 3 attempts. If the correct password is found in the attempts list ["hello", "mac2026", "python"], print "Access granted" and stop. If all 3 fail, print "Account locked".
Sample Input
(No input required)
Sample Output
Attempt 1: hello - Wrong!
Attempt 2: mac2026 - Access granted!
Use a while loop with break. Use the loop-else pattern.
attempts = ["hello", "mac2026", "python"]
correct = "mac2026"
i = 0
while i < len(attempts):
guess = attempts[i]
i += 1
if guess == correct:
print("Attempt " + str(i) + ": " + guess + " - Access granted!")
break
else:
print("Attempt " + str(i) + ": " + guess + " - Wrong!")
else:
print("Account locked")Challenge 5: Skip Multiples
MediumWrite a program that prints numbers from 1 to 30, but skips all multiples of 3 and all multiples of 5. Use continue for skipping.
Sample Input
(No input required)
Sample Output
1 2 4 7 8 11 13 14 16 17 19 22 23 26 28 29
Use a single for loop with continue.
for i in range(1, 31):
if i % 3 == 0 or i % 5 == 0:
continue
print(i, end=" ")
print()Challenge 6: Find Common Element
HardGiven two lists list1 = [3, 7, 12, 9, 5] and list2 = [8, 5, 14, 3, 20], find and print the first element that appears in both lists. Use nested loops with break.
Sample Input
(No input required)
Sample Output
First common element: 3
Use nested for loops with break. Use a flag variable to exit both loops.
list1 = [3, 7, 12, 9, 5]
list2 = [8, 5, 14, 3, 20]
found = False
for a in list1:
for b in list2:
if a == b:
print("First common element:", a)
found = True
break
if found:
breakChallenge 7: Count Digits, Letters, and Spaces
HardGiven the string "Aarav is 14 years old and in class 9", count and print the number of digits, letters, and spaces. Use continue to skip characters that are not in any category.
Sample Input
(No input required)
Sample Output
Digits: 3
Letters: 26
Spaces: 8
Use a for loop. Use str.isdigit(), str.isalpha(), and comparison with ' '.
text = "Aarav is 14 years old and in class 9"
digits = 0
letters = 0
spaces = 0
for ch in text:
if ch.isdigit():
digits += 1
elif ch.isalpha():
letters += 1
elif ch == ' ':
spaces += 1
else:
continue
print("Digits:", digits)
print("Letters:", letters)
print("Spaces:", spaces)Challenge 8: Prime Numbers in Range
HardWrite a program that prints all prime numbers between 2 and 50. Use nested loops with break and the loop-else pattern to check each number for primality.
Sample Input
(No input required)
Sample Output
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Use a for loop inside a for loop. Use break when a divisor is found and else to print primes.
for num in range(2, 51):
for i in range(2, num):
if num % i == 0:
break
else:
print(num, end=" ")
print()Need to Review the Concepts?
Go back to the detailed notes for this chapter.
Read Chapter NotesWant to learn Python with a live mentor?
Explore our Python course