Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 14 Online
OpenStudy (anonymous):

Can someone help me understand how this code works? I am not familiar with how the break command works. i = 2 while(i < 100): j = 2 while(j <= (i/j)): if not(i%j): break j = j + 1 if (j > i/j) : print i, " is prime" i = i + 1 print "Good bye!"

OpenStudy (konradzuse):

while continues until some condition is met, then stops. You can also break out of the while loops with the break command.

OpenStudy (anonymous):

'break' jumps you out of the while(j ... loop. If j is a factor of i, then i is not prime, so you don't need to test any further values of j.

OpenStudy (rsmith6559):

while continues until some condition is NOT met, then stops. If the condition returns true, the code block is executed. If the condition returns false, the code block is skipped.

OpenStudy (konradzuse):

^That is more technically correct "Not met." :p

OpenStudy (anonymous):

Got it. Thanks.

OpenStudy (anonymous):

break lets you skip a snippet of code

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!