0

Why it is important to use break

Def checkprime(num): For n in range (2,num): If num%n==0: Print(num, 'is not prime') Break Else: Print(num,'is prime')

9th Dec 2019, 7:33 PM
Vikash Shandilya
Vikash Shandilya - avatar
1 Respuesta
+ 1
You should indent your code correctly and each line should start with lowercase letter. It is not apparent from your code but the 'else' branch belongs to 'for' here. The logic of the code, if there is any number up to n that gives 0 remainder to the num, then num is not a prime. If this happens, then the 'break' statement exits the for loop. The 'else' will execute only when the loop was never broken, which means that num is actually a prime.
9th Dec 2019, 7:51 PM
Tibor Santa
Tibor Santa - avatar