tags:

views:

243

answers:

7

Hi all, I am a newb in python, I can understand what does for loop do, but cant really understand while loop do, I knew it does repeat something while the condition is true, easy to say, but really hard to use as far as I think. say a example here:

while 1:
   rate(100) #what does this rate(100) do?
   try:
      'something'
   except:
      'something else'

I just cant understand it.....help! thanks in advance:)

+2  A: 

while 1 is a special case of while loop: It's a so-called "infinite loop".

Of course nothing is infinite. What the code is saying is: Run the contents of this loop until something inside causes it to exit. There's no explicit test in the loop, so the exit condition must be inside.

Either that, or the loop is meant to run for the entire duration of the program, and the program will run until killed by the operating system - or you flip the big red switch on your PC.

Carl Smotricz
A: 

Well, while 1 is an infinite loop that will run forever. I'm guessing here a bit but rate(N) looks like the sort of function that would try to do something 100 times a second (or minute or other time period), in which case it's likely to be a simple "sleep for time period divided by rate".

For example, rate(100) would, in that case, be a sleep for 1/100th of a second (or other time unit).

Bottom line, it looks like a loop which continously tries something 100 times per time unit and, upon failure, do something else (the exception).

Without more context, you're probably not going to be able to get much more than that.

paxdiablo
A: 

The while 1: construction is a infinite loop, ie it never ends.

HeMan
+2  A: 

You have actually defined an infinite loop. while 1: means to run the code-block below infinite times. You can only exit from it using a break statement.

The rate(100) calls the rate method, which must be defined somewhere. Then a try-catch block is executed. First 'something' code is run. If no errors occur, then the execution returns to the top of the loop. In case of an error, 'something else' is run.

kgiannakakis
A: 

while 1 is an infinite loop - it will run forever.

The while loop is a statement that allows code to be run on a given boolean value.

You can find more information here, and I've included the example given in the article below - converted to python.

x = 0
while (x < 3):
   print "x = %d\n" % x
   x++
Daniel May
+3  A: 

The example code you have provided is equivalent to this:

while True:
    rate(100) #what does this rate(100) do?
    try:
        'something'
    except:
        'something else'

In Python many expressions can be tested for truth (that is evaluate to True or False when used in conditions and logical operations). For example non-empty sequences or non-zero numbers as you have evaluate to True. So this loop will run forever or until some code explicitly breaks out of it. This is because the loop condition is hard-coded to True so will never evaluate to False to exit the loop.

The basic behaviour of the while loop in python is well explained in the Python Tutorial. Unless you have a more specific question I would suggest you start there. Other Python flow control constructs are also described there.

Tendayi Mawushe