If Blocks in VB6

There is a bunch of control flow blocks in Visual Basic 6 that help in executing some statements in a particular way. This lesson demonstrates the use of If-block.

The If control flow blocks provide a great way for decision making where one or more statements are executed depending upon a condition.


In case of If-Then block, the condition is first checked. The condition can either be True or False. If it is True, the statements inside the If-Then block is executed. Otherwise, it does not execute the code in the If-Then block, the If-Then structure is skipped. It starts executing the statements just after the 'End if' phrase.      

                If Condition Then
                End If

Example: To check if the number is positive.
Dim num1 As Integer
num1 = 30
If num1 > 0 Then
    Print "The number is positive"
End If 

Output: The number is positive.

Single Line version of If-Then

Single line and single statement
If a > 0 Then b = 1

Single line and multiple statements
If a > 0 Then b = 1: c = 2


An If-Else block has two parts. If the condition is True then the first part is executed, otherwise the control goes to the second part and starts executing the lines of statements in the second part of the If-Else block.

              If Condition Then
              End If

If Condition is true then statements1 will be executed and if Condition is false, statements2 will be executed.

Example: To print the largest number.
Dim num1 As Integer, num2 As Integer
num1 = 30
num2 = 50
If num1 > num2 Then
    Print num1
    Print num2
End If

Output: 50

Go here for similar programs: VB6 source code for beginners.

Single line version of If-Else

If a > 0 Then b = 1 Else b = 0

In the next lesson, you'll learn more about the if-else statements.