If - Then - Else

This is probably the most common instruction used in most programming languages and it allows you to include decision making into your program.
If a particular condition is true, then execute this statement(s) otherwise execute that statement(s).
The condition is always a boolean expression that evaluates to either True or False.
This can be used to execute one or more lines conditionally.


Optional Else Clause

The Else clause is optional but can be useful when you want to execute statements for both conditions.
If you type EndIf as one word it will be automatically split into 2 for you.

If (iValue > 10) Then 
   Call MsgBox("number is greater than 10")
Else
   Call MsgBox("number is less than or equal to 10")
End If

The Else clause is optional and can be excluded if you only want to execute one statement based on only one condition.
You can exclude the "= True" part as this is implicit but including it makes your code easier to understand.

If (bInformUser = True) Then 
   Call Msgbox("Hello user")
End If

If (iValue < 20) Then
   Call Msgbox("The value " & iValue & " is less than 20")
End If

One Line Versions

An alternative to the 'If-Then-Else-End If' and 'If-Then-End If' are the one line versions.
If you have one statement for when the condition is true, this can be condensed to one line.
If you are including a second statement for when the condition is NOT true, this can also be condensed to one line.
Condensing your code to one line makes it a lot harder to read and very hard to debug.

If (bInformUser = True) Then Call Msgbox("TRUE") 
If (bInformUser = True) Then Call Msgbox("TRUE") Else Call Msgbox("FALSE")

Exit If Statement

You can exit an If statement at any point using the Exit If statement.

Exit If 

Nested If

You can place an If statement inside another If statement to create nested If statements.
Nested If statements are hard to read and very hard to debug.
When you have a complex set of choices always use a Select-Case statement instead.

If (iMyValue = 32) Then 
'do something
Else
   If (iMyValue < 32) Then
'do something else
   Else
'do something different
   End If
End If

ElseIf Statement

Instead of nesting your If statements another alternative is to use the ElseIf statement.
If the first If evaluates to False, then evaluate the first ElseIf.
The first ElseIf condition that evaluates to True will have its statements executed.
If none of the ElseIf conditions evaluate to True then the final Else is executed.
Long sequences of ElseIf are hard to read and very hard to debug.
When you have a complex set of choices always use a Select-Case statement instead.

If (iMyValue = 32) Then 
'do something
ElseIf (iMyValue < 32) Then
'do something else
Else
'do something different
End If

It is worth mentioning that you don't have to finish with an Else, you could finish with an ElseIf.


Choose

The CHOOSE function can return the value from a list of values based on an index number.
This provides an alternative to the If-Then-Else condition


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext