Page 154 - Information_Practice_Fliipbook_Class11
P. 154
Test False
condition
True
Enter Loop
Break
Exit Loop
Fig 6.4: Flowchart to explain the working of break statement
The break statement causes unconditional termination of a loop. Even if the test condition is True, or all values
in the range have not been traversed, yet, as the break statement is encountered, the execution of the loop stops.
Consider the code explaining the functioning of break statement in a for loop and while loop:
for x in range(1,5): num=5
print(x) while num>=0:
if x==3: print(num*num)
break if num%3==0:
print("Over") break
num=num-1
print("Done")
Output: Output:
1 25
2 16
3 9
Over Done
In the example of for loop, as the value of x becomes equal to 3, the test condition of if statement evaluates to
True, which leads to the execution of break statement. The control then skips the remaining statements in the
loop (even though all the values in the range have not been traversed) and moves to the statement immediately
following the loop (print ("Over")).
In the example of while loop, the break statement is encountered when the value of num is 3. Therefore, the loop
terminates and the control shifts to the statement, print("Done").
If the break statement is inside a nested loop, the innermost loop is terminated and the control shifts to the immediate
outer loop.
Program 6.8 Write a program To check whether a number is prime number or not.
01 '''
02 objective: To check whether a number is prime.
03 input:
04 n: the numbers to be tested for primality
05 output:
06 The message indicating whether n is prime
07 Approach: Given n is prime if it is not divisible by any integer in range(2, n)
140 Touchpad Informatics Practices-XI

