Looping

Looping in programming refers to the ability of the program to iterate through a set of steps repeatedly until a break condition has been met.
Inquisit provides the following two looping set ups:

  1. While loops
  2. For loops

1. While loops

The while statement provides the most basic way to program a loop. A while loop basically follows the structure 'while (this condition is still true) {do this}'.

While loops are particular useful for situations when it's uncertain how many times a loop will have to be repeated. However, because while loops can be used when it's uncertain how many loops have to be run, it's important to avoid 'infinite' looping. Thus all while loops need an emergency break so that the program can continue.

Consider the following example in which an ordered list of response latencies is checked against a maximum allowed response time. All latencies faster than the maximum allowed response time are counted as valid. Once the first invalid latency is found, looping should stop. Furthermore, in case all latencies are valid, the loop should stop if there are no further latencies to check.

<trial looping>
/ onTrialBegin = {
	var repeatLoop = true; //set the variable to 'true', so that looping can start
	while (repeatLoop){//as long as 'repeatLoop' stays 'true' another loop begins
		if (list.latencies.nextValue > parameters.maxAllowedRT || list.latencies.unselectedCount <= 0){ //when the first latency larger than the maximum allowed response time is found OR all response times have already been checked, the looping conditions needs to be set to false to stop the loop
			repeatLoop = false; //we have reached the end for the loop
		} else 
			values.validResponseTimeCounter ++;
		}
	}
}
/ stimulusFrames = [1 = validResponseCounts]
/ validResponse = (" ")
</trial>	

2. For loops

For loops are the preferred way for setting up loops when the number of loops is known (or at least retrievable) at the onset of the loop. Consider the following example, in which item.correctResponses stores either "A", "B", "C", or "D" as correct multiple choice responses for a problem set. The goal is to count the number of correct A,B,C, or D responses and store the result in values.countA/values.countB/values.countC/values.countD, respectively. The implemented for-loop is set up to run as often as there are items in item.correctResponses (stored in property 'itemCount').

<trial looping>
/ onTrialBegin = {
	for (var i = 0; i < item.correctResponses.itemCount; i++){ //a counter variable 'i' is set to a start value of 0. As long as the next i is still smaller or equal to the 'itemCount' at the start of each loop, counter 'i' increases by one.		
		values["count" + item.correctResponses.item(i)]++; 
	}
/ stimulusFrames = [1 = myText]
/ validResponse = (" ")
</trial>