Purpose of conditional logic in scripting
It is common when you write scripts to want to perform different actions when different conditions are met. You can use conditional statements in your script functions to do this.
You can use the following conditional statements in scripts:
- Use if to specify a block of code to be executed, if a specified condition is true
- Use else to specify a block of code to be executed, if the condition tested above is false
- Use else if to specify a new condition to test, if the condition tested above is false
Note here that a condition is any variable or statement that evaluates to true or false (a Boolean).
The 'if' statement
Use the ‘if’ statement to specify a block of script to be executed if a condition is true.
This is the general syntax that all if statements follow.
if (condition) { //insert script to run if the condition is true, here }
Note that the ‘if’ statement must be in lowercase. Otherwise it will generate an error.
A basic example of how to use an if statement follows. Note that ‘hour’ is a numeric variable that should be set before the if statement runs. ‘greeting’ is an already created variable that we are changing the value of inside our if-statement.
if (hour < 18) { greeting = "Good day"; }
If hour has been set to a number less than 18, greeting will change to “Good day”. But if hour has been set to 18 or higher, greeting will retain its previous value.
The 'if-else' statement
Use the ‘else’ statement to specify a further block of script to be executed if the condition tested in the if statement is false.
This is the general syntax that all if-else statements follow.
if (condition) { //insert script to run if the condition is true, here } else { //insert script to run if the condition is false, here }
Again, note that the ‘else’ statement must be in lowercase. Otherwise it will generate an error.
A basic example of how to use an if-else statement follows, where ‘hour’ and ‘greeting’ are as defined above.
if (hour < 18) { greeting = "Good day"; } else { greeting = "Good evening"; }
If hour has been set to a number less than 18, greeting will change to “Good day”, if hour has been set to 18 or higher, greeting will change to “Good evening”.
The ‘else if’ statement
Use the ‘else if’ statement to specify a new condition to be tested if the first condition tested is false.
This is the general syntax that all else if statements follow.
if (hour < 10) { greeting = "Good morning"; } else if (hour < 20) { greeting = "Good day"; } else { greeting = "Good evening"; }
If hour has been set to a number less than 10, greeting will change to “Good morning”. If not, and hour has been set to a number greater than or equal to 10 and less than 20, greeting will change to “Good day”. But if hour has been set to 20 or higher, greeting will change to “Good evening”.
The else statement at the end of the above chain is optional. If it is removed in the above example, and hour was greater than or equal to 20, greeting would retain its previous value.
Also note that you can continue the else if chain with any number of conditions, but you can only include an else statement at the end of the chain. Otherwise you will generate an error.
The short 'if-else' statement (ternary operator)
If you are feeling confident with simple if else statements and you want to minimise the length of your script, you can use the following single line version of an if statement, known as a ternary operator.
var variable = condition ? trueValue : falseValue
This sets a ‘variable’ to ‘trueValue’ if the condition is true and ‘falseValue’ if the condition is false. Using our if-else example above, the five lines of script can be written as a ternary operation as follows:
var greeting = hour < 18 ? "Good day" : "Good evening"
Again, if hour has been set to a number less than 18, greeting will be set to “Good day”, if hour has been set to 18 or higher, greeting will be set to “Good evening”.
For more information on different types of conditions see the following articles.
- Testing the value of a string
- .isTrue() and .isFalse()
- .IsSelected() and .isChecked()
- .SelectedValue() and .CheckedValue()
For information on using conditional logic in your document templates, check out this article.
If you have any trouble with this script, you can submit a request at the top of this page.
Comments
0 comments
Article is closed for comments.