View contents of this chapter

Execution Control

The If-Else Statement

The If-Else statement is the most basic way to control program flow. The If statement conditionally executes a group of statements, depending on the value of an expression.

If condition Then
  [Block of statements]
End If

The condition is any expression that returns a True or False value.

Play Sound All multiline If statements have a matching set of End If statements.

Consider the following program segment:

1: If (value > 10) Then
2:    no = no + 10
3: End If

If the value is greater than 10, statement "2:" will be executed.

Play Sound It is convenient to indent the body of control flow statement so the reader might easily determine where it begins and ends.

The preceding section described one form of If, the following code segment shows the expanded form:

If condition Then
  [Block of statements]
else
  [Block of statements]
End If

The If-Else statement provides two bodies of code: one that executes if the condition is true and one that executes if the condition is false. Consider the following program segment:

1: If (value > 10) Then
2:    no = no + 10
3: Else
4:    no = no - 10
5: End If

If the value is greater than 10, statement "2:" will be executed else statement "4:" will be executed.



© Universal Teacher Publications        INDEX Previous Screen Next Screen