Skip to main content
Version: 7.9

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.

Inductive University

Control Flow Logic

Watch the video

Simple If-Statement Example

The syntax for if is as follows:

Pseudocode - If Statement
# 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:

x = 5
z = 15
if x < 10:
# Since the condition "x < 10" is true,
# the following line will execute
print "'x' is less than 10"
if z < 10:
# This condition "z < 10" is false,
# so the following line will not execute
print "this will never show"

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:

x = 15
if x < 10:
print "x is less than 10"
else:
print "x is not less than 10"

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.

Example:

x = 3
if x == 1:
print "one"
elif x == 2:
print "two"
elif x == 3:
print "three"
else:
print "not 1-3"

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

Inductive University

Control Flow Loops

Watch the video

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.

Pseudocode - For Loop
# 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:

listOfFruit = ['Apples', 'Oranges', 'Bananas']
for fruit in listOfFruit:
print fruit

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:

# 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"

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.

Pseudocode - While Loop
# 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:

listOfFruit = ['Apples', 'Oranges', 'Bananas']
x = 0
while x < len(listOfFruit):
print listOfFruit[x]
x = x + 1

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:

for x in range(10):
if x >= 2:
break
print "Loop"
print "Finished"

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:

for x in range(10):
if x == 4:
continue
print x

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.

Python - Infinite Loop Created by While Statement
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.

Python - Preventing Infinite Loops Using the Break Keyword
###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
Python - Preventing Infinite Loops Using an Additional Condition
###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.

Python - Pass Keyword
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