VB Tutorial 4 - Statement Syntax
 


In our last tutorial we looked at the VB editor and all the help it provides in programming. At the same time the 12 variable types of Visual Basic were discussed. In most VB programs you will find that you use String, Double, Integer, Long and Variant the most often. In this session we get into the heart of programming, and examine the 4 most commonly used statements in Visual Basic.

Every procedural programming language has 5 common programming statements. Visual Basic is no exception. So expect to see the following type of statements in just about every VB program which you use or write:

Table 1
Visual Basic's 6 Most Common Programming Statements

Statement type

Examples

Declarations - define the name, type and attributes of all program variables Dim Num as Integer ' declares a variable "Num" to be an Integer
Dim vals(5) as Double ' declares an array of 5 Doubles named "vals"
Assignment - set values for the variables Num = Num/10 ' after the assignment Num is set to 1/10th of its former value
HiString = "Hello " + "World" ' value of HiString is set to "Hello World"
Conditionals - do operations depending on the value of one or more variables if Num > 0 then Num = Num * 2 ' Basic logic building block
Select Case .... End Case 'Case block simplifies GUI programming
Iterations - control looping for repeated operations for i = 1 to 5 'For-loop counts through a precise number of steps
while ( val(i) > val(imin) ) 'While loops as long as condition remains True
Subroutines - calls on functions and subroutines Private Sub Form_Load() 'A subroutine does not return a value
Private Function Digit() ' A function returns a value
Special statements - used to implement unique features Set MyObject = Your Object 'Set statement assigns object references
Print #FileNum, MyObject.Text 'I/O statements like Print, Input Line

Visual Basic like most other programming languages uses a whole group of Special statements to add new features and functionality to the language such as classes , file i/o and database connectivity. We shall cover these Special statements as the need arises while concentrating on the meat-and-potatoes - assignments, conditionals, iteration loops and function calls.

Fortunately, Visual Basic supplies the user with a number of methods of testing out statement calulations , string manipulations and one or two line program snippets - it is called the Immediate Window. On the other hand if you need to check the exact syntax and functioning of a subroutine then using a button click to launch the routine is probably the fastest and easiest means of testing your program code - especially because you have the full power of VB's debug capabilities.

Figure 1 - Immediate Window

Immediate Window

Visual Basic's Immediate Window can be used at anytime (not just during a debug session) to test how a statement will work. In Figure 1 we test to see how the Format statement works by joining or concatenating its results to the string "apple". First we initialize i = 3. Then by adding the print command, the exact way "apple" is concatenated with the format string "xxx0" - the "0" character says print one digit of the value of i.
 

But don't try to enter a declaration or multi-statement if or for loop. The Immediate Window only works with line by line commands like Print (1+rate2 ^ n) * PV . For more elaborate testing, its simple and easy to setup a small VB program to test a snippet of code. Infact, while describing the conditional if statement in more detail we will also be setting up a simple VB Test Program

Testing Programs in VB

One of the best things to do in VB is to take advantage of its speed and ease of setting up programs. So in order to test some annuity calculations, we shall set up just such a test program:

1)Click File | New Project from the menus;
2)Add a button to the program form, and change its caption to Test It in the Properties;
3)Add two text fields one below the other in the form - they will be automatically named Text1 and Text2;
4)Change their Text property from Text1 to PV, Text2 to 100;
5)Add a label field and change its caption from Label1 to Value;
6)Then double click on the Test It button and add the following code to Sub:

Dim xx As Double, fv2 As Double, pv2 As Double
Dim rate2 As Double, ann2 As Double, n2 As Double
rate2 = 0.1
fv2 = 0#
pv2 = 0#
n2 = 3
ann2 = CDbl(Text2.Text)
If UCase(Text1.Text) = "PV" Then
  pv2 = ann2 * (1 - (1 + rate2) ^ (-n2)) / rate2
  Label1.Caption = "PV=" + Format(pv2)
ElseIf UCase(Text1.Text) = "FV" Then
  fv2 = ann2 * ((1 + rate2) ^ n2 - 1) / rate2
  Label1.Caption = " FV=" + Format(fv2)
Else
  MsgBox "Enter PV or FV in first text box", vbOKOnly, "Warning"
End If

Finally add a breakpoint by going into VB's editor, highlighting the point or line of code where you want the program to break/pause and then pressing the F9 key or clicking the Debug | Toggle Breakpoint menuitem. Now for some fun ! Click Debug | Start and the program starts up halting at your breakpoint as in Figure 2 below

We can't cover all the details but there is a wealth of information available to help figure out a)how VB works and b)any bugs in your program. In a later tutorial we will cover VB's debugger in more detail. For now, here are 5 highlights. 1)The breakpoint line is higlighted right in the familiar editor environ. You can set many breakpoints. 2)You can step through the code line by line. The active line is highlighted in yellow. 3)By moving the cursor over any variable, VB's debugger reports the variable's current value in a hint note. 4)There is a special Debug toolbar which allows you to trace, step into and out of routines plus a whole range of special debug display options. 5)Finally, you can see the VB program form, using it to check that input and output works as expected. This interactive debugging capability is one of the pillar's of strength for Visual Basic. Only Delphi and Java come close to Visual Basic in quickness and the ease with which you can quickly setup and test parts of program code. So as your are learning Visual Basic take advantage of VB's powerful debugger.


In the meantime, the program uses a if..else if .. else conditional. This type of conditional statememnt allows the user to check for one of several conditions and specify actions to be taken for each condition. The program currently checks for two values: if "PV" is input then do a Present value calculation, if "FV", do a Future Value calculation otherwise a message box issues a warning. It is easy to add another else-if conditional for "AN"-annuity calculation when it is required. Conditional are also called control-flow statements because they control the flow of the program logic depending on input values. However, conditionals can become quite complex. Our next tutorial will show how the Select Case statement helps to handle these more complex conditionals. Meanwhile, users should try and explore with VB's debugger on some of the sample programs in Beginning VB6/WROX or VB6 Black Book/Coriolis.

Resources:
The book Beginning VB6/Wrox covers variables on pages 113-173; VB6 Black Book/Coriolis does the same on pages 36-96. Also check the VB References
 
Top of Page  Tutorials Home