VB6 Tutorial 17: If ... Then Blocks

Posted On 2018-04-01 by VB6Boy
Tags: VB6 Tutorials Windows
Views: 45

Title Uploaded Size
IfThen.zip 4/1/2018 12:15:03 PM 995
IfThenElse.zip 4/1/2018 12:16:07 PM 999
LargestNumber.zip 4/1/2018 12:16:20 PM 1,258

Looking for the more of the VB6 Tutorial? Here is the tutorial index.

There are many 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


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.

See the attached project IfThen to see this in action.

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.

See the attached project IfThenElse to see how this block works.


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

The attached project LargestNumber allows you to see how If / then / else works with real data.

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.

Looking for the more of the VB6 Tutorial? Here is the tutorial index.

About the Author

VB6Boy has posted a total of 71 articles.

Comments On This Post

No comments on this post yet!

Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.

Your IP address is:

Before you can post, you need to prove you are human. If you log in, this test goes away.

Code Links