Monday, May 25, 2009

Well, School's out, my headache's going away, and I can pick up this blog from where I left off. I'm sure you have heard of Monty Python before. One day, some guys decided to create a new scripting language based in c with a powerful command prompt. I don't think it was their intention to turn traditional coding on its head either, but they did succeed in that regard. check out this block of code.

>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print n, 'equals', x, '*', n/x
... break
... else:
... # loop fell through without finding a factor
... print n, 'is a prime number'
...

2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

the above is code which creates a list of prime numbers. The logic should be easy enough to follow. the interesting trick is how the two for loops, break, and else all work in lock step. if the embedded loop finds a prime, the break statement returns the the next step of the first for loop. If the embedded loop doesn't, and it has gone through all its iterations, then the else code block is called in response and prints 'n is a prime number'. It's a good example for why you want to think about your programming logic in order to get more effective results.

(python code referenced from www.python.org. Go check out this language. It does a lot of fun things with syntax and logic)