# 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

3 Likes

Another possible way:

find domain.gh (14.9 KB)

3 Likes

Thank you,
I just decided to use python instead.

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.