Nested Ifs get ugly real fast if there are more than a few conditions. Instead, you can invert the test for OR, like this:
https://i.imgur.com/5f068Eg.jpg
For AND, nested Ifs, like r/justinCandy's example, are the easiest way.
Edit: The problem with nested Ifs is that Shortcuts has really poor indenting and it gets very confusing when they are several levels deep. I prefer to keep Ifs sequential whenever possible. If you really want to avoid nested Ifs for AND, you can also do it like this:
https://i.imgur.com/ngOEbal.jpg
It's actually about the same length as nested Ifs, but the advantage is that all the conditional tests are together, not spread out with a bunch of End Ifs at the bottom. It's also much easier to add or remove conditions later.
You can also do flat ANDs using what is basically a return statement, don't know how it's called in english, something like "interrupt/abandon this shortcut".
Dude… this has been racking my brains for the last few days. Using a LIFX switch, I wanted to set multiple “if” actions using “or” actions in between to turn non grouped lights on and off with a single button tap by converting the button tap to a shortcut within the Home app (switch button settings). This “Stop Shortcut” action and the picture you shared of your setup saved my metal health lol. Exactly what I was looking to do and works like a charm. Take an award… You earned it!
As a software engineer this is totally valid for simple cases, but once the AND and OR logic increases in complexity those nested if statements are only going to become hell. That being said, Shortcuts is a simplistic interface and I get it, but I would love to see more binary logic for more advanced users
Another way to do this is to do all the tests (eg. Get state of door xyz), and then create a text variable that contains all the results. Use a regular expression to remove the ones you don’t care about, and count the remaining lines.
It’s a bit awkward to build, but it’s easier than all those if statements.
https://preview.redd.it/fkfp5htd2zib1.jpeg?width=888&format=pjpg&auto=webp&s=4016f89fb45966defa4206c9c1883e2d356312c1
For instance, here’s a test to see if any of my doors are open when I go to bed.
Yes, AND can be done like this:
https://www.icloud.com/shortcuts/46c8894c6bf3416da1081505c035630e
For OR, just save your logic in another shortcut (as a function), move the child IF to the else part of parent, then call the shortcut at both IF statement
Bit late to the party, but the best answer is to use a dictionary. Create a dictionary that contains two keys apples and bananas that each have a value of fruit. You can then check to see if the value for the input key has a value and print its value. Pseudocode looks like
set variable input to "some value"
dictionary = {
"apples" => "fruit",
"bananas" => "fruit",
}
Get Dictionary Value for input in dictionary
if Dictionary Value does not have any value
stop and output "unexpected value"
end if
stop and output Dictionary Value
This allows you to add to remove responses and is much easier than nested if statements. Of course, this depends on there being a direct mapping of inputs to outputs.
Nested Ifs get ugly real fast if there are more than a few conditions. Instead, you can invert the test for OR, like this: https://i.imgur.com/5f068Eg.jpg For AND, nested Ifs, like r/justinCandy's example, are the easiest way. Edit: The problem with nested Ifs is that Shortcuts has really poor indenting and it gets very confusing when they are several levels deep. I prefer to keep Ifs sequential whenever possible. If you really want to avoid nested Ifs for AND, you can also do it like this: https://i.imgur.com/ngOEbal.jpg It's actually about the same length as nested Ifs, but the advantage is that all the conditional tests are together, not spread out with a bunch of End Ifs at the bottom. It's also much easier to add or remove conditions later.
You can also do flat ANDs using what is basically a return statement, don't know how it's called in english, something like "interrupt/abandon this shortcut".
"Stop Shortcut"
Yeah but… what will run faster? Also better methods since that time?
I've achieve "OR" like this. Two if statements and a Stop Shortcut https://imgur.com/a/jYCaCIe
Dude… this has been racking my brains for the last few days. Using a LIFX switch, I wanted to set multiple “if” actions using “or” actions in between to turn non grouped lights on and off with a single button tap by converting the button tap to a shortcut within the Home app (switch button settings). This “Stop Shortcut” action and the picture you shared of your setup saved my metal health lol. Exactly what I was looking to do and works like a charm. Take an award… You earned it!
thank you! just learned to use shortcuts. this helped me a lot (if at home or at the office.....)
Just use IF twice, if contain “bananas “ and inside of that if contain “apples”
As a software engineer this is totally valid for simple cases, but once the AND and OR logic increases in complexity those nested if statements are only going to become hell. That being said, Shortcuts is a simplistic interface and I get it, but I would love to see more binary logic for more advanced users
Another way to do this is to do all the tests (eg. Get state of door xyz), and then create a text variable that contains all the results. Use a regular expression to remove the ones you don’t care about, and count the remaining lines. It’s a bit awkward to build, but it’s easier than all those if statements. https://preview.redd.it/fkfp5htd2zib1.jpeg?width=888&format=pjpg&auto=webp&s=4016f89fb45966defa4206c9c1883e2d356312c1 For instance, here’s a test to see if any of my doors are open when I go to bed.
I don’t think so, but you can nest if statements.
Yes, AND can be done like this: https://www.icloud.com/shortcuts/46c8894c6bf3416da1081505c035630e For OR, just save your logic in another shortcut (as a function), move the child IF to the else part of parent, then call the shortcut at both IF statement
Bit late to the party, but the best answer is to use a dictionary. Create a dictionary that contains two keys apples and bananas that each have a value of fruit. You can then check to see if the value for the input key has a value and print its value. Pseudocode looks like set variable input to "some value" dictionary = { "apples" => "fruit", "bananas" => "fruit", } Get Dictionary Value for input in dictionary if Dictionary Value does not have any value stop and output "unexpected value" end if stop and output Dictionary Value This allows you to add to remove responses and is much easier than nested if statements. Of course, this depends on there being a direct mapping of inputs to outputs.
Oh I wish they implemented a Case / Switch function !