If Then Else

Until our dedicated user library is in place you can post examples and modules here

Moderators: electrogear, exonerate

If Then Else

Postby trogluddite on Mon Mar 28, 2011 8:43 pm

Here's an example of the new If..Then..Else primivitve introduced in SM 2.0.3...
IfThenElse.osm
(55.89 KiB) Downloaded 169 times

IfThenElse.png
IfThenElse.png (46.31 KiB) Viewed 986 times

The one on the left is probably easiest to understand ..

Type - String, so all the "Conditions" are treated as strings
If - your ibput value that you want to test goes in here
Op - the type of comparison, in this case we're seeing if the input is an exact match for one of the conditions
Conditions - the primitive works down this list from top to bottom, stopping when it finds a comparison which is true. In this case it will stop if the input matches exactly any of the strings in the array (because the 'Op' is "=").
Then - each item in this list pairs up with each one in the 'Conditions' list - this is the output that you will get if the condition is true. So in this case "one"-->"sheep", "two"-->"cow", etc.
Else - if no match is found in the conditions list, you get this output instead.

The right hand example is a bit tricker, but essentially works the same way.

Type - Float this time, so the strings at the 'If' and 'Conditions' inputs are now treated as numbers rather than strings. Important, because if you used strings, "100.00" would not equal "1e2" - but as floats, they are the same number.
If - the input
Op - I used 'Greater' this time, to demonstrate a particular point...
Conditions - this time we're looking through the list until we find a number where the input is greater - but CARE NEEDED - say I had put 0.25 as the first number, and the input is 0.9 - 0.9 is greater than 0.25, wahay, it's true, BUT the rest of the list will not get scanned, so we'd never find out that 0.9 is greater than 0.75 too. So if using the primitive to sort numbers - be careful what order you list the conditions.
Then - as before, the output is the 'Then' value with the same index as the 'True 'Condition'
Else - as before, the default value if there is no match.
Feel free to use any schematics and algorithms I post on the forum in your own designs - a credit is appreciated (but not a requirement).
Don't stagnate, mutate to create. Without randomness and serendipity the earth would be just another barren rock.
User avatar
trogluddite
smychopath
 
Posts: 3024
Joined: Mon Oct 20, 2008 3:52 pm
Location: Yorkshire, UK

Re: If Then Else

Postby infuzion on Tue Mar 29, 2011 6:28 am

Thanks trog!
Need help? First search the forum & WiKi, then post in the help forum with a clear topic, request, & OSM. Then please WiKi the correct solution. If you want my personal assistance, I charge by the hour or for an exchange of services.
infuzion
smstar
smstar
 
Posts: 6163
Joined: Wed May 04, 2005 8:02 pm
Location: Earth, USA, CO, Denver

Re: If Then Else

Postby philter5 on Tue Mar 29, 2011 7:44 am

thank you for this nice explanation ;)
---Yes, a piece of software CAN be your best friend---
User avatar
philter5
smaniac
 
Posts: 1478
Joined: Thu Jan 04, 2007 7:52 pm
Location: Germany


Return to Examples

Who is online

Users browsing this forum: No registered users and 0 guests