I have not understood correctly not understood While true:
asked 11 Mar '12, 05:16
What unit? What video? What Python code? What are you asking?
answered 11 Mar '12, 05:19
means that you are creating an infinite loop. If you don't create a condition to stop it, it will continue forever.
This loop will break as soon as n will be equal to 0.
answered 11 Mar '12, 05:25
so for using while true:
so why is this needed when we can do it by giving any other condition ...
answered 11 Mar '12, 05:28
The while loop is a control flow construct that checks an expression that must result in a boolean value (True or False). If the expression results in True the while loop will allow the code within it's code block to execute (anything one indentation level greater than the line that has the While loop on it). Example:
The two print statements above will execute FOREVER because the expression that while is using to base its decision on whether to execute the print statements is always true (because it's the boolean value True). To make it so the above print statements don't execute until the end of time there are two options:
I break statement is pretty straight forward....it breaks the program out of the loop. The return statement also changes the execution of code. If you're using a return statement that means you're in a procedure and if your loop is being run that means a different part of the program called that procedure. Executing a return statement returns the program back to the location the procedure was called from (technically just after it) so that is also why a return statement can get your program out of a loop.
The while loop itself doesn't return anything...it just blindly checks "can I let the code within my code block run?" to get the answer it checks to see if the conditional expression associated with it results to True or False.
Hope this helps some.
Guess I spent too much typing :) Looks like others have helped out.
The while loops works like this:
While the condition is true, you execute the code in the while loop, otherwise you don't
While True is true, you execute the code in the while lopp, otherwise you don't.
As True is always true, it is a never ending loop, or an infinite loop. And the only way to get out of it is to break (which basically means "get out of this loop NOW", or if it's in a procedure a return is possible too. If you choose to return the value, it won't have anything to do with the condition.
answered 11 Mar '12, 06:11