If statement on empty branch

I used to teach if/then/else statements using the Pick’n’Choose component like this :

You have components that allow you to establish some conditions (True/False). Equality, Smaller Than, Larger Than, Match Text are among the most useful.
If you have several conditions to test, you can use logical operators (and/or/xor/not) to combine them.

The thing that changes in Grasshopper versus classic programming is that you need to evaluate both the then and else branches for each item in your list/tree, and the condition result is only there to pick the correct one. Basically, in your example, even though List Length = 0, you still compute the shattering operation on the curve (which returns an empty branch), only to ignore that specific result, and pick the original curve instead.

Note that Pick’n’Choose can also be used as an equivalent to a switch/Select Case operating on an integer value, since you can add as many inputs as you want.

Sometimes, for convoluted mathematical expressions (e.g. x>2.43 y + sqrt(z1/2) + …), it’s best to use Expression, which accepts the syntax if( condition , then , else).

1 Like