CSC104 Programming
Visual Basic Overhead Transparencies

Declaration and Assignment Statements (6-6)

Dim Age As Integer
Age = 23
Age = Age + 1

Age24
Dim Addr As String Addr = "R.R. #5 Cavan"
Addr"R.R. #5 Cavan"
Dim Price, Total, Tax As Single Price = 9.99 Tax = 0.15 Total = Price * (1 + Tax)
Price9.99
Tax0.15
Total11.48

Identifiers - Names for Variables, Functions, or Subroutines (6-9)

BadGood
94CurrSalesCurr94Sales
IntRegion WestIntRegionWest
IngEast.popIngEastPop
DimDimension
Bad StyleGood Style
nNumber
totalsalesTotalSales
OntarioProvincialSalesTaxOntProvTax

Constants (6-13)

Dim SubTotal, Total As Single
Const TAX = 0.15

SubTotal = 2.99 + 5.99
Total = SubTotal * (1 + TAX)

SubTotal = SubTotal + 1.02
Total = SubTotal * (1 + TAX)

TAX0.15
SubTotal10.0
Total11.5

Expressions (6-15)

Dim Result As Integer
Result = 5 + 2 * 3
Result = 6 * Result + 1
Result = - Result

Result-67

Built-In Functions (6-17)

Dim Input As String
Dim Number As Integer
Input = "2457"
Number = Val (Sum)

Input"2457"
Number2457

String Concatenation (7-2)

Dim Age As Integer
Dim FirstName, LastName As String

Age = 25
FirstName = "Bob"
LastName = "Smith"

Label3.Caption =  "Age for " & FirstName & " " & LastName & " is " & Age

Label3.Caption"Age for Bob Smith is 25"

String Functions (7-3)

Dim Result As String
Result = Right (Left ("CSC 104Y", 6), 2)
Result = Left (Result, 1)

Result"1"

String Functions (7-6)

Dim Position As Integer
Const VOWELS = "aeiou"

Dim Word As String
Word = "Excellent"

' Is 5th character in Word a vowel?  Display result on Label3.
Position = InStr (1, VOWELS, Mid(Word, 5, 1), 1)

If Position = 0 Then
    Label3.Caption = "Character 5 is not a vowel."
Else
    Label3.Caption = "Character 5 is a vowel."
End If

Label3.Caption"Character 5 is not a vowel"

What if we change 5 to 4?

Dim Position As Integer Const VOWELS = "aeiou" Dim Word As String Word = "Excellent" ' Is 4th character in Word a vowel? Display result on Label3. Position = InStr (1, VOWELS, Mid(Word, 4, 1), 1) If Position = 0 Then Label3.Caption = "Character 4 is not a vowel." Else Label3.Caption = "Character 4 is a vowel." End If
Label3.Caption"Character 4 is a vowel"

Decision Structure (7-17)

Private Sub Command1_Click()
    Dim Age As Integer
    Age = Text1.Text

    If Age > 30 Then
        Text2.Text = "Person is old"
    End If
End Sub

        ___________________________
        |             ______      |
Label1  | Enter age: | 32   |     |     Text1
        |            |______|     |
        |      _______________    |
        |     | Person is old |   |     Text2
        |     |_______________|   |
        |         _________       |
        |        | Process |      |     Command1
        |         ---------       |
        |_________________________|


Private Sub Command1_Click()
    Dim Gender As String
    Gender = Text1.Text

    If Gender = "M" Then
        Label2.Caption = "Person is male"
    Else
        Label2.Caption = "Person is female"
    End If
End Sub

        ___________________________
        |             ______      |
Label1  | Enter sex: | F    |     |     Text1
        |  (M/F)     |______|     |
        |                         |
        |    Person is female     |     Label2
        |                         |
        |         _________       |
        |        | Process |      |     Command1
        |         ---------       |
        |_________________________|

For Loop (7-21)

Dim Junk As String
Junk = ""

For Count = 1 To 5
    Junk = Junk & Count
Next Count

Label2.Caption = Junk

Label2.Caption"12345"
This table shows the value of the variables as the for loop executes. This is a common way to build a new string, one character at a time.
CountJunk & CountJunk
1"" & 1"1"
2"1" & 2"12"
3"12" & 3"123"
4"123" & 4"1234"
5"1234" & 5"12345"

For Loop - Reverse Order (7-21)

This program is the same as the one above, except that it uses "Count &
Junk" so that the resulting string that is placed into Junk is
reversed.


Dim Junk As String
Junk = ""

For Count = 1 To 5
    Junk = Count & Junk
Next Count

Label2.Caption = Junk

Label2.Caption"54321"
This table shows the value of the variables as the for loop executes. This is a common way to build a new string, one character at a time.
CountCount & JunkJunk
11 & """1"
22 & "1""21"
33 & "21""321"
44 & "321""4321"
55 & "4321""54321"

Do Loop Eg. #2 (7-25)

CurrentCurrent Mod 2Current Mod 3
Current=11 Mod 2
0 R. 1
1 Mod 3
0 R. 1
Current=22 Mod 2
1 R. 0
2 Mod 3
0 R. 2
Current=33 Mod 2
1 R. 1
3 Mod 3
1 R. 0
Current=44 Mod 2
2 R. 0
4 Mod 3
1 R. 1
Current=55 Mod 2
2 R. 1
5 Mod 3
1 R. 2
Current=66 Mod 2
3 R. 0
6 Mod 3
2 R. 0

Do Loop Eg. #3 (7-26)

InputBox Function:

 This function is used to read a single value from the user.

 InputBox("Enter a Number", "Input Window") pops up a window like this:
    _______________________________________________ 
   | Input Window                                  |
   |-----------------------------------------------|
   |                  _______________________      |
   | Enter a Number  |_______________________|     |
   |                                               |
   |_______________________________________________|

 If we use InputBox in an assignment statement, then whatever variable was
 assigned to InputBox will contain what the user typed into the input box.
   I.e., Num = InputBox("Enter a Number", "Input Window")
   If user enters a 3, then the Num variable will contain 3.

 If we put the InputBox function in a loop, then an input box window will
 pop up several times as the program runs.  Each time, a single number
 is entered by the user.

Input Entered:
    3
    5
    2
    8
    -1

Here are the contents of the variables as the program runs.  When
the user types in -1, Smallest will contain the value 2.

NumSmallest
33
53
22
82
-12

Do Loop Eg. #4 (7-28)

Input Entered:
    3
    5
    6
    5
    5
    999

Here are the contents of the variables as the program runs.  When
the user types in -1, Count5 will contain the value 3.

NumCount5
30
51
61
52
53
9993

Do Loop Eg. #5 (7-29)

Input Entered:
    hello
    the
    you
    the
    the
    me
    the
    quit

Here are the contents of the variables as the program runs.  When
the user types in quit, CountThe will contain the value 4.

WordCountThe
hello0
the1
you1
the2
the3
me3
the4
quit4

For Loop Eg. #1 (7-30)

Suppose the user types this sentence into the Text1 text box:
The love letter.

Notice that Count is the counter for the for loop.  The for loop will
end when Count is 16, because this is the length of the sentence
entered, Len(Text1.Text).  Each time the for loop executes, ThisChar
will hold the next character in the Text1.Text string.  Text2.Text
starts as an empty string, and each time in the loop, we concatenate
one letter to the end of it.  In this way, the output is built into
the Text2.Text string.

Here is what happens as the loop executes:

CountThisCharText2.Text
1T"T"
2h"Th"
3e"Tha"
4 "Tha "
5l"Tha l"
6o"Tha lo"
7v"Tha lov"
8e"Tha lova"
9 "Tha lova "
10l"Tha lova l"
11e"Tha lova la"
12t"Tha lova lat"
13t"Tha lova latt"
14e"Tha lova latta"
15r"Tha lova lattar"
16."Tha lova lattar."
Text1.Text"The love letter."
Text2.Text"Tha lova lattar."
This example demonstrates the power of using a for loop whenever you need to go through each character in a string, one character at a time.

For Loop Eg. #2 (7-30)

Let's use the same example, but build the output string in the reverse
order.  We'll also use a few more variables.

Use this code as a guide whenever you want to go through each character
in a string.  This code goes through each character in the string
called InText.  It puts the output into the OutText string.

    ' Declare variables to hold input from Text1 in InText and
    ' output which will be placed in Text2 in OutText.
    Dim InText, OutText As String 
    InText = Text1.Text
    OutText = ""

    ' Store the number of characters in the input
    Dim Length As Integer
    Length = Len (InText)

    Dim Count As Integer        ' Declare for loop counter

    ' Use for loop to count through each character in the input.
    For Count = 1 To Length

        ' Holds the next character in the input string
        Dim ThisChar As String
        ThisChar = Mid (InText, Count, 1)

        ' If character is an "e" add a "a" at start of output string,
        ' otherwise add the original character.  OutText will be in
        ' reverse order.
        If ThisChar = "e" Then
            OutText = "a" & OutText
        Else
            OutText = ThisChar & OutText
        End If
    Next Count

    ' Put the output into the Text2 text box.
    Text2.Text = OutText

Suppose the user types this sentence into the Text1 text box:
The love letter.

Unlike the last example, the character "a" or ThisChar is concatenated
to the front of OutText.  In this way, the output string will be in
reverse order.  Here is what happens as the loop executes:

CountThisCharOutText
1T"T"
2h"hT"
3e"ahT"
4 " ahT
5l"l ahT
6o"ol ahT
7v"vol ahT
8e"avol ahT
9 " avol ahT
10l"l avol ahT
11e"al avol ahT
12t"tal avol ahT
13t"ttal avol ahT
14e"attal avol ahT
15r"rattal avol ahT
16.".rattal avol ahT"
Text1.Text / InText"The love letter."
Text2.Text / OutText".rattal avol ahT"
Note: To try this program in Visual Basic, create two text boxes (Text1 and Text2) and create a command button. Double click on the command button, and copy the above code into the code window. When you run the program (by clicking the command button), type a sentence into input box Text1 and the output will appear in output box Text2.