Noob question: Loop instead of nested IF

Hi for now I always use nested if-statements like:

IF(pageVars.show_counter==0,0,IF(pageVars.show_counter==1,0.1,IF(pageVars.show_counter==2,0.2,IF(pageVars.show_counter==3,0.3,IF(pageVars.show_counter==4,0.4,IF(pageVars.show_counter==5,0.5,IF(pageVars.show_counter==6,0.6,IF(pageVars.show_counter==7,0.7,IF(pageVars.show_counter==8,0.8,IF(pageVars.show_counter==9,0.9,1))))))))))

Can I instead use some form of loop formular to improve myself?

In the example shown, it looks like you could just divide show_counter by 10 and that would handle all the scenarios you are doing in your ‘if’.

If you’re just doing a bunch of lookups, if A then X, if B then Y, if C, then Z, I tend to use app or page variables (with type object) to create “lookup variables”. The “key” is the looked-up element, the “value” is the returned value.

{
  'A': 'red',
  'B': 'yellow',
  'C': 'blue'
}

Then use LOOKUP(‘A’) which will return ‘red’. This has an advantage of containing your mapping values outside of the app logic itself - so if you want to change a label, or color assignment, etc, you don’t have to go into the app logic, you just edit your lookup variable.

Here I have a lookup called “pressure needle” which takes a weather trend label (retrieved from an external api call) and converts it to a rotation angle for an animated compass needle:

if I LOOKUP(‘rising’), I get 0, if I LOOKUP(‘steady’) I get 90, and LOOKUP(‘falling’) returns 180…all compass degrees.

The IF/ELSE version of this would be:

IF(label ==‘rising’,0,IF(label==‘falling’,90),IF(label==‘steady’),180,NULL)))

1 Like

thanks for the answer. this helps me alot