Check out the LimeSurvey source code on GitHub!
Welcome, Guest
Username: Password:

TOPIC: "Else if" in Expression Manager?

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

  • Steve
  • Steve's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 493
  • 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: 4 years 6 months ago by Steve.
The administrator has disabled public write access.

"Else if" in Expression Manager? 4 years 6 months ago #72076

  • TMSWhite
  • TMSWhite's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 758
  • Thank you received: 83
  • Karma: 37
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...p_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.

"Else if" in Expression Manager? 4 years 6 months ago #72081

  • Steve
  • Steve's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 493
  • 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: 4 years 6 months ago by Steve.
The administrator has disabled public write access.

"Else if" in Expression Manager? 4 years 6 months ago #72113

  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5974
  • Thank you received: 366
  • Karma: 260
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.

"Else if" in Expression Manager? 4 years 6 months ago #72136

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

"Else if" in Expression Manager? 4 years 6 months ago #72144

  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5974
  • Thank you received: 366
  • Karma: 260
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.

"Else if" in Expression Manager? 3 years 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.

"Else if" in Expression Manager? 3 years 10 months ago #86158

  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5974
  • Thank you received: 366
  • Karma: 260
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.

"Else if" in Expression Manager? 1 year 6 months ago #116004

  • Hiwi_PE
  • Hiwi_PE's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 10
  • Karma: 0
I just imported your suggestion an got the following result:



Obviously the signs have changed somehow.

Are there any ideas for a reason?

Thanks a lot!
Last Edit: 1 year 6 months ago by Hiwi_PE.
The administrator has disabled public write access.

"Else if" in Expression Manager? 1 year 6 months ago #116005

  • Mazi
  • Mazi's Avatar
  • Offline
  • LimeSurvey Team
  • Posts: 5974
  • Thank you received: 366
  • Karma: 260
This is a known issue at older versions, see manual.limesurvey.org/Expression_Manager#HTML_editor_issue

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.

"Else if" in Expression Manager? 1 year 6 months ago #116006

  • Hiwi_PE
  • Hiwi_PE's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 10
  • Karma: 0
Thank you for your fast and helpful reply!
The administrator has disabled public write access.

"Else if" in Expression Manager? 1 year 6 months ago #116140

  • xatarov
  • xatarov's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 5
  • Karma: 0
I have the same saying as TSMWHITE.....well done
Graduated from Soran University with First Class Degree with Honours in Computer Science.
The administrator has disabled public write access.

"Else if" in Expression Manager? 4 weeks 17 hours ago #138674

  • JosB
  • JosB's Avatar
  • Offline
  • Fresh Lemon
  • Posts: 1
  • Karma: 0
Have been struggling with this elementary problem for whole day. Hope someone can point me in the right direction. Using 2.5+
In a report we calculate successfully:
Saha dev level {sum(qG01.value,qG02.value,qG03.value,qG04.value,qG05.value,qG06.value,qG07.value,qG08.value,qG09.value)}

I need to use this result in an Else If expression. Tried to place the {sum... above in the expression without joy. Now, am trying INSERTANS but EM does not like it. Tried various ways around with { and with ( and without.

{if {INSERTANS:528299X27X9} < 3, 'This is blocked', if (INSERTANS:528299X27X9 >= 4 && < 8, 'This is normal?')}

What am I doing wrong here?
Thank you
Last Edit: 4 weeks 17 hours ago by JosB. Reason: added question
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.308 seconds
Imprint                   Data Protection Statement                  Revocation information and revocation form