Definition of a variable (1 minute)

advertisement
Variables and their Use in Programming
Definition: A variable is a container in the computer (memory location) that can hold a
value and the value can be operated upon. It has both a name and a value.
Example: num1 = 5, where num1 is the name of the variable and 5 is the value that it
holds. Another example is fname = “Roger”
Now, each programming language has a specific way to create variables. In VB.Net you
use the key word “Dim” before the variable name, followed by a data type of that
variable. So what is a data type? A data type identifies the type of data that particular
variable can store. The type can be a numeric value such as 5 (integer) or 5.5 as
(decimal), “Harry” as a (string) and so on. So in our earlier example, num1 = 5, would be
declared in VB.net as
Dim num1 as Integer
Num1 = 5
Dim fname as String
Fname = “Roger”
Variable Naming Restrictions
1. Variables must start with a letter or an underscore.
2. No other special characters can be used in creating a name for a varibale. The
only character other than letters or numeric digits is the underscore which can be
used in naming a variable.
3. Maximum length of a variable is 255 characters
4. Key words ( will be discussed in later lessons) cannot be used as variable names.
Student input: Now, you try to write the correct statement for a variable called Lname
as string and assign a value of “Jones” to it .
Expression: An expression is a formed using values or variables and some kind of
operators, and saving the result in another variable. For example
num3 = 5 + 4 – expression with values and an addition operator
num1 = 5
num2 = 3
num3 = num1 + num2 – expression with variables and an addition operator
Student input: Now, try and create an expression using values 2 and 5, and a
multiplication operator. Save the result in a variable called product.
Input/Output (I/O): In any programming language there must be a provision for the user
to enter input and the programming language to manipulate the variable and return the
output. Such a mechanism is called I/O. Let us look at how VisualBasic.net deals with
I/O.
VB.Net provides many ways to collect input from the user, such as text boxes, input
boxes. To collect user input from a user for a variable called num1, we can use an
Inputbox like so:
Num1 = InputBox(“Enter a value for num1”) , where the Input box prompts the user to
enter a value and assigns this value to a variable called Num1.
Assume that the user input a value of 5 for num1 into the input box.
Assume also that you want your program to create an expression that will square this
num1 and save it in another variable called sqrnum1. This is done as follows
Sqrnum1 = 5 * 5
Lastly, assume that you want to output this value to the user on the monitor. Vb.Net has
several ways to display output. These include text boxes, labels, Message boxes and so
on. We will use a message box to display the output and that is done as follows:
MsgBox(sqrnum1)
Activity1: Follow the example given above and create a program that will calculate the
area of a room given two inputs. Here is the procedure to accomplish this task.
1. Get two numeric values from the user and save them in variables called length and
width. Make sure you use the DIM key words to define your variables followed by the
datatype. Use the inputbox method to complete this task. (Hint: You will need two
inputboxes, one for each numeric value)
2. Next write an expression that will calculate the product of these two numbers and save
it in a variable called area.
3. Finally use the MsgBox method to display the area to the user.
Activity2: You are required to perform a conversion from Kilograms to Pounds. Your
user wants to know the pound equivalent to the Kilograms he enters. Your program must
read the user input and display the pound equivalent value. Your hint: 1 kilogram = 2.2
pounds. Write the required steps to perform this calculation in steps as shown in activity1
and save it on your computer. You have two dates to keep in mind:
1. Dead line date to submit your assignment to the dropbox. This date is a week
from the date the assignment is posted.
2. Dead line date to get help from the T/A for questions on your assignment. This
date is day#3 from the date the assignment is posted. This is mainly to your
advantage, you can send the file to the T/A and check if you are on the right track,
or need some help.
This activity is not a self - check exercise. The student will actually submit this for a
grade.
Remarks:
Of course, the students will do all this actually on the computer after hand writing the
algorithm to see how the code works. Hand writing and sketching the algorithm is a
crucial part of programming which must be addressed and stressed. Hand writing the
code is phase one of the learning task, which will be followed by phase two in the next
lesson.
Download