If...Then...Else

Syntax

If Condition Then Instruction [Else Instruction]

If Condition1 Then
Series of instructions
[ElseIf Condition2 Then
Series of instructions]
[Else
Series of instructions]
End If

Remarks

Runs instructions conditionally. In the simplest form of the If conditional — If Condition Then Instruction — the Instruction runs if Condition is true. In WordBasic, "true" means the condition evaluates to –1 and "false" means the condition evaluates to 0 (zero).

You can write an entire If conditional on one line if you specify one condition following If and one instruction following Then (and one instruction following Else, if included). Do not conclude this form of the conditional with End If. Note that it is possible to specify multiple instructions using this form if you separate the instructions with colons, as in the following conditional:


If Bold() = 1 Then Bold 0 : Italic 1

In general, if you need to specify a series of conditional instructions, the full syntax is preferable to separating instructions with colons. With the full syntax, you can use ElseIf to include a second condition nested within the If conditional. You can add as many ElseIf instructions to an If conditional as you need.

For more information about If¼Then¼Else, see Chapter 3, "WordBasic Fundamentals," in Part 1, "Learning WordBasic."

Examples

This example applies bold formatting to the entire selection if the selection is partially bold:


If Bold() = -1 Then Bold 1

The following example applies italic formatting if the selection is entirely bold; otherwise, underline formatting is applied:


If Bold() = 1 Then Italic 1 Else Underline 1

The following example shows how you can use a compound expression as the condition (in this case, whether the selection is both bold and italic):


If Bold() = 1 And Italic() = 1 Then ResetChar

The following example uses the full syntax available with the If conditional. The conditional could be described as follows: "If the selection is entirely bold, make it italic. If the selection is partially bold, reset the character formatting. Otherwise, make the selection bold."


If Bold() = 1 Then
    Italic 1
ElseIf Bold() = -1 Then
    ResetChar
Else
    Bold 1
End If

See Also

For¼Next, Goto, Select Case, While¼Wend