FIT 100

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:

  1. To use the VB IDE to create a form using radio buttons, labels, command buttons and text boxes.
  2. To write code that incorporates conditionals as part of the process to choose between two given options.
  3. To understand where computation statements must be placed for a program to run error free.

 

TO DO: 

  1. Open up a new project in Visual Basic.  If you have already gone ahead and started a project and form for this lab, then open it up and check the names of your objects against the ones given in Steps 3 and 4.

  2. Rename your project Temperature. 

 

  1. Create a form with the following controls on it:

 

·         3 Labels

·         2 Command Buttons

·         1 Text Box

Name your form frmTemp.  Make the caption “Lab 10”.

  1. Spend a few minutes changing the names and captions of your objects.  Change the colors and fonts to something you like as well.

·         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):

. 

  1. We want the user to be able to enter in the temperature outside.  Assuming that they enter in only a number and not text (that’s a whole other issue!), we want the program to look at the value entered.  If the value is greater than 80, the program will respond one way.  If it is below 60, it will respond a different way.  If it is between 60 and 80, there is a third response!  Because we have several conditions as possibilities, the If/Then/Else general condition statement will be used. 

  2. Before worrying about the statement, think of how you will store the value that a user enters.  Let’s use the variable name userAnswer to hold any number entered into the text box.

·         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

 

  1. Save your project and form.

 

  1. There is a control on the form called cmdSubmit.  The most obvious place for us to put statements (code that we would like to run) is in the click event of the Submit button.

 

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. 

 

  1. Now select the cmdSubmit object from the drop down menu on the left.  The Click event should appear in the procedures menu on the right.  Write a statement inside the procedure to do each of the following:

·         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.

 

  1. Try running your program.  Enter in a temperature and click Submit.

 

  1. What happens when you click on Reset?  Nothing right now, because there are no code statements set inside the click event procedure.  When someone clicks the Reset button, you want the lblResponse to disappear and the text in the text box to go away.  So, inside of the cmdReset_Click() procedure, add the following statements:

txtAnswer.Text = " "

lblResponse.Caption = " "

 

This will clear out the text box and the Label caption so that someone can enter another temperature.

  1. Run your program and test temperatures in all three areas (above 80, below 60 and between 60 and 80).  Is it working? 

    What happens if you enter your name instead of a number? 

    You probably will get a run time error.  Why?  Because you have declared your variable userAnswer as an Integer and you are comparing it to integers in your IF-THEN-ELSE statement.  We will learn more about how to trap user input errors before they break your program in the labs to come.  For now, just enter in a number!!!!!!

 

 

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.

 

  1. Some additional elements that will make the program look sharper will be controlled in several different event procedures.  When the program first starts, set the visible properties of the lblResponse object to false using a code statement, not the properties window. Enter the following statement in the Form_Load event procedure.

lblResponse.Visible = False

 

In the cmdSubmit click event procedure, add the above statement again, but change the visibility to true.

  1. Using the idea of concatenation discussed in lecture, change the string values that you currently have assigned to the lblResponse captions in the if-then-else statement. Include the value entered by the user.  The syntax will look something like this:

 

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:

  1. The formula for converting Fahrenheit to Celsius is C=(5/9)(F-32).  How could we use the integer value that our user enters in this formula and display the converted value in another label?  One way would be to substitute the F in the formula with our userAnswer variable and also declare another variable as integer to hold the value of that formula.

 

·         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.”

  1. Run your program.

  2. After you click Submit, and then Click Reset-can you see any additional statements you might want to add to improve your user interface?

 

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.