Computer Science · Ch 6 — Flow of Control
Break Statement
Break Statement
The break statement alters the normal flow of execution: it terminates the current loop immediately and resumes execution at the statement following that loop. The rest of the loop body for the current iteration is abandoned, and no further iterations occur — regardless of what the loop's condition says. The flowchart for break (printed as Figure 6.5 in the book) shows the moment break is encountered, control jumps straight out of the loop body to the statement after the loop.
break in a for loop
# Demonstrate break inside a for loop
n = 0
for n in range(10):
n = n + 1
if n == 8:
break
print('Num has value ' + str(n))
print('Encountered break!! Out of loop')
Num has value 1
Num has value 2
Num has value 3
Num has value 4
Num has value 5
Num has value 6
Num has value 7
Encountered break!! Out of loop
Although range(10) could drive ten iterations, the loop stops the moment the value reaches 8: the break executes, the print inside the loop is skipped for that pass, and control lands on the final print after the loop.
break as a deliberate exit from while True
break is the natural way out of a loop whose condition never turns false on its own. Here a while True loop keeps accepting numbers and accumulating their sum; the first negative entry ends the input:
# Sum positive numbers; a negative number ends the loop
value = 0
total = 0
print("Enter numbers to find their sum, negative number ends the loop:")
while True:
value = int(input()) # int() typecasts the input string to integer
if value < 0:
break
total += value
print("Sum =", total)
Enter numbers to find their sum, negative number ends the loop:
3
4
5
-1
Sum = 12
The negative number itself is not added — break fires before the accumulation line.
break to stop searching early — prime check
When testing whether a number is prime, the first divisor found already settles the answer, so there is no need to check any further. A flag variable records what was discovered:
# Check whether a given number is prime
num = int(input("Enter the number to be checked: "))
found = 0 # presume num is a prime number
if num > 1:
for d in range(2, int(num / 2)):
if num % d == 0:
found = 1 # num is not a prime number
break # no need to check any further
if found == 1:
print(num, "is not a prime number")
else:
print(num, "is a prime number")
else:
print("Entered number is <= 1, execute again!")
Sample runs:
Enter the number to be checked: 20
20 is not a prime number
Enter the number to be checked: 19
19 is a prime number
Enter the number to be checked: 2 …
Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your textbook's own diagram.
This flowchart shows what happens when a break statement sits inside the body of a loop. (It is printed as "Figure 6.5" in the book — a duplicate of the while-loop figure's number; an NCERT misprint.)
The overall shape is a normal loop with one extra escape route:
- An oval terminator "Start" at the top leads down into a diamond labelled "Loop condition" — the loop's usual test.
- If the condition is False, an arrow leaves the diamond to the left and turns down into a rectangle "Statement following the loop", then continues down to the terminator "Stop". This is the loop's normal ending.
- If the condition is True, the flow drops into the loop's body. The body is drawn as a dashed rectangle labelled "Body of the loop" at its top-right corner, enclosing three elements in sequence:
- a rectangle "Loop statements" — the part of the body before the break check;
- a diamond "If break statement is encountered" — the decision that models reaching a
break; - a rectangle "More loop statements" — the rest of the body.
- From the break diamond, the True exit goes left and joins the arrow entering "Statement following the loop". This is the whole meaning of
break: the current loop terminates on the spot, the remaining body statements ("More loop statements") are never reached, and execution resumes at the statement immediately after the loop — exactly as if the loop condition had turned false. - The False exit of the break diamond continues down into "More loop statements", whose outgoing arrow runs out to the right and up the right-hand side back into the "Loop condition" diamond — the normal loop-back for the next iteration. …