Numbers, Variables and strings

Download Report

Transcript Numbers, Variables and strings

Numbers, Variables and strings
• 3.3 Numbers:
Much of the data processed by computers consists of
numbers. Frequently numbers are called numeric
constants
• The five arithmetic operations in Visual Basic are
addition, subtraction, multiplication, division, and
exponentiation.
Multiplication
Exponentiation
a*b
a^b
Addition
Subtraction
Division
a+b
a-b
a/b
• One way to show a number on the screen is to
display it in a picture box. If m is a number, then the
instruction
picBox.Print m
Displays the number m in the picture box picBox.
Print is a reserved word and the Print operation is
called a method. Another important method is Cls.
The statement
picBox.Cls
Erases all text and graphics from the picture box
picBox.
• A variable is a name that is used to refer to an item
of data. The value assigned to the variable may
change during the execution of the program.
• In Visual Basic, variable names can be up to 225
characters long, must begin with a letter, and can
consist only of letters, digits, and underscores. Visual
Basic does not distinguish between uppercase and
lowercase letters used in the variable names.
• If var is a variable and num is a constant, then the
statement
Var=num
assigns the number num to the variable var. Such
statement is called an assignment statement. The
computer sets aside a location in memory with the
name var and places the number num in it.
• The statement
picBox.Print var
Displays the value of the variable var in the picture box.
A combination of constants, variables, and arithmetic operations
that can be evaluated to yield a number is called a numeric
expression. Expressions are evaluated by replacing each
variable by its value and carrying out the arithmetic operations.
For example 2*distance +75, (a+b+c)/6.
Example: The following program displays the value of an
expression.
Private Sub cmdCompute_Click()
picResults.Cls
A=5
B=4
picResults.Print A*(2+B)
End Sub
• Run, and then click the command button. The
following is displayed in the picture box.
30
• If var is a variable, then the statement
var=expression
First evaluates the expression on the right side and
then assigns its value to the variable. For this reason
a statement such as
n=n+1
Is meaningful. In terms of memory locations, the
statement retrieves the value of n from n’s memory
location, uses it to compute n+1, and then places the
sum back into n’s memory location.
• A string is a sequence of characters that is treated
as a single item. For example phrases, words,
names, telephone numbers, social security numbers.
• A string variable is a name used to refer to a string.
The allowable names of string variables are identical
to those of numeric variables. The value of a string
variable is assigned or altered with assignment
statements and displayed with Print methods just like
the value of a numeric variable.
• If x,y,..,z are characters and strVar1 is a string
variable, then the statement
strVar1=“xy..z”
Assigns the string constant xy..z to the variable.
• The statement
picBox.Print “xy..z”
Or
picBox.Print strVar1
Displays the string xy..z in a picture box.
If strVar2 is another string variable, then the statement
strVar2=strVar1
Assigns the value of the variable strVar1 to the variable strVar2. The
value of strVar1 will remain the same.
Example (The form design consists of a command button and picture
a picture box)
Private Sub cmdCompute_Click()
picBalance.Cls
Interestrate=0.0655
principal=100
Phrase= “The balance after a year is “
picBalance.Print phrase;(1+interestrate)*principal
End Sub
• Run, and then click the command button. The
following is displayed in the picture box.
• The balance after a year is 106.65
Declaring Variable Types
There are several advantages to specifying the type
values that can be assigned to a variable. A
statement of the form
Dim variableName As string
Specifies that only strings can be assigned to the
named variable. A statement of the form
Dim variableName As Single
Specifies that only real numbers of single-precision can
be assigned to the named variable. A single-precision
numeric variables can hold numbers of magnitude
from as small as 1.4x10-54 to as large as 3. 4x10-38 .
• A Dim statement is said to declare a variable.From
now on we will declare all variables. However, all the
programs run correctly even if the Dim statements
are omitted.Declaring variables at the beginning of
each event procedure makes programs easier to
read and helps prevent certain type of errors.
• Example
Dim interestrate As Single
Dim principal As Single
Dim phrase As String
Dim interestrate As Single, principal As Single, phrase As String
• Another type of numeric variable, called integer, can hold only
integers from –32768 to 32767.
• Example
Dim intVar As integer
Other types of numeric variable are Long, Double and
Currency (see Appendix C of the textbook)
Input and Output by means of Text Boxes
• The contents of a text box is always a string. Thus
• strVar =txtBox.text
And
• txtBox.Text=strVar
Can be used to assign the contents of the text box to
the string variable strVar and vice versa.
• Numbers are stored in text boxes as strings.
Therefore, they should be converted to numbers
before being assigned to numeric variables.
• If str is a string representation of a number, then
Val(str)
Is that number.
• If num is a number, then
Str(num)
Is a string representation of the number.
Example:
The statements
numVar=Val(txtBox.Text)
And
txtBox.Text=Str(numVar)
Can be used to assign the contents of the text box to
the numeric variable numVar and vice versa.
• The Conversion Program
Scroll bars are useful controls and allow the user to
enter information which can be used by the program.
We are going to develop a program such that the
user can specify a distance in miles which the
program converts into kilometres.
·
Form
Name frmConversion
Caption Conversion
Command Button
Name
cmdExit
Caption
Exit
Text Box
Text Box
Name
txtMiles
Name
txtKilometers
Label Name lblMiles
Caption Miles
Label Name lblKilometers
Caption Kilometres
Horizontal Scroll Bar
Name hsbMiles
• The code for the cmdExit button is the following:
Private Sub cmdExit_Click()
Beep
End
End Sub
• Run the Project
• Firstly, we declare the two variables in the General
Declarations section like this:
Turning to the code for the hsbMiles scroll bar, we
have:
Private Sub hsbMiles_Change()
Miles = hsbMiles.Value
txtMiles.Text = Miles
Kilometers = 1.6 * Miles
txtKilometers.Text = Kilometers
End Sub
• Run the program
Now use the following code for the hsbMiles scroll
bar:
Private Sub hsbMiles_Scroll()
Miles = hsbMiles.Value
txtMiles.Text = Miles
Kilometers = 1.6 * Miles
txtKilometers.Text = Kilometers
End Sub
• Run the program