Difference between revisions of "Documentation/BASIC Guide/Branching"

From Apache OpenOffice Wiki
Jump to: navigation, search
m (Reverted edits by Kclty (Talk) to last revision by BMarcelly)
 
(11 intermediate revisions by 5 users not shown)
Line 7: Line 7:
 
|lang=block
 
|lang=block
 
}}
 
}}
 
+
 
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.
  
Line 14: Line 14:
 
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:
  
If A > 3 Then
+
<source lang="oobas">
  B = 2
+
If A > 3 Then
End If
+
  B = 2
 +
End If
 +
</source>
  
 
The <tt>B = 2</tt> assignment only occurs when value of variable <tt>A</tt> is greater than three. A variation of the <tt>If</tt> statement is the <tt>If/Else</tt> clause:
 
The <tt>B = 2</tt> assignment only occurs when value of variable <tt>A</tt> is greater than three. A variation of the <tt>If</tt> statement is the <tt>If/Else</tt> clause:
  
If A > 3 Then
+
<source lang="oobas">
  B = 2
+
If A > 3 Then
Else
+
  B = 2
  B = 0
+
Else
End If
+
  B = 0
 +
End If
 +
</source>
  
 
In this example, the variable <tt>B</tt> is assigned the value of 2 when <tt>A</tt> is greater than 3, otherwise <tt>B</tt> is assigned the value of 0.
 
In this example, the variable <tt>B</tt> is assigned the value of 2 when <tt>A</tt> is greater than 3, otherwise <tt>B</tt> is assigned the value of 0.
Line 30: Line 34:
 
For more complex statements, you can cascade the <tt>If</tt> statement, for example:
 
For more complex statements, you can cascade the <tt>If</tt> statement, for example:
  
If A = 0 Then
+
<source lang="oobas">
  B = 0
+
If A = 0 Then
ElseIf A < 3 Then
+
  B = 0
  B = 1
+
ElseIf A < 3 Then
Else  
+
  B = 1
  B = 2
+
Else  
End If
+
  B = 2
 +
End If
 +
</source>
  
 
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.
 +
 +
 +
A complete If statement may be written on a single line, with a simpler syntax. The first example of this page may be written as:
 +
 +
<source lang="oobas">
 +
If A > 3 Then  B = 2
 +
</source>
 +
The second example of this page may be written as:
 +
<source lang="oobas">
 +
If A > 3 Then  B = 2  Else  B = 0
 +
</source>
  
 
== <tt>Select...Case</tt> ==
 
== <tt>Select...Case</tt> ==
Line 44: Line 61:
 
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:
  
Select Case DayOfWeek
+
<source lang="oobas">
  Case 1:
+
Select Case DayOfWeek
    NameOfWeekday = "Sunday"
+
  Case 1:
  Case 2:  
+
    NameOfWeekday = "Sunday"
    NameOfWeekday = "Monday"
+
  Case 2:  
  Case 3:  
+
    NameOfWeekday = "Monday"
    NameOfWeekday = "Tuesday"
+
  Case 3:  
  Case 4:
+
    NameOfWeekday = "Tuesday"
    NameOfWeekday = "Wednesday"
+
  Case 4:
  Case 5:
+
    NameOfWeekday = "Wednesday"
    NameOfWeekday = "Thursday"
+
  Case 5:
  Case 6:
+
    NameOfWeekday = "Thursday"
    NameOfWeekday = "Friday"
+
  Case 6:
  Case 7:
+
    NameOfWeekday = "Friday"
    NameOfWeekday = "Saturday"
+
  Case 7:
End Select
+
    NameOfWeekday = "Saturday"
 +
End Select
 +
</source>
  
 
In this example, the name of a weekday corresponds to a number, so that the <tt>DayOfWeek</tt> variable is assigned the value of 1 for <tt>Sunday</tt>, 2 for <tt>Monday</tt> value, and so on.
 
In this example, the name of a weekday corresponds to a number, so that the <tt>DayOfWeek</tt> variable is assigned the value of 1 for <tt>Sunday</tt>, 2 for <tt>Monday</tt> value, and so on.
Line 65: Line 84:
 
The <tt>Select</tt> command is not restricted to simple 1:1 assignments — you can also specify comparison operators or lists of expressions in a <tt>Case</tt> branch. The following example lists the most important syntax variants:
 
The <tt>Select</tt> command is not restricted to simple 1:1 assignments — you can also specify comparison operators or lists of expressions in a <tt>Case</tt> branch. The following example lists the most important syntax variants:
  
Select Case Var
+
<source lang="oobas">
  Case 1 To 5                   
+
Select Case Var
    ' ... Var is between the numbers 1 and 5
+
  Case 1 To 5                   
  Case 6, 7, 8                   
+
    ' ... Var is between the numbers 1 and 5 (including the values 1 and 5).
    ' ... Var is 6, 7 or 8
+
  Case > 100
  Case Var > 8 And Var < 11   
+
    ' ... Var is greater than 100
    ' ... Var is greater than 8 and less than 11
+
  Case 6, 7, 8                   
  Case Else
+
    ' ... Var is 6, 7 or 8
    ' ... all other instances
+
  Case 6, 7, 8, > 15, < 0
End Select
+
    ' ... Var is 6, 7, 8, greater than 15, or less than 0
 +
  Case Else
 +
    ' ... all other instances
 +
End Select
 +
</source>
 +
 
 +
Now consider a misleading (advanced) example, and a common error:
 +
 
 +
<source lang="oobas">
 +
Select Case Var
 +
  Case Var = 8
 +
    ' ... Var is 0
 +
  Case Else
 +
    ' ... all other instances
 +
End Select
 +
</source>
 +
 
 +
The statement (Var = 8) evaluates to TRUE if Var is 8, and FALSE otherwise. TRUE is -1 and FALSE is 0. The Select Case statement evaluates the expression, which is TRUE or FALSE, and then compares that value to Var. When Var is 0, there is a match. If you understand the last example, then you also know why this example does not do what it appears
 +
 
 +
<source lang="oobas">
 +
Select Case Var
 +
  Case Var > 8 And Var < 11
 +
    ' ... Var is 0
 +
  Case Else
 +
    ' ... all other instances
 +
End Select
 +
</source>
  
  
 +
{{InterWiki Languages BasicGuide|articletitle=Documentation/BASIC Guide/Branching}}
 
{{PDL1}}
 
{{PDL1}}

Latest revision as of 16:42, 7 October 2011


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.


A complete If statement may be written on a single line, with a simpler syntax. The first example of this page may be written as:

If A > 3 Then  B = 2

The second example of this page may be written as:

If A > 3 Then  B = 2  Else  B = 0

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 (including the values 1 and 5).
  Case > 100
    ' ... Var is greater than 100
  Case 6, 7, 8                  
    ' ... Var is 6, 7 or 8
  Case 6, 7, 8, > 15, < 0
    ' ... Var is 6, 7, 8, greater than 15, or less than 0
  Case Else
    ' ... all other instances
End Select

Now consider a misleading (advanced) example, and a common error:

Select Case Var
  Case Var = 8
    ' ... Var is 0
  Case Else
    ' ... all other instances
End Select

The statement (Var = 8) evaluates to TRUE if Var is 8, and FALSE otherwise. TRUE is -1 and FALSE is 0. The Select Case statement evaluates the expression, which is TRUE or FALSE, and then compares that value to Var. When Var is 0, there is a match. If you understand the last example, then you also know why this example does not do what it appears

Select Case Var
  Case Var > 8 And Var < 11
    ' ... Var is 0
  Case Else
    ' ... all other instances
End Select


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools