Question Type | Calculations
Previous Topic  Next Topic 

A special type of question, to help with calculations & costing, is available in the Enterprise version of PocketSurvey.

Expressions allow computations to be performed between basic types and return the result in as the answer. Allowable computations are addition (+), subtraction (-), multiplication (*), division (/), and power (^). These operators may be used with most basic types of data variables, but certain pairings may produce run-time errors, for example multiplying a string by 2 ("fred" * 2)

Parentheses may be used to override normal operator precedence. If you have any doubt about precedence when constructing an expression then use parentheses. The expression editor will remove any unnecessary bracketing.

Variables selector

Selecting the Variables pull-down list in the expression helper will display a list of available variables. Selecting from this list will insert the variable at the current cursor position in the expression editor.

If you wish to use a variable from another section, use the Section pull-down list to firstly select a section then use the Variables pull-down to select a variable from that section.

The PocketSurvey calculator statement allows arithmetic operations to be carried out not only on numbers but also on text strings and lists.

Numeric arithmetic

Arithmetic operations on numbers behave as expected. Infix notation of the basic form <operand> <operator> <operand> is used, e.g. 1 + 2.

All the usual arithmetic operators and functions are available:



Evaluation Priority
















Natural logarithm



Order of evaluation

With operators of the same priority the normal order of evaluation is from left to right. Operators with a higher evaluation priority will be evaluated first.


All numbers are held internally as real numbers. Any calculations performed on them will produce results accurate to 12 significant digits. In the case of fractional results this can produce numbers with a large number of digits after the decimal point, e.g. 355 / 113 will produce 3.14159292035. The function 'round' can trim to any desired number of decimal places. Its use is:

        round ( number , decimalplaces )

number and decimalplaces can be variables.

Automatic removal of parentheses

Parentheses may be used to change the order of evaluation, i.e. anything within parentheses will be evaluated before anything outside of them. For instance, if we wish the addition to be carried out before the multiplication in the following expression:


we would use parentheses so:


If you are in any doubt as to what will be evaluated first, then use parentheses. PocketSurvey's expression editor will remove them if they prove to be unnecessary.

String arithmetic

Some arithmetic operators are overloaded to enable them to perform operations on text. Text operators are:





<text1> + <text2>

Joins <text1> to <text2>


<text1> - <text2>

<text2> is removed from <text1>

Head removal

<text1> * <num>

The first <num> characters are removed from <text1>

Tail removal

<text1> / <num>

Only the first <num> characters are retained from <text1>

List arithmetic

Some arithmetic operators are overloaded to enable them to perform operations on lists. List operators are:





<list1> + <list2>

Joins <list1> to <list2>


<text> + <list>

Adds <text> to the beginning of <list>

Head removal

<list> + <text>

Adds <text> to the end of <list>

Tail removal

<list> * <num>

Returns the <num>th item in <list>

Conditional expressions

An expression can be made to produce different results based on pre-defined conditions. This is done by using the IF...ELSE expression.

The format is:

IF ( condition ) expression ELSE expression

condition can be any valid Boolean expression (see Conditional Jump for examples).

If you have done any programming, you may be used to using IF statements without the ELSE option; this is not available here. An IF is always accompanied by an ELSE. The reason for this is that IF...ELSE is not a statement - it is an expression; therefore it must always produce a result. If we allowed IF to be used on its own then there could be conditions that would produce no result.

Because IF...ELSE is an expression it can be nested inside itself, e.g.

IF ({{Score}} < 5) "Poor"

ELSE IF ({{Score}} < 10) "Acceptable"

ELSE IF ({{Score}} < 15) "Good"

ELSE "Excellent"

It can also be embedded within a regular non-conditional expression, e.g.

{{UnitCost}} * IF ({{IncludeTax}}) 1.20 ELSE 1.0

Be careful when forming the final ELSE expression, you may not get the results you expected, e.g.

IF ({{Answer}} = "Poor") 2

ELSE IF ({{Answer}} = "Average") 4

ELSE IF ({{Answer}} = "Good") 6

ELSE {{Score}} / 2

The above example will divide all possible results by 2. If the intention is to divide only {{Score}} by 2 then the last line should be bracketed so:

ELSE ( {{Score}} / 2 )

Running an external program

You can run other programs from PocketSurvey by using the 'execute' function; this has two forms:

        execute (ProgramName, CommandLine)


        execute (ProgramName, CommandLine, Action)


ProgramName is the name of the program that you wish to execute (do not include the '.EXE' extension), e.g. "calculator".

CommandLine is any command line arguments that the external program may use.

If the 2-argument version of execute is used then the named program will run concurrently with PocketSurvey. To synchronise execution you may need to use the third argument:

Action may be either stop, kill, or wait.

kill will terminate the named program without notice. Only use this if the named program does not respond to stop.

stop will instruct the named program to stop if it is already running. This is a safer option than kill as the named program will have the opportunity to close files etc before terminating.

wait will cause PocketSurvey to wait until the named program has terminated.

All arguments must be enclosed by double-quotes.

Variable types - auto typing

In most programming languages variables have to be formally defined before they are created or referred to. Part of this definition usually includes a description of the variable's type; such as integer, real, text, etc. Because PocketSurvey tries to avoid formal programming and its complications, it deduces the variable's type automatically. It uses a set of internal rules to do this but, as with most labour saving devices, there is a trade-off: it never does quite as good a job as a human being - it sometimes gets it wrong. To avoid this always make sure that your text strings start with a letter, starting with a number will cause them to be interpreted a numeric types and will not produce the results you may have expected.


This is an advanced question type that is only available if you have purchased an enterprise or advanced license for PocketSurvey.

HandHeld Systems Ltd ©