IF and IF-ELSE syntaxis for constraining an question's answer

Hello everybody,

I would like to use an IF-ELSE statement for a constraint in one of my questions. Basically, I would like to have one constraint if a specific answer was selected in a previous question, and another constraint for the others answers.

I have been looking to different posts and solutions in this forum, but neither helped me. So I am trying to understand the syntax for using IF-ELSE statements in questions constraints, but I cannot find any documentation that helps. Can anyone help?

The case would be something like this:
Q1: Name of indicator (select_one)

  1. name1
  2. name2
  3. name3

Q2. Value of indicator (decimal)
The constraint in Q2 should be the following:
If Q1=name1 , then Q2 should be between 1 and 12
If Q1=name2 or name3, then Q2 should be >0

Many thanks!

You may have a look here. Form Operators and Functions - ODK Docs and ODK XForms Specification.

Syntax is small letters: if( condition, trueCase, falseCase ).
There can be cascading ifs, i.e. replace the trueCase or falseCase by another complete if formula. Also, true() or false() can be used as value for the case. Example: if( condition01 and condition02, true(), if(condition03, true(), false()) ).
Take care of bracket balancing.

1 Like

Hi wroos,

Thank you very much! This is exactly what I needed. I can make my if statement in the constraint work now.

And thank you for the links to the documentation. Extremely useful for me.


1 Like

@wroos, @sfermepin, :clap: :heart: :partying_face: