are stopping the execution of the current iteration and continuing with the next iteration. Otherwise, we are checking whether the number is a factor of n.
The above program will print the following output:
1 7 21
Now, let’s say we want to check whether a number n is a prime number in a primitive way. We will take all numbers between 2 and (n – 1) and divide n by the number. If no number between 2 and (n – 1) divides n, then n is a prime number. We can implement the code in the following way:
n = 37 i = 2 while i < n: if n % i == 0: print("The number is not a prime number") break else: i += 1 continue else: print("The number is a prime number")
Here, we are taking all numbers between 2 and (n – 1) and checking whether the number divides n. If it does, then n is a composite number and not a prime number. So, we are using the break statement to come out of the loop.
If the number does not divide n, then we are using the continue statement to stop the current iteration of the loop and continue with the next iteration.
If no number between 2 and (n – 1) divides n, then n is a prime number. So, in that case, the while loop will terminate and the else part of the while loop will be executed. As a result, the program will print the output that the number is a prime number.
The above program will print the following output:
The number is a prime number






0 Comments