Control Flow
Table of contents
If/Otherwise
Use the following syntax:
@intVar = 5
if intVar > 6 {
} else {
}
If statements are not required to contain the else statement but do require the ending curly brace as in other languages.
The first operand of the if statement must be a variable. The second can optionally be a variable.
Conditional Operators
==
Is!=
Is Notcontains
Contains!contains
Does Not ContainbeginsWith
Begins WithendsWith
Ends With>
Greater Than>=
Greater or Equal<
Less Than<=
Less or Equal
Has Value/Does Not
For these conditional operators, you can compare the reference, or if checking for no value, prepend with a !
.
@variable: text
/* Has Any Value */
if variable {
}
/* Does not have any value */
if !variable {
}
Between
This checks if intVar
is between 5
and 7
.
@intVar = 5
if intVar <> 5 7 {
}
Multiple Conditions
Future Release
You can use multiple conditions by separating each condition with the same logical operator:
&&
And||
Or
To be clear, this is a limitation of how this has been implemented in Shortcuts; only “Any” (||
) or “All” (&&
) are allowed for logical comparison of conditions.
@textVar = "test"
@intVar = 1
if textVar == "test" && intVar == 1 {
// ...
}
Loops
Repeat
Use the following syntax:
@items: array
repeat i for 6 {
@items += "Item {i}"
}
The number of times to repeat can also be a variable as long as it evaluates to a number value.
Repeat With Each
Use the following syntax:
@items = list("item 1","item 2","item 3")
for item in items {
alert(item)
}
list
must be an iterable variable.
Repeat Globals
The repeat
and for
statements create variables as the RepeatIndex
and RepeatItem
globals need to be numbered after more than one nested repeat.
The globals RepeatIndex
and RepeatItem
are still available, but it is recommended to use the variables these statements create.
Nesting
if/else
, repeat
, for
, and menu
can all be nested inside each other and vice versa.
Automatic nothing actions at the end
The nothing()
actions are automatically added to the ending block of any statement. This ensures the control flow block does not store any output to reduce memory usage.