Conditional commands in Zanscript


When you only want a command (or series of commands) to execute when a certain condition is met, you need to use conditional statements. 

The most commonly used one of these is the IF statement. An IF statement allows you to place prerequisites in your script in order to run a specified block of commands. 

Top

Setting Variables


Before using any of the conditional commands below, variables need to be defined. Whilst not a requirement, it is good practice to define variables at the top of the script so they can be easily edited. 

Variables are name placeholders for values which are called up later in the script. Zanscript can define variables to be used in two ways: one for assigning integer values, such as seconds or repeats: (e.g., DEFINE TIME_BIN 1 is setting the name TIME_BIN to a value of 1). The other is setting a variable as a name for use in a Boolean expression commands (e.g., IF x = true, then y, ENDIF commands).

DEFINE INIT_TIME 180     # time allowed to initiate the trial to start
DEFINE CHOICE_TIME 90    # time allowed to make a choice response
 
DEFINE TRIAL_NUMBER 101
DEFINE TOTAL_CORRECT 102
DEFINE TOTAL_NO_CHOICE 103
  • The names of variables can be any letter character with the exception of other  command names used within Zanscript syntax (i.e., LIGHTS, WAIT). These other commands are listed in the syntax manual.
  • The number assigned to the variables used in a Boolean expression can be any number between 0-899.

These variables can now be placed into the appropriate ACTIONs. 

Top

IF...ENDIF statement

An 'IF' statement checks the variable's condition status at the time it is run. The status can be true = 1 or false = 0. The variable's condition must be able to be evaluated to 'true' or 'false'. If the variable's condition is true, Zanscript executes the commands between IF and ENDIF. If not, the commands are ignored and Zanscript continues on the next command after the 'IF' statement.

IF variable condition 
    {commands}
ENDIF

IF: States the variable condition which needs to be met in order to run the subsequent block of commands. For example, IF @VARIABLE=TRUE.

ENDIF: Ends the block of commands started by the IF statement.

The condition in the 'IF' statement can take the form of:

  • = equal to
  • > less than 
  • > greater than
  • <= less than or equal to
  • >= greater than or equal to 

Example of IF...ENDIF

ACTION EXAMPLE
 
@CORRECT_RESPONSE=TRUE
 
IF @CORRECT_RESPONSE=TRUE
    LIGHTS(LIGHT16,RED)
    WAIT(4)
ENDIF
 
COMPLETE

In the ACTION above, the variable, @CORRECT_RESPONSE, has been set to true (=1). The IF...ENDIF statement occurs and asks if the status of the variable is true. Since it is true, the commands to turn LIGHT16 to red and wait 4 seconds are executed.

Top

IF...ELSE...ENDIF statement

An IF statement can be followed by an optional ELSE statement, which runs when the variable's condition is false. When the variable's condition is true, the commands between the IF and ELSE are run and the commands between the ELSE and ENDIF are ignored. If the variable's condition is FALSE, the commands between ELSE and ENDIF are run instead. Once the relevant set of commands is executed, the Zanscript execution will continue from the line after the 'ENDIF'.  

IF variable condition
    {commands}
ELSE
    {commands}
ENDIF

ELSE command: carries out a block of commands if the condition in the IF command is not met.


Example of IF...ELSE...ENDIF

ACTION EXAMPLE
 
@CORRECT_RESPONSE=0
 
IF @CORRECT_RESPONSE=1
    LIGHTS(LIGHT16,RED)
    WAIT(4)
ELSE
    LOAD(AUDIO_FILE,"tone.wav") 
    WAIT(5)
ENDIF
 
COMPLETE

In the ACTION above, the variable @CORRECT_RESPONSE has been set to false (=0). When the IF statement is run and asks if the status of the variable is true, since it is not, it will execute the commands following the ELSE and play the audio file "tone.wav" and wait 5 seconds.

Top

IF...ELSEIF...ENDIF statement

