From BlenderWiki

Jump to: navigation, search
Note: This is an archived version of the Blender Developer Wiki. The current and active wiki is available on wiki.blender.org.

Contents | Previous | Next


7 Loops

So far we had good fun, and if you have done homework and as I suggested you start to build up your Borg Cub game, you will soon notice that you repeating or copy pasting alot of code, and the more complex your code becomes that harder it is to read it and understand what is going on.

What will happen if you wanted to print numbers from 1 to 1.000.000, would you really need to call print 1 million times, write 1.000.000 lines of code? Obviously this is far from efficient and that is why Python implements loops. All loops do is repeating a fragment of your program.


7.1 While


The while loop is a lot similar to an <if> statement, you can even say that it is an <if> statement that can repeat itself until it returns the boolean type False. Lets print one million numbers

Note: a # ends a line for the Python interpreter (if not in a text!), therefore one can add comment inside of a code-line as you see below ;-).

x = 0       # any other (integer) value
while x < 1000000:
    print("x: ", x)
    x = x+1 # later you will use preferable '''x += 1''',
            # which does the same but allows at once runtime optimization

We managed to compress one million lines of print statements (that abit less that the entire source code of blender) to just 5 lines ! Not bad, not bad at all. As you can see <while> acts very similar to an <if>, it examines the comparison and only if it returns true it executes it, and the big difference with <if> is that after execution of the sub-statements it repeats the examination of the comparison and the execution until the comparison returns False. When it returns False the looping/repeating ends. In this case it stops when x became 1000000.

This is still not an ideal way of coding it. <While> is used for repeating execution of code based on whether some specific criteria have or have not been met. If you want just to make a loop that repeat a specific amount of times then a For loop is more suitable syntax, but you can accomplish the same results with a while loop just fine.


7.2 For


In this case we can change our code into this

for x in range(1000000):  #later you learn about
                          #'''range(startvalue, endvalue, stepvalue)''
    print("x: ", x)

You already see the benefit, we decreased the code from 5 lines to only two. We could have written the above code < for x in range(1000000): print("x: ", x) >, pretty much anything that takes a <:> can be put in a single line like that. It makes sense for small code but it is not ideal for anything big that could create a long ugly line.

Th above code does not say much in such a tiny program but when you programs start getting bigger and bigger and less readable you will wish that you used <for> instead of <while> to make your life easier.

As you can see here there is no reason to define x, or increase x value by 1 for each loop, for syntax defines x for us and uses the built in function range to build a range of values between 0-1.000.000 which are assigned one by one to the x variable.

Observe that the last value x takes is 999.999, so if you really want it to end at 1 million the range should be range(0,1000001)

<range> function could be used in many different formats. We could write the above code range(0,1000001), or range(0,1000001,1) which basically says "start from zero end in 1000001 increasing by 1 each time", of course you can change the arguments of the function to anything you want and thus change how for counts and how many times is executed. If you omit the first and third argument they will default to 0 and 1, so in this case we don't need to use them.

Another useful way of using loops is with the "in" keyword we talked about in the if statement Chapter.

x = ["john", "david", "mary", "tom", "james", "bill"]
for y in x:
    print("x contains: ", y)

Here y instead of a range of numerical values it get the values contained in the x's list indices.

Sometimes you need to know at which place the y was to be found too. To do that there is the is the possibility using enumerate and the code changes for example like this:

x=["john", "david", "mary", "tom", "james", "bill"]
for i, y in enumerate(x):
    print("x contains: ", y, " found on place ", i + 1)

Note: if you start counting at zero and not at one', as Python does, change i + 1 into i.


Contents | Previous | Next