|Home About Free Videos FAQ Contact|
Selection structures (also known as conditional statements) allow a program to decide on a course of action based on the result of a condition. These structures are commonly divided into two general classifications. In the first class, the course of action is typically limited to one or two paths (i.e., without nesting statements). The first path is executed when the condition evaluates to true. The second path, also known as the alternate or else part, is executed when the condition evaluates to false. For example, the following Visual Basic statement prints "Pass" when the variable average is greater than or equal to 60, otherwise, it prints "Fail".
if average >= 60 then print "Pass" else print "Fail" end ifIn the second class, multiple courses of action may be specified. These structures allow a problem to be divided into several cases that are handled differently. Visual Basic supports a labeled case-statement that is more flexible than an un-labeled case-statement. In particular, the labeled case-statement allows values other than integers to be used to determine between the cases. For example, the code below displays a message based on a student's grade.
select case grade case "A" print "Excellent" case "B" print "Above Average" case "C" print "Average" case "D", "F" print "Below Average" end select
Repetition structures can be divided into definite iteration and indefinite iteration. Definite iteration - iterates a predictable number of times. In Visual Basic, the For…Next statement supports definite iteration. For example, the following code prints out the numbers between 1 and 10.
For index = 1 to 10 print index Next indexIndefinite iteration is a repetition structure where the exact number of iterations is not known in advance. When using indefinite iteration, a condition is tested during each iteration to determine whether or not the loop has completed. Visual Basic supports both a leading-decision iterator (i.e., the Do…While loop) and a trailing-decision iterator (i.e., the Do…Until loop). In the example below, the file "movies.txt" will be sequentially read until the end-of-file (eof) marker has been encountered.
open "c:\temp\movies.txt" for input as #1 do while not eof (1) input #1, temp loopOther Structures
Other constructs that should be examined in an introductory computer course and which are supported in Visual Basic include:
File Access – how to create, open, and close files, as well as how to write and read data from both sequential and random access files.
Subroutines (procedures/functions) – blocks of code that perform specific tasks. Passing information to and from subroutines should also be studied in an introductory programming course.
Arrays – an ordered arrangement of data elements. Many common business applications use one-dimensional and two-dimensional arrays for storing data. Visual Basic supports arrays containing up to 60 dimensions (Zak 1999).
Visual Basic is an object-oriented programming language. This is in contrast to procedure-oriented languages such as COBOL, BASIC, Pascal, and C. In procedure-oriented languages, the programmer controls the order in which the instructions are executed. The programmer must write the necessary code from the start of the application until the task is completed. In these languages the code (computer instructions) often called procedures and functions is separate from the data (information) that is usually stored in some type of structure. However in object- oriented languages, the code and the data are stored together in objects. The programmer communicates with objects by sending messages to them. The programmer does not change the code contained within the objects. This process of hiding the code and not allowing the user to modify it is called "information hiding" or encapsulation.
While applications like Excel include a comprehensive listing of built-in functions, inevitably there will arise a need to customize or write functions for a particular task. For example, Excel includes a built-in function called max that returns the maximum value for a specified range. However, Excel does not support a function that returns the two largest values from a range. To find the two largest values in a range requires a VBA to be developed similar to the one shown below. This example finds the two largest values in cells A1 through A100 and stores this information in cells B1 and B2.
Dim largest, second_largest As Integer Dim temp, temp2, index As Integer temp = Cells(1, 1).Value temp2 = Cells(2, 1).Value If (temp > temp2) Then largest = temp second_largest = temp2 Else largest = temp2 second_largest = temp End If For index = 3 To 100 temp = Cells(index, 1).Value If (temp > largest) Then second_largest = largest largest = temp Else If (temp > second_largest) Then second_largest = temp End If End If Next index Cells(1, 2).Value = largest Cells(2, 2).Value = second_largest
Every day more and more applications are being developed for the Web. These applications are being used to access databases and order products. They are proving that the Web is much more than a server protocol for transferring static documents. Instead, we are seeing the Web being used to perform sophisticated information retrieval and run external applications. When selecting a programming language, the ability to create applications for the Web should be a considered. By learning Visual Basic, students are gaining the knowledge to create fully interactive, dynamic pages. This is because Visual Basic is a superset of Visual Basic Script (VBScript) as shown in Figure 1. VBScript is a scripting language designed to run inside web browsers and makes it easy to turn a static HTML page into a dynamic, interactive client side application. One of the main differences between Visual Basic and VBScript is that since the code runs within a web page, functions that can be used to edit files and in any way tamper with the system are not present in VBScript.