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.
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
You can use as many elif items as you want, and the else is not required at the end.
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].
this will print 4 times this will print 4 times this will print 4 times this will print 4 times
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.
This code uses a function called len() , which is a built-in function that returns the length of a sequence.
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 ".
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
0 1 2 3 5 6 7 8 9
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.
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.
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.