|
ANY-maze Help > The ANY-maze reference > The Protocol page > The elements of a protocol > Testing > Procedures > Elements of a procedure > Statements available to procedures > The If statement The If statement
IntroductionThe If statement, or 'Conditional' statement, will take one of a number of actions depending on whether a certain condition is true. If statements with a single conditionThe simplest form of the conditional statement will check one single condition, and take appropriate action if that condition is true. For example, the following example procedure will end the test if the animal does not enter a specific zone for 30 seconds.
Figure 1. This simple If statement will wait for either a zone entry, or a specific time to elapse. It uses an If statement to see which event it was that occurred, and if it was the Time elapsed event, the test is ended.
To do this, it must wait for one of two things to happen - either the animal enters the zone, OR 30 seconds elapse without the animal entering the zone. We can use a logical 'OR' operator to wait for one of two events to happen, but then we are left with the situation that we need to take a different action depending on which event actually occurred. This is where the If statement comes in. The If statement allows the procedure to check which event did actually occur, and then run a different set of statements accordingly. Conditional statements with multiple conditionsConditional statements can have any number of conditions, which they evaluate one after the other, in order. When the procedure reaches the first condition that evaluates to True, the statements within that section of the If statement will be run. If statements can also have a 'catch all' condition called 'Else', which contains code that runs if none of the other conditions are true. The following example shows this:
Figure 2. This If statement evaluates a number of conditions; if none of these conditions are true, the statements in the Else clause will run.
More informationA common mistake when writing a procedure is to use an If statement instead of a Wait until statement when waiting for an event to occur. When you're deciding what you need a procedure to do, you might think to yourself 'Well, if the animal enters the island, I want to end the test'. So you drag in the If statement, and drag in the Animal enters Island zone event as its condition.
Figure 3. This procedure is incorrect (and an error will be generated when the procedure is checked for errors, or when you exit the procedure editor).
However, this isn't what an If statement is designed for. An If statement is used to evaluate a specific condition, at a single point in time in the procedure - so the chance of the procedure running the If statement at the exact point that the Animal enters Island zone event occurs is pretty slim! When thinking about writing a procedure, you need to consider what the procedure is going to do, and remember that the procedure will just run through each of its statements in turn until it has finished. In this example, it's going to do nothing until the animal enters the island - i.e. it's going to Wait until the animal enters the island - before continuing. Adding further conditionsWhen you drag an If statement into the ANY-maze procedure editor, it will contain a single condition and space for the statements to execute if that condition is true. To add further conditions, select the drop-arrow next to the 'End' at the bottom of the statement, and select whether you require another condition ('Else if'), or a catch-all condition ('Else') if no other conditions are valid.
Within the procedure editor, if you need to remove a condition from an If statement, just right-click on the 'arm' of the condition (where the 'Else if' or 'Else' text is displayed) and you'll see a menu appear. Select the last item, 'Delete this condition' to delete the condition. Note that this will also delete all the statements contained within this condition! If you don't want this to happen, select multiple statements using the left-hand margin of the editor and drag those statements out of the If statement, to somewhere else in the procedure, before deleting the condition.
If statements can 'contain' any number of statements within each condition. This can include further If statements or Repeat statements, and these in turn can contain If statements or Repeat statements, 'nested' to any level. Within the ANY-maze procedure editor, dragging an If statement will also drag all its contents. Similarly, deleting the statement will also delete all its contents, in all its clauses.
See also:
© Copyright 2003-2026 Stoelting Co. All rights reserved ANY-maze help topic T0481 |