(Apr-12-2020, 06:22 PM)Chuck_Norwich Wrote: so ran them both without the break statements and the output is the same!That is not true, the output is different.
First snippet, with break:
Output:4 equals 2 * 2.0
6 equals 2 * 3.0
8 equals 2 * 4.0
9 equals 3 * 3.0
First snippet, without break:Output:4 equals 2 * 2.0
6 equals 2 * 3.0
6 equals 3 * 2.0
8 equals 2 * 4.0
8 equals 4 * 2.0
9 equals 3 * 3.0
There are 2 extra lines.break
serve to break out (i.e. terminate the execution) of the loop early. In the example - 6 is evenly divisible by 2, so there is no need to continue searching for other divisors, obviously it's not prime number.Now, in the second snippet you have also extra
else
clause. If the loop exection finishes normally (i.e. not from break) this will be executed.Output:2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
if not clear, look at this step by step execution
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs