Welcome, Guest
Username: Password: Remember me

TOPIC: "Else if" in Expression Manager?

"Else if" in Expression Manager? 2 years 6 months ago #72042

  • Steve
  • Steve's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 494
  • Thank you received: 46
  • Karma: 17
Hello,

I'm not sure if it is or would be possible to have an
if (a == 1) 'cool'
else if (a == 2) 'not so cool'
else if (a == 3) 'uncool'
else 'I don't know'

I know you can use "if(test,result_if_true,result_if_false)" but i seems to get complicated when you have more than one else part.

I tried
Wow {if(!is_empty(name),implode(' ', ', ', name, ', '),'' )} {if(!is_empty(age),if (age < 5, 'You are kind of young',if (age >= 5 && age < 10, 'Is kindergarden or school fun?',if (age >=10 && age < 18, 'Moah, I want to be an adult right now!', 'You are already an adult!')),'')}
but EM told me
"Parentheses not balanced; Conditional processing; if(test,result_if_true,result_if_false)"
perhaps there is something missing.
Mit freundlichen Grüßen/Best regards,

Stefan Gohlke
LimeSurvey Team
Last Edit: 2 years 6 months ago by Steve.
The administrator has disabled public write access.

Re: "Else if" in Expression Manager? 2 years 6 months ago #72076

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
vierundzwanzig-

I'm glad to see you're putting EM through its paces.

if/elseif/else clauses can be a pain to deal with in recursive descent parsers, so I didn't try to support them. Yes, when nested if(test,true,false) clauses get deep, they can be a bit of a pain to debug, but if you count parentheses, you can get them right pretty quickly.

Also, the error checker will tell you if you have extra right parentheses. So, unbalanced parentheses mean you're missing at least one right parentheses (perhaps I should update the documentation).

Regardless, you were missing a right parenthesis before the empty string at the end of the if().

Attached is a working version of your test.

File Attachment:

File Name: limesurvey_group_33.lsg
File Size: 13 KB


And here is the corrected expression
Wow {if(!is_empty(name),implode(' ', ', ', name, ', '),'' )} {if(!is_empty(age),if (age < 5, 'You are kind of young',if (age >= 5 && age < 10, 'Is kindergarden or school fun?',if (age >=10 && age < 18, 'Moah, I want to be an adult right now!', 'You are already an adult!'))),'')}

Once we have a GUI for EM, I expect we'll try to support parenthesis matching - so if you select a parenthesis, it will highlight the matching one.

/Tom
The administrator has disabled public write access.

Re: "Else if" in Expression Manager? 2 years 6 months ago #72081

  • Steve
  • Steve's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 494
  • Thank you received: 46
  • Karma: 17
Thank you, I added the missing parenthesis and it now works.

Parenthesis matching would be kind of nice. Even if the GUI isn't available now, would it be possible to check if the expression is correct when you leave/move out/hover out the text filed (or even before saving a group/question)? If you want to check if your expression works correctly you have to save the question first, then check if it's correct and if not to fix it.
Mit freundlichen Grüßen/Best regards,

Stefan Gohlke
LimeSurvey Team
Last Edit: 2 years 6 months ago by Steve.
The administrator has disabled public write access.

Re: "Else if" in Expression Manager? 2 years 6 months ago #72113

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5301
  • Thank you received: 291
  • Karma: 247
Good ideas, vierundzwanzig.

I also think that we should extend the EM documentation by adding such forum examples and some .LSS files at the wiki. This should help other users a lot.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Re: "Else if" in Expression Manager? 2 years 6 months ago #72136

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
Good idea. I added a variant of vierundzwangig's example to the wiki here.
The administrator has disabled public write access.

Re: "Else if" in Expression Manager? 2 years 6 months ago #72144

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5301
  • Thank you received: 291
  • Karma: 247
Thanks!

@others: If you find further examples, please extend the above wiki page.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Re: "Else if" in Expression Manager? 1 year 10 months ago #85716

I have a problem to with a nested if

if(calc_final=6.5, if(finalgrade_thesis= > calc_final, ceil(calc_final), floor(calc_final)), "test")

As you can see it is a test run. However it states I have a comma to much ( after the 6.5)

I have also tried the && function but in that case it states that the if function is not valid.

The calc_final and finalgrade_thesis are working and valid variables (fieldnames)

Hope you can help me out

The function is working in excel ( but then more extended for several situations.
See below
=ALS(I20=5.5;ALS(D15>=I20;AFRONDEN.NAAR.BOVEN(I20;0);AFRONDEN.NAAR.BENEDEN(I20;0));ALS(I20=6.5;ALS(D15>=I20;AFRONDEN.NAAR.BOVEN(I20;0);AFRONDEN.NAAR.BENEDEN(I20;0));ALS(I20=7.5;ALS(D15>=I20;AFRONDEN.NAAR.BOVEN(I20;0);AFRONDEN.NAAR.BENEDEN(I20;0));ALS(I20=8.5;ALS(D15>=I20;AFRONDEN.NAAR.BOVEN(I20;0);AFRONDEN.NAAR.BENEDEN(I20;0));ALS(I20=9.5;ALS(D15>=I20;AFRONDEN.NAAR.BOVEN(I20;0);AFRONDEN.NAAR.BENEDEN(I20;0));AFRONDEN(I20;0))))))
The administrator has disabled public write access.

Re: "Else if" in Expression Manager? 1 year 10 months ago #86158

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5301
  • Thank you received: 291
  • Karma: 247
Please attach your survey or question group for testing.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.230 seconds
Donation Image