AssignmentStatements
variableName
assignmentOperator
expression;The assignment statement updates the value of a variable based upon an assignment operator and an expression (and for +=, -=, *=, /= and %= also the current value of the variable). For example:
JScript example:
var x : int = 12;
var y : int;
y = x – 2;
JavaScript example:
var x : int = 12;
var y : int;
y = x – 2;
assigns the value 10 to y (if x is 12 as in the previous example).
if Statement
You can use an if statement to control the flow of your code based on a logical expression. If the expression gives the Boolean value true, the statements are executed, whereas if the expression gives the Boolean value false they are skipped. If you use an else branch, the statements inside the else branch are executed if the expression gives the value false.
if
if( condition )
{
statements
}
If the expression inside the parenthesis is true, the statements inside the curly brackets, { and }, are executed. If the condition is false, then the statements are skipped and execution continues on the next program line.
Note that the parentheses around the condition are required.
If-then conditions are often used when creating scripts in Forsta Plus, especially for validation code.
We have used if conditions several times already. See the examples in Validation Code, Null and toBoolean for more information.
if else
if( condition )
{
statements
}
else
{
statements
}
If the condition inside the parenthesis is true, then the first set of statements is executed. If the condition is false, then the second set of statements is executed.
What values will x and y have after running these two code samples (separately):
Code sample 1
if(x<y)
{
x++;
}
else
{
y++;
}
Code sample 2
if(x<y)
{
x++;
}
y++;
…when x and y are declared as
JScript example:
var x : int = 4; var y : int = 5;
JavaScript example:
var x = 4; var y = 5;
…when x and y are declared as
JScript example:
var x : int = 5; var y : int = 4;
JavaScript example:
var x = 5; var y = 4;
Using Curly Brackets in if Statements
Curly brackets are used to group a set of statements. If you have just one statement that should be executed inside an if statement, you may omit the curly brackets {}. The following is equivalent to sample 1 above:
if(x<y)
x++;
else
y++;
However, it is easy to make mistakes when you do not use the brackets.
if(x<y)
x++;
y++;
is equivalent to sample 2 in the previous exercise, but that might be hard to spot. So it is recommended to always use the curly brackets, both for clarity and also to simplify later modifications such as adding another statement within a branch in the if statement.
switch Statement
The switch statement is used when you want different statements to run for different values of a variable. Instead of writing a lot of if statements with conditions for each of the values you want to check, you can use switch. The syntax for the switch statement is like this:
switch (expression)
{
case value1:
statements1
break
.
.
.
case valuen:
statementsn
break
default:
statementsx
}
You may have more than one statement between each case and break, and do not need to have curly brackets in front and after them, since for each case all the following statements will be executed until the break statement.
switch evaluates the expression and looks at the values one by one until a match is found in one of the case statements. When a match is found, the accompanying statements are executed until a break statement is encountered or the switch statement ends. If you omit the break statement before the next case, the following statements will also be executed until a break is reached or you get to the end of the switch statement.
Use the default clause to provide a statement to be executed if none of the values matches the expression.
If no value matches the value of expression, and a default case is not supplied, no statements are executed.
The switch statement above will be equivalent to this code:
if(expression == value1)
{ statements1
}
...else if(expression == valuen)
{
statementsn
}
else
{
statementsx
}
Using switch to set Values for each of the Answer Alternatives on a Single
Let us say we have a single question where the respondent picks from a list of different concepts. Each of these has a price, and we want to set the price in a hidden numeric question based on which of the concepts the respondent has picked. The value in the hidden numeric question may e.g. be used in calculations later in the questionnaire. The single question has question ID concept and the numeric question has question ID price.
Here are two different ways of scripting this; the first using if and the second using switch. As you see, switch gives more compact code that is easier to read:
//using if:
if(f("concept")=="1")
{
f("price").set(234);
}
else if(f("concept") == "2")
{
f("price").set(249);
}
else if(f("concept") == "3")
{
f("price").set(244);
}
else if(f("concept") == "4")
{
f("price").set(229);
}else
{
f("price").set(271);
}
//using switch:
switch(f("concept").get())
{
case "1":
f("price").set(234);
break
case "2":
f("price").set(249);
break
case "3":
f("price").set(244);
break
case "4":
f("price").set(229);
break
default:
f("price").set(271);
}