# Write a program to display all prime numbers within a range
start = 25
end = 50
for num in range(start, end + 1):
if num > 1: # all prime #s are greater than 1
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
Why is it that the else
is written directly under the second for
?
Is it that it's not necessarily under the for
but instead, outside of the for-loop?
Therefore, running every item in the range and checking the ones that are not prime in the for-loop as well as the ones that are during the else
?
If that's the case, then why is it that there is break? Doesn't the break immediately stop the entire process and keep the loop ever reaching the else
statement? Or does it only stop the current for-loop-- allowing the else
statement to run?
I suppose I just need help understanding what's going on.