If else statement in grasshopper

Is there a way to create if else statement like this one?
if 0<x=<0.2 is true, y =1
if 0.2<x=<0.4 is true, y =2
if 0.4<x=<0.4 is true, y =3
if 0.6<x=<0.8 is true, y =4
if 0.8<x=<1.0 is true, y =5


If else statement_re.gh (10.5 KB)

3 Likes

Another possible way:


find domain.gh (14.9 KB)

3 Likes

Thank you,
I just decided to use python instead.

:roll_eyes: Be sure to share your Python solution? Hard to believe it would be simpler than what @akilli posted.

here is the script
if x <= 0.2 and x >= 0:
print (β€œ1”)
if x <= .4 and x > .2:
print (β€œ2”)
if x <= .6 and x > .4:
print (β€œ3”)
if x <= .8 and x > .6:
print (β€œ4”)
if x <= 1 and x > .8:
print (β€œ5”),
then convert it to a number.

if x <= 0.2 and x >= 0:
    print (β€œ1”)
if x <= .4 and x > .2:
    print (β€œ2”)
if x <= .6 and x > .4:
    print (β€œ3”)
if x <= .8 and x > .6:
    print (β€œ4”)
if x <= 1 and x > .8:
    print (β€œ5”)

That produces an error:

Runtime error (SyntaxErrorException): unexpected token β€˜β€œβ€™

File β€œβ€, line 2
print (β€œ1”)
^
SyntaxError: unexpected token β€˜β€œβ€™

This works better:

if x <= 0.2 and x >= 0:
    a = 1
if x <= .4 and x > .2:
    a = 2
if x <= .6 and x > .4:
    a = 3
if x <= .8 and x > .6:
    a = 4
if x <= 1 and x > .8:
    a = 5

Do you understand what @akilli posted? It’s better than a series of β€œif” statements.

Math

if you don t care about includiung 0 and values below 0 or above 1
you can also use
y = Ceiling(x/0.2)

(Ceiling β†’ round up to next integer equal or bigger
Floor β†’ round down …)

title / topic

and as the title of your topic might direct someone with a complete different question:

The Sift+Combine pattern is designed to mimic conditional clauses (i.e. β€˜if statements’). It allows you to pull apart a stream of data, operate on subsets of that stream and then put it all back together again in the same order.

from the old forum

2 Likes

Find Domain is far more flexible than a series of hand coded β€œif” statements:


find domain_2023Feb19a.gh (12.9 KB)

3 Likes

True, I will use that for the future.
Thank you for the help and script it is very much apricated.

Stream filter (or stream gate) worked well for me for a true/false if/then filter.