Hidden Values Example 3: If Statements

Now that you are an advanced user of Hidden Values, let's try to do something more complicated!

Sometimes you want to write a complex condition. Imagine in our application, we want to create a property called pregnancy_risk where we can keep track of whether a pregnancy is high risk or low risk. Maybe there are a lot of ways that this can be affected but we just want to have one property where we can see at a glance the woman's "status."  For the purposes of the form we have created let's imagine the following:

If the woman has more than 5 living children and if she is feeling sick, then we want to consider her risk to be "high" but otherwise it should be "low." This would be really confusing to do using normal questions unless we just ask the mobile user to manually figure it out and choose the status. Sometimes we want to automate it!

To do this we can use an "if" statement. If statements are always in the same format:

if(a,b,c)

This means if a is true, then the answer is b, otherwise/else (i.e. if a is not true), then the answer is c.

So for our example, we want the expression to be:

if total_children is greater than 5 and feeling_sick = 'yes' then the value of status should be 'high_risk' and otherwise it should be 'low_risk'.

  • Create a hidden value by clicking on 'Add Question' followed by Hidden Value
  • Set the Question ID to "pregnancy_risk" 
  • Create a Calculate Condition like this:

if(#form/total_children > 5 and #form/feeling_sick = 'yes','high_risk','low_risk')

If that's confusing, break it down into pieces. There are two conditions (number of children and feeling sick) and if those are both true, then we assign one value to the hidden value pregnancy_risk, and if those two conditions are not both met, we give a different value to pregnancy_risk.

You can use this same general principle to create a lot of powerful calculations.

Argh! I can't figure out what the mistake I made is!

You might get a generic error message and not understand why your complex statement is not working. Here are some common sticking points:

  • Check all of your parentheses and quotation marks! They must all be in pairs. If you can't find it, try copying the statement into a tool like Notepad++ or Sublime Text as these tools can help you find missing parentheses
  • Check your spellings!
  • Drag questions from the question tree to make sure you have the right path or you can add it by typing #form/ and selecting the correct question or reference
  • Anytime you use "and" or "or" they must be lowercase

 

In the last section of the Hidden Value tutorial, we will demonstrate how to show the result of a calculation in a hidden value.

Go to the next section

  • No labels