CSC104 Programming
Visual Basic Overhead Transparencies
Declaration and Assignment Statements (6-6)
Dim Age As Integer
Age = 23
Age = Age + 1
Dim Addr As String
Addr = "R.R. #5 Cavan"
Dim Price, Total, Tax As Single
Price = 9.99
Tax = 0.15
Total = Price * (1 + Tax)
Price | 9.99
|
Tax | 0.15
|
Total | 11.48
|
Identifiers - Names for Variables, Functions, or Subroutines (6-9)
Bad | Good
|
94CurrSales | Curr94Sales
|
IntRegion West | IntRegionWest
|
IngEast.pop | IngEastPop
|
Dim | Dimension
|
Bad Style | Good Style
|
n | Number
|
totalsales | TotalSales
|
OntarioProvincialSalesTax | OntProvTax
|
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)
TAX | 0.15
|
SubTotal | 10.0
|
Total | 11.5
|
Expressions (6-15)
Dim Result As Integer
Result = 5 + 2 * 3
Result = 6 * Result + 1
Result = - Result
Built-In Functions (6-17)
Dim Input As String
Dim Number As Integer
Input = "2457"
Number = Val (Sum)
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)
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
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.
Count | Junk & Count | Junk
|
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
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.
Count | Count & Junk | Junk
|
1 | 1 & "" | "1"
|
2 | 2 & "1" | "21"
|
3 | 3 & "21" | "321"
|
4 | 4 & "321" | "4321"
|
5 | 5 & "4321" | "54321"
|
Do Loop Eg. #2 (7-25)
Current | Current Mod 2 | Current Mod 3
|
Current=1 | 1 Mod 2 0 R. 1
| 1 Mod 3 0 R. 1
|
Current=2 | 2 Mod 2 1 R. 0
| 2 Mod 3 0 R. 2
|
Current=3 | 3 Mod 2 1 R. 1
| 3 Mod 3 1 R. 0
|
Current=4 | 4 Mod 2 2 R. 0
| 4 Mod 3 1 R. 1
|
Current=5 | 5 Mod 2 2 R. 1
| 5 Mod 3 1 R. 2
|
Current=6 | 6 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.
Num | Smallest
|
3 | 3
|
5 | 3
|
2 | 2
|
8 | 2
|
-1 | 2
|
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.
Num | Count5
|
3 | 0
|
5 | 1
|
6 | 1
|
5 | 2
|
5 | 3
|
999 | 3
|
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.
Word | CountThe
|
hello | 0
|
the | 1
|
you | 1
|
the | 2
|
the | 3
|
me | 3
|
the | 4
|
quit | 4
|
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:
Count | ThisChar | Text2.Text
|
1 | T | "T"
|
2 | h | "Th"
|
3 | e | "Tha"
|
4 | | "Tha "
|
5 | l | "Tha l"
|
6 | o | "Tha lo"
|
7 | v | "Tha lov"
|
8 | e | "Tha lova"
|
9 | | "Tha lova "
|
10 | l | "Tha lova l"
|
11 | e | "Tha lova la"
|
12 | t | "Tha lova lat"
|
13 | t | "Tha lova latt"
|
14 | e | "Tha lova latta"
|
15 | r | "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:
Count | ThisChar | OutText
|
1 | T | "T"
|
2 | h | "hT"
|
3 | e | "ahT"
|
4 | | " ahT
|
5 | l | "l ahT
|
6 | o | "ol ahT
|
7 | v | "vol ahT
|
8 | e | "avol ahT
|
9 | | " avol ahT
|
10 | l | "l avol ahT
|
11 | e | "al avol ahT
|
12 | t | "tal avol ahT
|
13 | t | "ttal avol ahT
|
14 | e | "attal avol ahT
|
15 | r | "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.