Conditional statements provide a basic programming tool for defining and adapting the behavior of a script to different outcomes. For example, your program may have to account for different responses participants may give and provide distinct actions for the script to carry out in each case. The following is an overview of the three main ways to implement out conditional logic in Inquisit:
For a brief overview of the comparison operators (e.g. to test for equality) to use in conditional statements in Inquisit, click here.
For a brief overview of conditional conjoining operators (e.g. AND and OR) to use in conditional statements in Inquisit, click here.
Simple if statements can be used in Inquisit to execute code if certain conditions are met. All if statements consist of 1) a conditional statment that is evaluated as true of false, and 2) one or more statements that are executed if the conditional statement is true. Sequential if-statements are evaluated one-by-one, independently from each other.
In the following example, four survey questions are checked for responses. Each question is checked separately and the responseCounter is updated if the condition (here: whether a response was recorded for a particular question) is evaluated to be true and only then. Note that the conditional statements use the comparison operator '!=' which translates to 'not equal to' (check here for additional comparison operators). Furthermore, it's safest to always use {} to enclose all commands that should be carried out for each if statement, particularly if more than one action should be taken by the computer.
/ onTrialEnd = {
if (radioButtons.q1.response != ""){//was q1 responded to and thus is not empty anymore? If so increase responseCounter by 1
values.responseCounter ++;
}
if (radioButtons.q2.response != ""){//was q2 responded to and thus is not empty anymore? If so increase responseCounter by 1
values.responseCounter ++;
}
if (radioButtons.q3.response != ""){//was q3 responded to and thus is not empty anymore? If so increase responseCounter by 1
values.responseCounter ++;
}
if (radioButtons.q4.response != ""){//was q4 responded to and thus is not empty anymore? If so increase responseCounter by 1
values.responseCounter ++;
}
}
Simple if...else or if...else if...else statements can be useful in cases where only one
condition to be evaluated as true needs to be found (if at all) amongst a list of alternatives. The alternative options
are added to the if conditional statement in the form of 'else if' or 'else' statements with 'else' being the catch-all
action statement for any not further specified outcomes that should be carried out if none of the other conditionals are
evaluated to be true.
For if...else or if...else if...else statements the program checks each condition statement in the order listed
until it finds one that evaluates to true. Once it finds a condition that evaluates to true it stops checking
the remaining conditions and carries out the specified actions. This setup can speed up execution time compared to simple sequential
if statements. Note though that using if...else if...else it would not work in the example above
as checking the responses to the four survey questions would end with the first true if statement.
In the following example, the color of a feedback stimuli is adapted from green (correct response) to yellow (no response) to red (wrong response).
<text feedback>
/ onprepare = {
if (values.selectFeedback == 0){
this.textColor = 'green';
} else if (values.selectFeedback == 1){
this.textColor = 'red';
} else {
this.textColor = 'yellow';
}
}
/ items = ("Correct", "Error", "NoResponse")
/ select = values.selectFeedback
</text>>
if and if...else if...else conditional statements can also be nested, which simply means that for each condition that is evaluated to be true, additional conditions to be checked can be added.
Check out the following example, in which a branching attribute uses a nested if...else if...else statement under a if...else statement to repeat a trial only if it was a) answered incorrectly and b) has not been repeated before. In addition, different feedback texts should be presented for repeated trials to differentiate between noresponses compared to error responses.
<trial choice>
/ onTrialBegin = {
values.trialCounter++;
}
/ stimulusTimes = (choiceA, choiceB, repeatFeedback)
/ validResponse = (choiceA, choiceB)
/ correctResponse = (choiceA)
/ timeout = 3000
/ branch = {
if (values.trialCounter < 2){ //trial has run only once: a possible repeat trial
//a nested conditional statement to check for accuracy
if (this.response == 0){ //participant did not respond at all
values.feedback = "respond faster";
return this; //repeat initiated
} else if (this.error){ //participant made an incorrect choice
values.feedback = "correct your choice";
return this;//repeat initiated
} else { //default: both error options have been checked and dismissed, what's left must be a correct choice
values.feedback = "good choice";
return trial.choiceEnd;
}
} else {//the trial has already been repeated
values.feedback = "repeat complete";
return trial.choiceEnd;
}
}
</trial>
switch statements provide an easy and fast way to provide the computer with the action items depending on the value of a single variable. In the case of switch statement, the computer does not need to check each individual case until it finds one that is true, but is immediately directed to the action item linked to a specific value.
The setup of switch statements is demonstrated in the example below in which an activity is tied to specific days of the week.
<trial myTrial>>
/ onTrialBegin = {
var day = "Monday";
switch (day) {
case "Monday": //list the different options after keyword 'case'
values.dayActivity = "zoo"; //action items linked to option 1
break;//keyword 'break' tells computer to leave the the switch statement
case "Wednesday":
values.dayActivity = "theater";
break;
case "Friday":
values.dayActivity = "museum";
break;
default: //use keyword 'default' to list the actions for any unspecified options
values.dayActivity = "rest";
}
}
/ stimulusTimes = [1=todaysActivity]
/ validResponse = (" ")
</trial>>
<trial myTrial>
/ ontrialBegin = {
var a = 1 //variable a is declared as a number variable
var b = "1" //variable b is declared as a string variable
var c = null //variable c is declared but undecided
if (a == b){//a and b are compared and will be found to be equal with the '==' comparison operator. The conditional is thus evaluated as true.
c = 1;//c is set to 1
}
}
/ trialDuration = 0
</trial>
<trial myTrial>
/ ontrialBegin = {
var a = 1 //variable a is declared as a number variable
var b = "1" //variable b is declared as a string variable
var c = null //variable c is declared but undecided
if (a === b){//a and b are compared and will be evaluated as 'NOT' equal with the '===' comparison operator. The conditional is thus evaluated as true.
c = 1;//c remains undecided
}
}
/ trialDuration = 0
</trial>
<trial myTrial>
/ ontrialBegin = {
var a = 1 //variable a is declared as a number variable
var b = 2 //variable b is declared as a number variable
var c = null //variable c is declared but undecided
if (a != b){//a and b are compared and will be evaluated as 'NOT EQUAL'. The conditional is thus evaluated as true.
c = 1;//c is set to 1
}
}
/ trialDuration = 0
</trial>
<trial myTrial>
/ ontrialBegin = {
var a = 1 //variable a is declared as a number variable
var b = 2 //variable b is declared as a number variable
var c = null //variable c is declared but undecided
if (a >= b){//This conditional is evaluated as false as '1' is neither greater NOR equal to '2'.
c = 1;//c remains undecided
}
}
/ trialDuration = 0
</trial>
<trial myTrial>
/ ontrialBegin = {
var a = 1 //variable a is declared as a number variable
var b = 2 //variable b is declared as a number variable
var c = null //variable c is declared but undecided
if (a < 10 && b < 10){//This conjoint conditional is evaluated as true as conditionals for a AND b are both evaluated as true
c = 1;//c is set to 1
}
}
/ trialDuration = 0
</trial>
<trial myTrial>
/ ontrialBegin = {
var a = 20 //variable a is declared as a number variable
var b = 9 //variable b is declared as a number variable
var c = null //variable c is declared but undecided
if (a < 10 || b < 10){//This conjoint conditional is evaluated as true as the condition is met by variable b (9 < 10), even if variable 'a' (20) is greater than 10.
c = 1;//c is set to 1
}
}
/ trialDuration = 0
</trial>