In VB 6.
0, the "Print" method is a versatile function used to display data on various outputs like
a form, picture box, printer, or the immediate window (debug console), essentially allowing you
to write text to the specified destination; you can also use it to write data to a file with the "Print
#" statement.
Key points about the Print method:
Multiple Outputs:
You can use "Print" to output data to the screen (on a form or picture box), directly to the
printer, or to the immediate window for debugging purposes.
Data Types:
You can print various data types like strings, numbers, dates, and variables directly using
the "Print" statement.
Formatting Options:
To control the appearance of printed data, you can use semicolons (;) to print multiple
values on the same line, or use tab characters to align text.
' Print a simple message to the form
Print "Hello World!"
' Print multiple values on the same line
Print "Name:", Name, "Age:", Age;
' Print to the printer
Printer.Print "This will be printed on paper"
Control Structure (Conditional - IF-ELSE)
If-Then
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.
Syntax:
If Condition Then
statements
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
If-Else
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.
Syntax:
If Condition Then
statements1
Else
statements2
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
Else
Print num2
End If
Output: 50
Single line version of If-Else
If a > 0 Then b = 1 Else b = 0
If-Else-if
If you have programmed in other languages, you might probably know about nested if-else
control flow block. In VB it is the same concept. See the syntax and the examples to capture the
concept.
In if-else-if, the structure is little changed, little advanced better to say, from the simple If-Block.
Here if the first condition does not satisfy, it looks for the next condition preceded by the ElseIf
term, if that too does not satisfy, it looks for the next, and it goes on until the end of the
structure.
Syntax:
If Condition Then
statements
ElseIf Condition then
statements
ElseIf Condition Then
statements
.......................
......................
Else
statements
End If
Example:
a = Val(InputBox("Enter a no."))
If a > 0 Then
Print "Positive"
ElseIf a < 0 Then
Print "Negative"
Else
Print "Zero"
End If
In a nested if-else block, one If-Block can reside in another. See the example below.
Nested-if-Else
In Nested-if-Else, the structure is little changed, little advanced better to say, from the simple If-
Block. Here if the first condition does not satisfy, it looks for the next condition preceded by the
ElseIf term, if that too does not satisfy, it looks for the next, and it goes on until the end of the
structure.
Syntax:
If Condition Then
If Condition Then
statements
ElseIf Condition then
statements
End If
Else
If Condition Then
statements
ElseIf Condition then
statements
End If
End If
Example:
'If-statement inside another if-statement
Dim a As Integer
a = Val(txtNumber.Text)
If a > 0 Then
Print "Positive"
If a > 2 Then
Print "Greater than 2"
Else
Print "Less than 2"
End If
Else
If a < 0 Then
Print "Negative"
End If
End If
Select Case Blocks
The Select Case block provides an efficient way for decision making which is used to execute one
block of statement(s) among a list of statement blocks according to the determined value of an
expression. The expression is preceded by the "Select Case" phrase. The choices are made based
on the value of this expression.
Syntax:
Select Case expression
Case value0
statements
Case value1
statements
Case value2
statements
...........
...............
Case else
statements
End select
Example:
Dim num As Integer
num = Val(Text1.Text)
Select Case num
Case 0
Print "You have entered zero"
Case 1
Print "You have entered one"
Case 2
Print "You have entered two"
Case Else
Print "The number you entered is greater than 2"
End Select
One block of statement(s) will be executed depending upon the value of num.
If num=0 then, Case 0 will be evaluated and if num =1 then Case 1 will be evaluated.
See the other forms of Select Case Structure in the following programs.
Example:
Dim score As Integer
score = Val(Text1.Text)
Select Case score
Case 900 To 1000
Print "First position"
Case 800 To 899
Print "Second position"
Case 700 To 799
Print "Third position"
Case Else
Print "No position, try again"
End Select
Example:
Dim num As Integer
num = Val(Text1.Text)
Select Case num
Case Is > 0
Print "Positive number"
Case Is < 0
Print "Negative number"
Case 0
Print "Zero"
End Select
Example:
Select Case value
Case 0, 1, 2, 3, 4, 5
Print "The values are 0,1,2,3,4,5"
Case Else
Print "Other values"
End Select
Control Structure (BRANCHING - LOOP)
Do Loop
There are different forms of Do Loops in Visual Basic 6. These are
i) Do While...Loop,
ii) Do...Loop While
iii) Do...Loop Until.
i) Do While...Loop:
The Do While...Loop structure is used to execute statements repeatedly based on a
certain condition.
It first tests the condition then evaluates the loop. If the condition is True, the
statements block is executed. If the condition is false, the statements inside the
loop are not executed and the control is transferred to the line after the loop
statements.
The repeated execution of the statements inside the loop goes on as long as the
condition is true. When the condition becomes False, it exits the loop.
Syntax:
Do While Condition
statement(s)
Loop
Example: To print from 0 to 9.
Dim num As Integer
num = 0
Do While num < 10
Print num
num = num + 1
Loop
The program first checks the condition. If num is less than 10, the statements inside
the Do While...Loop are executed. Again, the condition is checked. If it is True, the
statements are executed. In this way, the iterative execution goes on. When the
value of num becomes 10, the loop ends.
ii) Do...loop while:
This loop structure first executes the statements and after that tests the condition
for the repeated execution.
Syntax:
Do
statement(s)
Loop while Condition
Example: To print from 0 to 10.
Dim num As Integer
num = 0
Do
Print num
num = num + 1
Loop While num <= 10
Another example: Though the condition does not satisfy, the program will print
11 as this loop structure first executes the statements and after that tests the
condition.
Dim num As Integer
num = 11
Do
Print num
num = num + 1
Loop While num < 10
Output: 11
iii) Do...loop until:
The Do...Loop Untill structure executes the statements repeatedly until the
condition is met. It is an infinite loop if the condition does not satisfy. In this case,
the program cannot be stopped. Press Ctrl+Break key combination to force it to
stop. The loop continues until the condition is met. The repeated execution of the
statements stops when the condition is met.
Syntax:
Do
statement(s)
Loop Until Condition
Example:
'x is incremented until x becomes greater than 10
Dim x As Integer
x=0
Do
x=x+1
Loop Until x > 10
MsgBox x
For...Next Loops
Example: To print 0 to 10.
Dim i As Integer
For i = 0 To 10
Print i
Next i
When the value of i is 0, the Print statement is executed then i is incremented to 1. It checks
whether the value of i is from 0 to 10. If it satisfies, the Print statement is executed again. In this
way, the loop goes on until the value of i exceeds 10. Every time, the value of i is incremented by
1.
Example: To print 0 to 6 in steps of 2.
Dim i As Integer
For i = 0 To 6 Step 2
Print i
Next i
Every time, the value of i is incremented by 2.
Output:
0
Example: To print in descending order from 10 to 0 in step of -3.
Dim i As Integer
For i = 10 To 0 Step -3
Print i
Next i
Every time, the value of i is decremented by 3.
Output:
10
Exit For and Exit Do statement
A For Next Loop can be terminated by an Exit For statement and a Do loop can be terminated by
an Exit Do statement.
Example: Exit For statement :
Dim i As Integer
For i = 0 To 10
If i = 3 Then
Exit For
End If
Print i
Next i
Output:
Example: Exit Do statement
Dim num As Integer
num = 0
Do While num < 10
Print num
num = num + 1
If num = 4 Then
Exit Do
End If
Loop
Output:
The For Each Loop
You use the For Each loop to loop over elements in an array or collection. Heres its syntax:
For Each element In group
[statements]
[Exit For][statements]
Next [element]
You can get a look at this loop in action with an example like this one, where we display all the elements
of an array in message boxes:
Dim IDArray(1 To 3)
IDArray(1) = 1
IDArray(2) = 2
IDArray(3) = 3
For Each ArrayItem In IDArray
MsgBox (Str(ArrayItem))
Next ArrayItem
Program in Visual Basic to generate the following pattern of stars:
*
**
***
****
*****
Step-1 Drag and drop a command button on the form. Set its caption to ‘Print’
Step-2 Write the following code on the click event of command button
Private Sub Command1_Click()
Dim i As Integer
Dim j As Integer
For i = 1 To 5 Step 1
For j = 1 To i
Print " * ";
Next j
Print
Next i
End Sub