Day 5: Variables & Calculations
Previous Topic  Next Topic 

In the last lesson we asked you to add some different question types to your survey. If you have not done that, go back and do it now before continuing with this lesson.

This lesson will show you how to do a calculation such as TOTAL = QUANTITY x COST. This is achieved by using the answers to previous questions. The CALCULATION type question uses VARIABLES.


A VARIABLE is a programming term used to represent a piece of data within a program by NAME. This NAME is then used to refer to that piece of data at other places in the program. A SURVEY can be considered to be a program and the ANSWER to every question in your survey can be referenced as a VARIABLE. The VARIABLE NAME is normally the same as the QUESTION PROMPT.

Now do the following.

    1. Create a new survey, referring to LESSON 1 if you have forgotten how to do this!

    1. At the DIAMOND insert a NUMBER question with the QUESTION PROMPT of "Cost" and a DATA DESTINATION FILE of SINGLE ITEM. You should be able to do this quite quickly by now, but refer to previous lessons if you are not sure.

    1. Then insert another number question with the QUESTION PROMPT of "Quantity".

    1. Now we are going to insert a CALCULATION question with the QUESTION PROMPT of "Total". This calculation question will refer to the previous VARIABLES of "Cost" and "Quantity".

    1. Select the Calculation TAB. The input area labelled EXPRESSION has a 0 by default, simply delete this with your keyboard. Now type in "{{Cost}} * {{Quantity}}". You can also add this EXPRESSION using the EXPRESSION HELPER where you can pick VARIABLE NAMES from pull down lists.

    1. You will notice that when we refer to a VARIABLE we need to enclose it with double braces ({{ and }}). This is because computers are not as clever as humans, so we always need to provide more detailed information for the computer to understand. The * in between the two variables is called the OPERATOR... it multiplies the contents of the two VARIABLES to provide the answer to the "Total" question.

    1. Once you have formulated the EXPRESSION correctly click INSERT to add your CALCULATION QUESTION to your survey.

    1. If you want to take things further, you could add a MESSAGE question to display the variable {{Total}}. See if you can work out how to do this yourself, but don't worry if you can't do it just yet.

    1. Now RUN your survey in the SIMULATOR. Click RECEIVE and view your survey results using the DATA DOCTOR.

The next lesson will cover another PROGRAMMING technique called LOGIC, whereby you control the flow of questions in your survey.


You can actually formulate QUESTION PROMPTS so that they contain VARIABLES. For example if you were calculating the quantity of fruit, you might have a question named  "Fruit" offering a choice of fruit in a MENU question. Then later on you ask the USER to specify a quantity of their chosen fruit by using a question prompt of "How many {{Fruit}}". When the END-USER runs the survey it will prompt them with "How many Pears". Clever stuff!

HandHeld Systems Ltd ©