Lab 10: When a
decision must be made…..
Introduction to Conditionals
Spring 2001
Reading to be done prior
to Lab 10:
·
p. 97-105 in Chapter 5
of Computer Programming Fundamentals with Applications in Visual Basic 6.0
·
p. 151 -154 in Chapter 7
of Computer Programming Fundamentals with Applications in Visual Basic 6.0
Introduction:
You have been introduced to the idea of conditionals as a way to write code that allows a program to “make choices” based on user input. Today we will work on a simple program that allows a user to enter the temperature outside, and based on the information, the computer will give the appropriate response.
Objectives:
TO DO:
· 3 Labels
· 2 Command Buttons
·
1 Text Box
Name your form frmTemp. Make the caption “Lab 10”.
· Name your first label lblQuestion and give it the caption “What is the temperature outside?”
· Name your second label lblInst and give it the caption “Enter your answer here:”
· Name your third label lblResponse and leave the caption blank
· Name your first button cmdSubmit and give it the caption “Submit”
· Name your second button cmdReset and give it the caption “Reset”
· Name your text box txtAnswer and leave the caption blank. Set the TabIndex property in the Properties Window to 0.
Your form will look something like
this when you are done (use the colors and fonts of your choice):
.
· Open the code window
· At the very top of the code window, type in the debugging help line: Option Explicit
· Underneath that line, declare the variable userAnswer as an integer
· We will initialize this variable in a moment
The Scenario:
But what exactly do we want to have happen in our program?
When the program runs, the form will display the label that asks a user to
enter the temperature. Once they enter
their answer and click the submit button, the program will assign the value
that they enter to the declared variable userAnswer.
The value that is stored in userAnswer will be compared to a value hard
coded into the IF/THEN/ELSE statement.
If the value is greater than 80, the caption of the second label (lblResponse)
will be assigned the string value:
“Man, it’s hot. And humid, too!”
If the value is less than 60, the caption of lblResponse will be assigned the string value:
“Brrrrrr, my feet are cold-turn up the heat, will ya?”
If the value entered does not match either of these conditions (meaning it falls between 60 and 80), the caption of lblResponse will be assigned the string value:
“AHHHHHHHH, just right!”
All of the above description will be transformed into statements that sit inside the cmdSubmit control’s click event.
·
Assign the value of the text in txtAnswer to the
variable userAnswer
·
Write an if/then/else statement that checks if the
value in userAnswer is greater than 70 and if it is, assign the string
value:
“Man, it’s hot. And humid, too!”
to the caption of lblResponse.
(Bonus: The
background color of the form could also change to an appropriately warm color!)
Elseif (note the use of this
term!) the value in userAnswer is below 60 assign the string value:
“Brrrrrr, my feet are
cold-turn up the heat, will ya?”
to the caption of lblResponse.
(Bonus: The background color of the form could also
change to an appropriately cold color!)
Otherwise (ELSE), assign the string value:
“AHHHHHHHH, just right!”
to the caption of lblResponse.
The set up of the statement should
have this structure (you will supply the operands and the statements where the
italics are):
If <some statement runs true> Then
<perform
an action>
ElseIf <some other statement runs true>
Then
<perform a different action>
Else
<perform another action >
End If
Your TA will walk you through the creation of the full conditional statement.
txtAnswer.Text = " "
lblResponse.Caption = " "
This will clear out the text box
and the Label caption so that someone can enter another temperature.
EXTRA PRACTICE (Not to be turned
in)
Work on the following steps outside of
lab. You will need these skills to make
project 2, parts A and B look as sharp as possible.
lblResponse.Visible = False
In the cmdSubmit
click event procedure, add the above statement again, but change the visibility
to true.
lblResponse.Caption = “Some text ” & userAnswer & “ some more text”
NOTE: At this point a lot of your code may be running off the current code window space. There is no word wrap function in VB. To put one statement on two or more lines, you have to use the concatenation operator and an underscore or you will get a run time error.
EXAMPLE:
lblResponse.Caption = “Some text that goes for a long time, but then I ” & _
“want to continue on the next line, so I have to ” & _
“do this with the concatenation operator, a space ” & _
“ and an underscore.”
Using more than one variable:
· Declare a variable called celsius as integer at the top of your program. You will initialize this variable in a moment.
Dim celsius as
Integer
·
Add a label to your form. Increase the size of your form length so that you can add it
below lblResponse. Name the label lblCelsius. Change the Visibility property to False.
·
Add the following 3 statements to your cmdSubmit
click event (enter them after your End If statement).
What are you doing in each of these statements?
lblCelsius.visible = True
celsius = (5 / 9) * (userAnswer - 32)
lblCelsius.Caption
= “That’s actually “ & celsius & “ degrees Celsius.”
Congratulations!!!! If you have completed all 16 steps in this lab successfully, you are well on your way to developing a strong set of capabilities and skills that will help you anywhere you encounter technology.