Flow Control
Controlling the flow of the script is a critical component of programming. There are two major types of controlling the flow: conditional statements and loops.
Conditional Statements
Conditional statements determine or specify which part of the script should be executed based on
a condition. Contingent branching is often utilized in behavioral research and is based on whether a specific item is true or false. Specifically, If…Then statements are very commonly used to control the flow of the code.
If…Then statements simply are equated to making a choice (e.g., if I have money, then I can go to the movies). If…Then statements are the foundation of all logic. Although they seem to be very simple statements, they are quite powerful in a programming language.
There are actually 3 conditional expressions available in E-Basic. They are: If…Then, Select Case, and Do…Loop. These expressions are conditional statements simply because they perform a task based on a single true or false test. All 3 types of conditional expressions are explored in this section.
If…Then statements
The single most commonly used flow control statement is If…Then. Simply put, an If…Then statement will execute a block of code if the condition is true. If the condition is false, it will do nothing unless “Else” is used in the flow control statement.
If condition, then |
<Block of code statements to execute if the condition is true> |
End If |
Rules:
- Notice the “Then” portion of the statement is on the same line as the “If” portion. If there is a need to drop it to the next line, indicate that to the E-Basic compiler. This is done by placing an underscore (i.e., E-Basic line continuation character) at the end of the line to be continued.
- The End If statement is critical. It identifies the last statement in the block of code to be executed based on the condition.
- When the If…Then statement is only a one-line statement, the End If is not used. In fact, it will produce an error if used. In the following two examples, the code works exactly the same, but it is syntactically different.
Example 1: One-line If…Then statement
Dim a As Integer |
a = 12 |
If a > 10 Then MsgBox "A is greater than 10." |
Example 2: If…Then…End If statement
Dim a As Integer |
a = 12 |
If a > 10 Then |
MsgBox "A is greater than 10." |
End If |
If…Then…Else statements
If the program must choose between two alternative blocks of code to execute based on the conditional, then the Else statement is included.
Dim a As Integer |
a = 12 |
If a > 10 Then |
MsgBox "A is greater than 10." |
Else |
MsgBox "A is not greater than 10." |
End If |
Select Case statements
Nested If…Then statements are ideal for testing different values before executing the next block of code. Select Case statements are more appropriate for testing the same value against many different conditions.
Select Case Variable | ||
Case test1 | ||
<block of code statements to be executed if the | ||
value of variable meets test1 criteria> | ||
Case test2 | ||
<block of code statements to be executed if the | ||
value of variable meets test2 criteria> | ||
Case Else | ||
<block of code statements to be executed if the | ||
value of variable doesn't meet any of the | ||
listed Case criteria above> | ||
End Select |
The Select Case structure indirectly uses condition expressions. An expression may be A + B > C. In the example above, think of the variable being what is to the left of the operator (A + B) and test as everything to the right, including the operator (>C).
Rules:
- An unlimited number of cases may be used as test criteria.
- The Else case is optional. It is not required to be used, but can be useful in behavioral research.
Loops
The loop flow control structures are also quite commonly used in programming. They are useful when a block of code needs to be executed more than once. There are three major types of loops available in E-Basic: Do…Loop, For…Next, and For Each…Next.
Type of Loop |
Function |
Do…Loop |
Repeats the block of code until a condition is true. |
For…Next |
Repeats the block of code a specified number of times. |
For Each…Next |
Repeats the block of code for each object within a collection. |
Do…Loops
There are a variety of Do …Loops available in E-Basic.
Statement |
Description |
Do…Loop |
Repeats the block of code until a condition is true and then executes an Exit Do not End Do statement. |
Do…While…Loop |
Repeats the block of code only while a condition is true. |
Do Loop…While |
Executes the block of code once and then repeats it until the condition is false. |
Do Until…Loop |
Executes and repeats the block of code only while the condition is false. |
Do…Loop Until |
Executes the block of code once and then repeats it until the condition is true. |
Do While… Loop
The most typically used Do…Loop is the Do While…Loop. The basic syntax is:
Do While condition |
<block of statements that are executed while condition is true> |
Loop |
E-Basic evaluates the condition when it encounters a Do While statement. If the condition is true, then the block of code within the Loop structure will be executed. When it reaches the Loop statement, the entire process is repeated including re-evaluation of the condition. When the condition is false, the entire loop structure is skipped and E-Basic executes the next statement immediately following the Loop statement of the structure. In theory, no limit exists on the amount of times the block of code within the structure may be executed.
Do…Loop While
The only difference between a Do While…Loop and a Do… Loop While is the location of the condition. The Do While …Loop evaluates the condition before executing the block of code within the structure. The Do Loop…While evaluates the condition after executing the block of code within the structure. The While in this case determines if the block of code within the structure should be repeated based on the value of the condition. The major resulting difference is that a Do… Loop While will always execute the block of code at least once.
Do |
<block of statements that are executed while condition is true> |
Loop While condition |
The Do…Loop While structure is useful when the block of code within the structure sets a value for the condition before it is evaluated. This structure is also useful when performing an action on an item which has more than one element (e.g., a string or an array). Since the item has at least one element, execute the block of code at least once and then repeat it based on the total number of elements within the item.
Do Until…Loop
The Do Until Loop is essentially equivalent to the Do While…Loop structure. They both execute a block of code after evaluating a condition. The Do While structure will repeat a block of code until the condition is false. A Do Until structure repeats a block of code until the condition is true.
Do Until condition |
<block of statements that are executed while condition is true> |
Loop |
Do…Loop Until
Like the Do While Loop varieties, the Do Until Loop also offers the option of setting when the condition is evaluated. In this case, the condition is evaluated at the end of the block of code. Therefore, the block of code is executed at least once.
Do |
<block of statements that are exectured while condition is true> |
Loop Until Condition |
Do Loops with If..Then or Select Case statements
Exit Do
Occasionally, the Do Loop structure doesn’t quite meet the need for the intended flow. For instance, a loop may need to be broken immediately within the block of code contained within the structure. This is accomplished by nesting an If…Then…End If or Select Case structure within the block of code executed by the Do Loop.
Do While condition1 |
If condition2 Then |
Exit Do |
End If |
<block of statements that are executed while condition is true> |
Loop |
Exit Do is particularly helpful in debugging code. Specifically, Exit Do will allow the loop to be bypassed without having to manually comment out the entire Do Loop structure.
Do
While the previously described varieties of Do Loops evaluate a condition at either the beginning or the end of a block of code to be executed (and possibly repeated), it is also possible to evaluate the condition within the actual block of code itself. This requires the use of a nested If..Then or Select Case structure.
Do |
(block of statements to be executed while in the Loop structure) |
If condition Then |
Exit Do |
End If |
<block of statements that are executed while condition is true> |
Loop |
This process is useful when part of the block of code within the loop should be executed, but not the entire block of code.
For…Next Loops
If the number of times a block of code should be repeated is definite, a For…Next loop structure is most appropriate. With this structure, the loop is repeated based on the start and end values supplied. These values can be integers, variable expressions. A counter is used to keep track of the number of times the loop is repeated.
For counter = start To end |
<block of statements to be executed> |
Next counter |
When the loop begins, the counter is set to start. When the Next statement is executed, the counter is incremented by one. When the counter is equal to the end value supplied, the loop terminates and the next line of code outside the loop structure is executed.
Tips:
- Keep it simple. Unless there is a specific reason to start the counter at another value, use 1 to n.
- Although the counter variable is not required to follow the Next statement, it is good practice to include it. It may seem verbose, but makes parsing through the code a bit easier.
- Avoid changing the value of the counter within the loop structure manually. Let the Next statement increment the counter unless there is a specific reason to change the counter (e.g., set it to the end value to terminate early).
For…Next loops are particularly useful when working with arrays. An array is similar to a storage bin with numbered slots.
Exit For
Similar in concept to Exit Do, the Exit For statement allows the loop to terminate early. This is typically used in conjunction with If..Then and Select Case statements within the For…Next loop.
For Each…Next Loop
This version of the For…Next Loop is similar to the previous version. However, the primary distinction is that it is used to perform a block of code for each element within a set, rather than for a specified number of times. For Each…Next requires an element or variable that corresponds to the object types within the collection.
For Each variable In collection |
<block of statement to be executed> |
Next variable |
Notice a counter is not specifically used in this version of a For Loop. Instead, E-Basic figures out how many times to repeat the block of code based on the items in the collection specified. This is particularly useful when debugging. If a problem is suspected with perhaps one of the TextDisplay objects within a block, try ‘stepping’ through the processing of each object displaying a marker to the screen to help track down the problem.
Interrupting the Flow
GoTo Label
When a design calls for jumping to another place within the script based on a specific flag, the Goto statement is useful. In behavioral research, this is often required for contingent branching experiments. For example, perhaps the execution flow should continue uninterrupted until a participant responds by pressing the “a” key. If the participant presses any other letter, the execution flow should jump, or Goto a specific location within the code. In the example below, a Label is placed prior to an input statement (i.e., AskBox). The input is examined, and if it is not the required input, the execution of the program jump back to the Label at the beginning of the script in order to perform the response collection again. If the required input is entered (i.e., “a”), the program jumps to a point later in the script.
Dim answer As String |
LabelB: |
answer = AskBox ("Type in a letter:") |
If answer = "a" Then |
Goto LabelA |
Else |
MsgBox "That is the wrong letter, try again!" |
Goto LabelB 'Ask for another letter |
End If |
LabelA: |
MsgBox "Way to go!" |
See Also:
Comments
0 comments
Please sign in to leave a comment.