Lesson 3
(Chapter 5)

I. Making decisions

    If.. Then

If (boolean expression) Then

    do something
    do something else
    etc.

End If

Example

            If (txtAnswer.Text = "Washington") Then

                        lblFeedback.Caption =  "You are correct."

            End If

 II. Logical (Relational) Operators

Operator Meaning
= equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to
<> not equal to

III. If..Then.. Else

 If..Then..Else

If (boolean expression) Then

            do something
            

Else

            do something Else

End If

 Example

           If (txtAnswer.Text = "Washington") Then

                        lblFeedback.Caption =  "You are correct."

            Else

                          lblFeedback.Caption = "Wrong!"

            End If

 IV. If..Then..ElseIf

If (boolean expression) Then

           

ElseIf boolean expression Then

           

Else

           

End If

Example

            If (txtAnswer.Text = "Washington") Then

                        MsgBox "You are correct."

            ElseIf (txtAnswer.Text = "Jefferson") Then

                        MsgBox "Close, but he was the second president."

            Else

                        MsgBox "Wrong!"

            End If

V. Converting Data Types

In Visual Basic, it is often necessary to convert one data type to another. For example, if you enter a number in a textbox, it is stored as text, not a number. If you need to convert that text to a number, use the Val( ) function. The Val( ) function converts the text between the parentheses to numbers. For example, I CANNOT say this:

lblAnswer.Caption = txtNum1.Text + 1 

because txtNum1 is text and cannot be added to 1

However, I can say

lblAnswer.Caption = val(txtNum1.Text) + 1 

because Val has converted the text to a number

To do the opposite (convert a number to a string) use the Str( ) function. The following is NOT legal:

lblAnswer.Caption = "5 + 3 = " + 5 + 3 

because you can't "add" OR concatenate mixed data types.

However, the following IS legal

lblAnswer.Caption = "5 + 3 = " + Str(5 + 3)

Notice the 3 different meaning of the plus sign in the statement!

bullet

the first one (inside the string) is a literal- "just print me."

bullet

the second one is concatenation (adding strings together)

bullet

the third one is plain old addition!

Class Project 1: Grade Calculator

Write a program that asks the user to enter a test score between 1 and 100. The program then displays the letter grade for the score.

Step 1: The Interface

Step 2: Set the Properties

Object Properties
frmGradeCalculator caption = "Enter Numerical Grade"
txtNumGrade text = "" (blank)
lblLetterGradeLabel  caption = "Letter Grade"
lblLetterGrade caption  = "" (blank)
cmdCalculate caption = "Calculate"
cmdEnd caption = "End"

Step 3: Write the Code

Psuedo Code: Actual Code:

Private Sub cmdCalculate_Click()

if the grade is above 90 then

the letter grade is "A"

else if the letter grade is 80 or above then

the letter grade is "B"

else if the letter grade is 70 or above then

the letter grade is "C"

else if the letter grade is 60 or above then

the letter grade is "D"

else 

the letter grade if "F"

End Sub

Private Sub cmdCalculate_Click()

If (txtNumGrade.Text >= 90) Then

       lblLetterGrade.Caption = "A"

ElseIf (txtNumGrade.Text >= 80) Then

       lblLetterGrade.Caption = "B"

ElseIf (txtNumGrade.Text >= 70) Then

       lblLetterGrade.Caption = "C"

ElseIf (txtNumGrade.Text >= 80) Then

       lblLetterGrade.Caption = "D"

Else: lblLetterGrade.Caption = "F"

End If

End Sub

Generating Random Numbers

Sometimes programs need to simulate some degree of randomness. To generate random numbers, Visual Basic uses the Rnd function. The Rnd function generates a random number greater than or equal to 0 and less than 1.

0 >= Rnd < 1

This means that the result of the Rnd function could return any number between .00000000 and .99999999 (the number of places to the right of the decimal might vary, but you get the idea). Let's develop a program to generate and display random numbers. The interface is simple:

Clicking the Random button generates a random number using the code:

lblAnswer.Caption = str(Rnd)

Some typical answers might be:

.0012121
.4211000
.625540
.980032

Now suppose that I want a random number between 1 and ten. By multiplying each of the numbers above by 10, I would get the following:

0.012121
4.211000
6.25540
9.80032

Therefore we can replace the code behind the Random button with

lblAnswer.Caption = str(Rnd * 10)

This is a bit closer to the 1 to 10 range that we need. If we could magically "chop off" the decimal, we would be even closer. The Int function does exactly that- it returns the integer part of a real number. Thus, the code:

lblAnswer.Caption = str(Int(Rnd * 10))

would produce:

0
4
6
9

Now if we just add one to the above results, we have a number between 1 and 10 (easy, right!). The code:

lblAnswer.Caption = str(Int(Rnd * 10) + 1)

produces a random number between 1 and 10.

We will run the program several times and you will notice the interesting fact that Visual Basic seems to generate the same random numbers each time you run it! This is a useful feature for debugging, but once the program is working, you should add the Randomize statement to Form_Load to get new random numbers each time.

VII: Scope of Variables

Variables may either global or local. This is referred to as the scope of the variable and indicates where the variable is "known." So far, the variables we have defined are known only to the subroutine they were defined in. So the statement

Private Sub DimDemo()

Dim intA As Integer
intA = 7
MsgBox "The number is " + Str(IntA))

End Sub

(note the object MsgBox uses the stir() function to concatenate two strings!)

Global variables are know to all of the objects on the form. They are declared in the General Declarations section using the Private keyword instead of Dim.

Private intA As Integer 'known to all objects on the form!

Global constants are also declared in General Declarations using the General Declarations keyword.

Private Const dblPi = 3.14

VIII. Algorithms

The series of steps used to solve a problem is called an algorithm.

Develop a program that plays a number guessing game between the user and the computer. The user will guess the number and the computer will use a message box to tell the user if their guess is "Too high," "Too low," or "Right On!!" The interface looks like this:

If the computer generates the random number in the Guess routine, it will change each time it is clicked. This makes the game VERY unfair! Therefore, the variable for the random number is declared in the General Declarations section. The random number is defined when the form is loaded. To find the General Declarations section, click on the drop down menu in the Editor window.

The code is:

Private intSecretNumber As Integer

In Form_Load, assign the Random Number

intSecretNumber =  Int(Rnd * 100) + 1

The algorithm for determining if the guess is too high or too low is:

convert txtInput.Text to an Integer (intGuess)
if (the integer guessed = the secret number)

display message "Right On!!"

ElseIf (the integer guessed > the secret number)

display message "Too High"

Else

display message "Too Low"

Homework

Chapter 5: 1, 2, 3, 4, 7, 9, 10