Conditions and Loops
If-Statements​
The if statement should be familiar to anyone with a passing knowledge of programming. The idea of an if is that you want your script to execute a block of statements only when a certain condition is true. Python's if is simple to use, and has some additional keywords to provide more flexibility.
Simple If-Statement Example​
The syntax for if is as follows:
# Note that 'if' uses lowercase characters.
# Additionally, a colon is placed after the expression.
if expression:
# The statements that should execute when the expression is true
# MUST be indented.
statement
Example | Output |
---|---|
| 'x' is less than 10 |
If and Else​
You can use the if...else form of an if statement to do one thing if a condition is true, and something else if the condition is false.
Example | Output |
---|---|
| x is not less than 10 |
Elif (Else If)​
Lastly, you can use the if...elif form. This form combines multiple condition checks. elif
stands for "else if"
. This form can optionally have a catch-all else
clause at the end. For example, this script will print out three:
Example | Output |
---|---|
| three |
You can use as many elif items as you want, and the else is not required at the end.
For-Loops and While-Loops​
For-Loop​
Python's for loop may be a bit different than what you're used to if you've programmed any C. The for loop is specialized to iterate over the elements of any sequence, like a list. A for loop uses an iterator variable to reference each item as it steps through the sequence. This means it's very simple to write a loop!
Note that the syntax of the for-loop requires use of the in-keyword.
# In this example, "item" is a variable created specifically by the "for" loop to act as an iterator.
# The name "item" is not a keyword, and a different variable name may be used.
# Additionally, note that "for" and "in" are lowercase, and a colon is present at the end of the line.
for item in sequence:
# All statements that should execute each iteration must be indented after the "for" statement
statement
Example | Output |
---|---|
| 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 |
---|---|
| 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 |
---|---|
| 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 |
---|---|
| Loop Loop 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 |
---|---|
| 0 1 2 3 5 6 7 8 9 |
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
The Pass Keyword​
When using conditional statements and loops, the pass keyword can be especially useful when writing a new script. When called, the pass keyword does nothing, which may seem useless. However it is great when you find yourself in a situation where you need a line of code to meet a syntax requirement, but don't want the code to do any additional work.
myVar = system.tag.read(tagPath).value
if myVar == 0:
firstFunction()
elif myVar == 1:
secondFunction()
elif myVar == 2:
# I haven't implemented the thirdFunction() yet, so I can use pass here as a placeholder
pass