Contents:
- break Statement
- Example
break Statement
In Python, break statement is a control flow statement that terminates the closest enclosing loop, in which it is written, prematurely when encountered and execution starts from the next instruction following the loop just terminated.
Example
# Write a program in Python that takes an integer value from user and display whether the number is Prime number or not.
x = int(input(“Please Enter Your Number: “))
if x > 1: # Check for the number if it is greater than 1
y = 2
while y <= x//2:
if x%y == 0:
print(“It’s not a Prime Number”)
break # Terminates the loop if the number is divided by any number starting from 2 to half of the input number
y=y+1
else: # else clause executes if loop terminates normally without encountering break statement
print(“It’s a Prime Number”)
else:
print(“Give any positive value greater than 1”)
Output:
Please Enter Your Number: 53
It’s a Prime number
🙂 Happy Learning !