It is possible to use an 'ELSEIF' command and specify another condition. If the first variable's condition is true, the code between the 'IF' and 'ELSEIF' will be executed, otherwise if the 'ELSEIF' condition is met, the commands between 'ELSEIF' condition and 'ENDIF' will be executed. If neither are found to be true, the Zanscript moves on to the command after the ENDIF. You can use any number of ELSEIF conditions within an IF statement.

IF variable condition
    {commands}
ELSEIF second variable condition
    {commands}
ENDIF

ELSEIF command: runs a block of commands based on the second variable's condition status, if the condition in the IF command is not met.


Example of IF...ELSEIF...ENDIF

ACTION EXAMPLE
 
@CORRECT_RESPONSE=0
@INCORRECT_RESPONSE=1
 
IF @CORRECT_RESPONSE=1
    INVOKE(REWARD)
ELSEIF @INCORRECT_RESPONSE=1
    INVOKE(CORRECTION_TRIAL)
ENDIF
 
COMPLETE

In the ACTION above, the variable @CORRECT_RESPONSE, has been set to false (=0) and the variable @INCORRECT_RESPONSE has been set to true (=1). The IF statement is asking for the status of two variables. Firstly, it asks if @CORRECT_RESPONSE is true, which it is not. Therefore, Zanscript skips the commands following the IF statement and then asks if @INCORRECT_RESPONSE is true, which is it. Zanscript will execute the command following the ELSEIF and invoke the ACTION CORRECTION_TRIAL. 

If both condition statuses are false, Zanscript will move onto the next command in sequence. 

Top

WHILE...ENDWHILE statement

WHILE...ENDWHILE statements executes a set of commands repeatedly, as long as the variable's condition is continuing to be met. If the variable's condition is false when the WHILE statement is first run, the subsequent commands are not run. Zanscript checks the WHILE condition and if the condition is still true, the process is repeated.

WHILE variable condition
    {commands}
ENDWHILE

WHILE... ENDWHILE is used when a set of commands need repeating an indefinite number of times, as long as the variable's condition remains met. 


Example of WHILE...ENDWHILE

DEFINE LARGEST_ANIMAL_SIZE 7
 
DEFINE ANIMALS_SIZE 101
DEFINE THRESHOLD 102                
 
 
ACTION TRACKING_SETTINGS_SEQUENCE

@ANIMALS_SIZE= 1
 
WHILE @ANIMALS_SIZE < LARGEST_ANIMAL_SIZE 
        SET(TARGET_SIZE,@ANIMALS_SIZE)
        @THRESHOLD= 0.0
        TARGETMARKER(@ANIMALS_SIZE,1,1)
ENDWHILE
 
@ANIMALS_SIZE= @ANIMALS_SIZE + 1
 
COMPLETE

In the example above, the term LARGEST_ANIMAL_SIZE has been set to the value of 7. The terms @ANIMALS_SIZE and @THRESHOLD have been set to variable  numbers allowing them to be updated within the running of the script. In the ACTION, the variable @ANIMALS_SIZE has been set to the value of 1.

While @ANIMALS_SIZE continues to be less than the value set for LARGEST_ANIMAL_SIZE (7), the commands prior to the ENDWHILE will be repeatedly executed: set target size to the value of @ANIMALS_SIZE, set @THRESHOLD to 0 and set the target marker to the value of @ANIMALS_SIZE. 

Once the entire WHILE...ENDWHILE statement is run, the variable @ANIMALS_SIZE is increased by one. The sequence can then be repeated as long as @ANIMALS_SIZE remains less than 7. Once it reaches this limit, Zanscript will move on to the next commands.

Top

Script download

To download the target_size_det_threshold_testing demo script as a .zs file (file type Zantiks software reads), choose the Save File As option in the right-click dialogue box. Clicking on the script name hyperlink will open a read-only version of the script.

Script download: target_size_det_threshold_testing.zs