Using Else

Whereas If executes code based on the comparison test's true condition, the Else statement executes code based on the comparison test's false condition. Else is an optional part of the If statement. Else specifies the code that executes if the comparison test is false. The complete format of the If statement with Else is as follows:

If comparisonTest Then
  One or more Visual Basic statements
Else
  One or more Visual Basic statements
End If

Typically, programmers called this full-blown If statement the If...Else statement. The If...Else statement is sometimes called a mutually exclusive statement. The term mutually exclusive simply means that one set of code or the other executes, but not both. The If...Else statement contains two sets ...

Get Absolute Beginner's Guide to Programming, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.