User`s guide

E-Prime User’s Guide
Chapter 4: Using E-Basic
Page 143
If…Then statements simply are equated to making a choice (e.g., if I have money, then I can go
to the movies). If…Then statements are the foundation of all logic. Although they seem to be
very simple statements, they are quite powerful in a programming language.
There are actually 3 conditional expressions available in E-Basic. They are: If…Then, Select
Case, and Do…Loop. These expressions are conditional statements simply because they
perform a task based on a single true or false test. The If…Then and Select Case constructions
will be discussed in this section. The discussion of Do…Loop is reserved for section 4.5.2.2
Loops in this chapter.
If…Then statements
The single most commonly used flow control statement is If…Then. Simply put, an If…Then
statement will execute a block of code if the condition is true. If the condition is false, it will do
nothing unless "Else" is used in the flow control statement.
If condition Then
<Block of code statements to execute if the condition is true>
End If
Rules:
Notice the "Then" portion of the statement is on the same line as the "If" portion. If there
is a need to drop it to the next line, indicate that to the E-Basic compiler. This is done by
placing an underscore (i.e., E-Basic line continuation character) at the end of the line to
be continued.
The End If statement is critical. It identifies the last statement in the block of code to be
executed based on the condition.
When the If…Then statement is only a one-line statement, the End If is not used. In fact,
it will produce an error if used. In the following two examples, the code works exactly the
same, but it is syntactically different.
Example 1: One-line If…Then statement
Dim a As Integer
a = 12
If a > 10 Then MsgBox "A is greater than 10."
Example 2: If…Then…End If statement
Dim a As Integer
a = 12
If a > 10 Then
MsgBox "A is greater than 10."
End If