IT Window is a new series of articles special for younger people who are "into computers". I have a friend who is learning programming and looking over to "big boys" tools, and sure he have some questions, those still occurs even after reading tutorial or two. Here is the place where I step in and try to help him by writing these articles. Sure samples could be done better, but I try to use as simple format as possible avoiding one liners.
In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement.
Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. For loops are also typically used when the number of iterations is known before entering the loop. For loops are shorthand way to make loops when the number of iterations is known, as a for loop can be written as a while loop.
The name for loop comes from the English word for, which is used as the keyword in most programming languages to introduce a for loop. The loop body is executed "for" the given values of the loop variable, though this is more explicit in the ALGOL version of the statement, in which a list of possible values and/or increments can be specified. In FORTRAN and PL/I though, the keyword DO is used and it is called a do loop, but it is otherwise identical to the for loop described here.
For loop is a method/function that runs for certain time - for 5 minutes, for 5 times, for 5 objects, etc. During each cycle it usually contains value that changes each time. Lets show it using pseudo-code. Lets say we have 4 friends and we want to give each a number from 1 to 10, without a loop it would look something like this:
Already see repeating part? so with for loop we could do that:
Step to friend #1 give him a number 1 Step to friend #2 give him a number bigger than last one by 1 Step to friend #3 give him a number bigger than last one by 1 Step to friend #4 give him a number bigger than last one by 1
OK different situation - lets say we have two numbers (2 and 100) and we want to find even numbers between those two. No I won't write pseudo-code without a loop (that would be just spamming), but if we would use for loop:
For each friend Give number bigger than previous
So each time (each number or loop) we would take the same variable that contains new number and look if its even or not, if its even we shout "EVEN!" if not - we shout "D'OH!".
For each number starting with 2 ending with 100 if its even shout "EVEN!" else shout "D'OH!"
And our third and final situation - lets say we have unknown number of apples, some are yellow, some are red and some are green. We want to count each color. In our mind it's simple task, just look at each apple and count different colors. With for cycle we do the same:
After this we would have 3 values each representing different color and count.
For each apple look at the color add +1 to that color
Now lets do the same in real programming language - Python!
For our first case - firstly define our list of friends and base number:
Now lets do the loop:
friend_list = [['John', 0], ['Tom', 0], ['Billy', 0], ['Harry', 0]] number = 0
Have in mind that Python starts counting from 0, so second element in list is reached with array key 1 not 2. So now if we would run:
for friend in friend_list: number += 1 friend = number
Now lets do the second case:
[['John', 1], ['Tom', 2], ['Billy', 3], ['Harry', 4]]
Yep, to go from one number to another just use range(), easy eh? OK now the final one. Lets define our apples and colors:
for number in range(2, 100): if number % 2 == 0: print number, 'is even' else: print number, 'is odd'
Now simple for loop:
apples = ['yellow', 'green', 'red', 'yellow', 'red', 'red'] green, red, yellow = 0, 0, 0 # All colors have 0 value
So final result is:
for apple in apples: if apple == 'yellow': yellow += 1 if apple == 'red': red += 1 if apple == 'green': green += 1
And we sure get:
print 'yellow = ', yellow, ' red = ', red, ' green = ', green
More about For loop in Python - http://wiki.python.org/moin/ForLoop
yellow = 2 red = 3 green = 1
In most more basic languages such as Pascal we would need to know how many elements there are in the array therefore #1 and #3 samples are different since in #3 we don't know the number of apples, yet in Python it's not a problem. In other languages we firstly would count elements in the array using some libs or hold a number somewhere to determine count of data inside an array.