Difference between revisions of "Documentation/BASIC Guide/Branching"
Line 11: | Line 11: | ||
Use branching statements to restrict the execution of a code block until a particular condition is satisfied. | Use branching statements to restrict the execution of a code block until a particular condition is satisfied. | ||
− | == If...Then...Else == | + | == <tt>If...Then...Else</tt> == |
The most common branching statement is the <tt>If</tt> statement as shown in the following example: | The most common branching statement is the <tt>If</tt> statement as shown in the following example: | ||
Line 41: | Line 41: | ||
If the value of variable <tt>A</tt> equals zero, <tt>B</tt> is assigned the value 0. If <tt>A</tt> is less than 3 (but not equal to zero), then <tt>B</tt> is assigned the value 1. In all other instances (that is, if <tt>A</tt> is greater than or equal to 3), <tt>B</tt> is assigned the value 2. | If the value of variable <tt>A</tt> equals zero, <tt>B</tt> is assigned the value 0. If <tt>A</tt> is less than 3 (but not equal to zero), then <tt>B</tt> is assigned the value 1. In all other instances (that is, if <tt>A</tt> is greater than or equal to 3), <tt>B</tt> is assigned the value 2. | ||
− | == Select...Case == | + | == <tt>Select...Case</tt> == |
The <tt>Select...Case</tt> instruction is an alternative to the cascaded <tt>If</tt> statement and is used when you need to check a value against various conditions: | The <tt>Select...Case</tt> instruction is an alternative to the cascaded <tt>If</tt> statement and is used when you need to check a value against various conditions: |
Revision as of 12:19, 26 September 2007
Branching
Use branching statements to restrict the execution of a code block until a particular condition is satisfied.
If...Then...Else
The most common branching statement is the If statement as shown in the following example:
If A > 3 Then B = 2 End If
The B = 2 assignment only occurs when value of variable A is greater than three. A variation of the If statement is the If/Else clause:
If A > 3 Then B = 2 Else B = 0 End If
In this example, the variable B is assigned the value of 2 when A is greater than 3, otherwise B is assigned the value of 0.
For more complex statements, you can cascade the If statement, for example:
If A = 0 Then B = 0 ElseIf A < 3 Then B = 1 Else B = 2 End If
If the value of variable A equals zero, B is assigned the value 0. If A is less than 3 (but not equal to zero), then B is assigned the value 1. In all other instances (that is, if A is greater than or equal to 3), B is assigned the value 2.
Select...Case
The Select...Case instruction is an alternative to the cascaded If statement and is used when you need to check a value against various conditions:
Select Case DayOfWeek Case 1: NameOfWeekday = "Sunday" Case 2: NameOfWeekday = "Monday" Case 3: NameOfWeekday = "Tuesday" Case 4: NameOfWeekday = "Wednesday" Case 5: NameOfWeekday = "Thursday" Case 6: NameOfWeekday = "Friday" Case 7: NameOfWeekday = "Saturday" End Select
In this example, the name of a weekday corresponds to a number, so that the DayOfWeek variable is assigned the value of 1 for Sunday, 2 for Monday value, and so on.
The Select command is not restricted to simple 1:1 assignments — you can also specify comparison operators or lists of expressions in a Case branch. The following example lists the most important syntax variants:
Select Case Var Case 1 To 5 ' ... Var is between the numbers 1 and 5 Case 6, 7, 8 ' ... Var is 6, 7 or 8 Case Var > 8 And Var < 11 ' ... Var is greater than 8 and less than 11 Case Else ' ... all other instances End Select
Content on this page is licensed under the Public Documentation License (PDL). |