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

#edX - 6.00x - Would someone be able to explain the difference between these two programs? < and <= num = 10 while True: if num < 7: print('Breaking out of loop') break print(num) num -= 1 print('Outside of loop') num = 10 while True: if num <= 7: print('Breaking out of loop') break print(num) num -= 1 print('Outside of loop') 10 9 8 7 # to me this shouldn't be happening Breaking out of loop Outside of loop 10 9 8 # to me this shouldn't be happening Breaking out of loop Outside of loop

ganeshie8 (ganeshie8):

the first program loops until 'num' gets the value '6' the second program loops until 'num' gets the value '7'

ganeshie8 (ganeshie8):

in second program, when num gets the value 7, the if condition passes, and the control breaks out of loop.

OpenStudy (anonymous):

num < 7 returns True if and only if num is less than 7. num starts at 10. This is more than 7, so it continues it goes to 9, 8, then 7. When num hits 7, it is still not less than 7, so it continues. num goes to 6. This is less than 7. The loops is broken here. In the second case, it stops early because 7 is equal to 7 and <= will break when the things being compared are equal.

OpenStudy (anonymous):

Funny how some sleep and a lessening of a cold makes the mind a bit more clear. Obviously 7 is not less than 7. Maybe what through me is the While True. Would this conditional be different under another condition, say "while num <7". Does anyone know what part of the online textbook this comes from? Is anyone finding the assignments are further ahead than what's been taught?

OpenStudy (anonymous):

Thanks to all who answered :)

OpenStudy (anonymous):

threw me...so 7 is equal to 7, it's not greater than. Is that why that statement is still true?

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!