21.08.2013 Views

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

OpenOffice.org BASIC Guide - OpenOffice.org wiki

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Branching<br />

B = 0<br />

End If<br />

In this example, the variable B is assigned the value of 2 when A is greater than 3, otherwise B is assigned the value<br />

of 0.<br />

For more complex statements, you can cascade the If statement, for example:<br />

If A = 0 Then<br />

B = 0<br />

ElseIf A < 3 Then<br />

B = 1<br />

Else<br />

B = 2<br />

End If<br />

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<br />

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.<br />

A complete If statement may be written on a single line, with a simpler syntax. The first example of this page may<br />

be written as:<br />

If A > 3 Then B = 2<br />

The second example of this page may be written as:<br />

If A > 3 Then B = 2 Else B = 0<br />

Select...Case<br />

The Select...Case instruction is an alternative to the cascaded If statement and is used when you need to<br />

check a value against various conditions:<br />

Select Case DayOfWeek<br />

Case 1:<br />

NameOfWeekday = "Sunday"<br />

Case 2:<br />

NameOfWeekday = "Monday"<br />

Case 3:<br />

NameOfWeekday = "Tuesday"<br />

Case 4:<br />

NameOfWeekday = "Wednesday"<br />

Case 5:<br />

NameOfWeekday = "Thursday"<br />

Case 6:<br />

NameOfWeekday = "Friday"<br />

Case 7:<br />

NameOfWeekday = "Saturday"<br />

End Select<br />

In this example, the name of a weekday corresponds to a number, so that the DayOfWeek variable is assigned the<br />

value of 1 for Sunday, 2 for Monday value, and so on.<br />

The Select command is not restricted to simple 1:1 assignments — you can also specify comparison operators<br />

or lists of expressions in a Case branch. The following example lists the most important syntax variants:<br />

Select Case Var<br />

Case 1 To 5<br />

' ... Var is between the numbers 1 and 5 (including the values 1 and 5).<br />

Case > 100<br />

' ... Var is greater than 100<br />

Case 6, 7, 8<br />

' ... Var is 6, 7 or 8<br />

Case 6, 7, 8, > 15, < 0<br />

' ... Var is 6, 7, 8, greater than 15, or less than 0<br />

Case Else<br />

' ... all other instances<br />

End Select<br />

Now consider a misleading (advanced) example, and a common error:<br />

Select Case Var<br />

Case Var = 8<br />

' ... Var is 0<br />

24 <strong>OpenOffice</strong>.<strong>org</strong> 3.2 <strong>BASIC</strong> <strong>Guide</strong> · March 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!