Example | Output |
---|
listOfFruit = ['Apples', 'Oranges', 'Bananas']
for fruit in listOfFruit:
print fruit
| Apples
Oranges
Bananas |
You don't need to manually create a sequence to repeat a task several times in a for loop. Instead, the built-in function range() function can generate a variable-size list of integers starting at zero. For example, calling range(4) will return the list [0, 1, 2, 3].
Example | Output |
---|
# Even though this example isn't using the value of "x",
# the print statement will still be executed once for each item
# in the list returned by range().
for x in range(4):
print "this will print 4 times"
| this will print 4 times
this will print 4 times
this will print 4 times
this will print 4 times |
While-Loop
A while loop will repeat a block of statements as long as a condition is true. This code will print out the contents of the items in the list.
# A while loop simply needs the keyword "while", the condition that
# determines when we should stop iterating, and a colon at the end of the line.
while condition:
# All statements that should be repeated each iteration must be indented after the "while" statement
statement.
This code uses a function called len() , which is a built-in function that returns the length of a sequence.
Example | Output |
---|
listOfFruit = ['Apples', 'Oranges', 'Bananas']
x = 0
while x < len(listOfFruit):
print listOfFruit[x]
x = x + 1
| Apples
Oranges
Bananas |
The Break and Continue Statements
You can stop a loop from repeating in its tracks by using the break statement. This code will print out " Loop " exactly two times, and then print " Finished ".
Example | Output |
---|
for x in range(10):
if x >= 2:
break
print "Loop"
print "Finished"
| |
You can use the continue statement to make a loop stop executing its current iteration and skip to the beginning of the next iteration. The following code will print out the numbers 0-9, skipping 4
Example | Output |
---|
for x in range(10):
if x == 4:
continue
print x
| |
Infinite Loops
It is incredibly easy to create an infinite loop when using a while statement. Depending where the infinite loop was created, it could cause you to lose your work in the Designer, or create a large amount of overhead on the Gateway.
x = 0
while x < 10:
x += 1 # Forgetting to add a way to increment "x" will cause an infinite loop
print x
In many cases, a for loop could be used instead of a while, but this is not always possible. When using while, the best way to avoid an infinite loop is to make sure you always have a way to exit the loop: a simple approach involves using a counter that can eventually trigger a break statement, or add the counter as an additional condition to the while.
###Example 1: using the break keyword
# The counter variable will be used as a guaranteed way out of the While.
counter = 0
# Normally, using True as a condition in a While would be a quick
# way to generate an infinite loop, but the counter helps prevent that.
while (True):
# Increase the counter
counter += 1
# Check the value of the counter. If it's at the point where we can assume we're going to be looping indefinitely...
if counter >= 1000:
# Break out of the loop
break
###Example 2: using an additional condition
# Again, the counter variable will be used as a guaranteed way out of the While.
counter = 0
# Instead of using nested logic, we can simply add counter's value as an additional condition with "and"
while (True and counter < 1000):
# Increase the counter. Once counter >= 1000, the while loop will be forced to end.
counter += 1