If statements
In Visual Basic, the If
statement executes a block of code if a condition is true. The basic syntax of an If
statement in Visual Basic is as follows:
If condition Then ' code to execute if condition is true End If
For example, the following VB6 code checks whether a number is positive:
Dim test As Integer = 12 If test > 0 Then MsgBox ("The number is positive") End If
You can also use an Else
statement to execute a different block of code if the condition is false. The basic syntax of an If-Else
statement in Visual Basic is as follows:
If condition Then ' code to execute if condition is true Else ' code to execute if condition is false End If
For example, the following VB6 code checks whether a number is positive or negative:
Dim test As Integer = -12 If test > 0 Then MsgBox("The number is positive") Else ...