Skip to main content
Jerimy_Brown1
September 28, 2023

We could use IF, ELSE IF, ELSE statements?

  • September 28, 2023
  • 24 replies
  • 5042 views

We could use IF, ELSE IF, ELSE statements… Many times in code you may want to do many different things based on different string values

For example, I might have three different landing pages based on 3 conditions, and upon button press I might want to say

IF “has-users”==0 goto empty landing
ELSE IF “has-users”==1 goto landing a
ELSE IF “has-users”==2 goto landing b
ELSE goto other

24 replies

Jerimy_Brown1
January 23, 2024

I was hoping this would be helpful to someone else. Took me a while to consider it as a possible solution because it felt so janky. I tried it out of pure desperation.

Vaishnavi_Pardakhe
January 23, 2024

First we check the condition if the condition is satisfy the we take 1st condition else if condition is not satisfy then we going on 2nd condition in that way we can do it

Jerimy_Brown1
February 9, 2024

Another update for anyone using this hack…

I just got done merging a branch of fixes into our prototype where I discovered that Figma must have updated to a more strict sequencing protocol with there ‘if’ statements. So if you were using a string of ‘if’ statements to achieve an ‘if/else if/else’ behavior, like I was, you might have to re-evaluate your sequencing order and swap the order to assure that an evaluation of true on a smaller value doesn’t move your math along unintentionally.

So I original had a string of 'if’s

if var == 1
set properties or other variable values
if var == 2
set properties or other variable values
if var > 2
set properties or other variable values

I had to swap the order to

if var > 2
set properties or other variable values
if var == 2
set properties or other variable values
if var == 1
set properties or other variable values

Seems like the original Figma behavior was to stop at the first evaluation of true in the string of statements, but now it runs through each statement and on to the next (until complete done with all), which is fine as long as you know and expect that. None of this will mater if they decide to just give us true if/else if/else statements, but until then stringing if statements with blank else works if you understand how Figma will process that when you hack it 😉

So now that it seems to move on to the next until it gets through the whole sequence, I found I had to flip the order so some of the properties I was setting at 1, didn’t cause 2 to immediately evaluate to true as well. In my particular case just checking the higher values first fixed the problem of it evaluating to true at every step.

Hopefully I am explaining this clearly enough hahaha

Jerimy_Brown1
February 9, 2024

I think I have an answer to this now. Originally it seemed Figma was stopping at the first evaluation of true, but they recently changed something that broke my hacks, causing me to change my sequencing to get it to function correctly again. Now it looks at each statement until it gets to the end, so you have to be careful with sequencing or you can evaluate each line to true by the time it hits the next line in the sequence. In my case I just had to swap to Highest to Lowest value ordering, instead of Lowest to Highest to get things to work again. You can see my more detailed explanation in the main thread.

gilgongo
February 9, 2024

Doesn’t really inspire confidence in Figma if they’re making basic changes like this without warning. Not a mark of quality software, if you ask me.

oscargodson
New Member
March 4, 2024

Running into this now. Creating a prototype where im letting the demoer whos using the prototype can show different flows depending on the user type (like admin vs non-admin). I want to be like if admin AND featureEnabled do X, else if admin and featureDisabled do Y else if notAdmin…etc I can string together ifs but agree it feels hacky and also I feel like I’m gonna end up with a hole or opposite where more than 1 if evaluates by accident or something.

April 12, 2024

[MY SIMPLE SOLUTION]

I thought about it as a coder and came up with elegant solution.
The steps are following:

  1. create a condition frame
  2. IF var==1 then go to Frame A; ELSE go to condition frame.
  3. on condition frame set up interaction: delay 1ms and then condition:
    IF var == 2 then go to frame B; ELSE go to frame C.
  4. profit.
    Optional: this way you can create unlimited ELSEIFs. 🙂
Candin
New Member
July 7, 2024

really work!!

Marcus_Harvey
New Participant
July 10, 2024

Agree. We really, really need ‘else if’

Emma_Chelberg1
September 16, 2024

I am just going to add my thoughts on this thread as well. I really hope this gets added to Figma because I am currently trying to do a loop with a variable and just using multiple If statements does not work for me.

Basic functionality is a carousel type situation so variable starts at state 1:

  • if {variable} = state 1 → set to state 2
  • if {variable} = state 2 → set to state 3
  • if {variable} = state 3 → set to state 1

If I leave it as is above it looks like nothing happens because it cycles through since it runs each statement. If I change the order to check for state 3, then state 2, then state 1 it works the first time through but when it comes to the last state (3 to 1) it ends up skipping state 1 (see below) so that still doesn’t solve my problem.

  • if {variable} = state 3 → set to state 1
  • if {variable} = state 2 → set to state 3
  • if {variable} = state 1 → set to state 2

I really hope we can get else if statements or some way to jump out of the loop so it wouldn’t run the statements afterwards. I wish more updates were made to the prototyping side of Figma at ConFig, hopefully next year they will add some cool new functionality.