Numbers, Variables and strings - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Numbers, Variables and strings

Description:

Frequently numbers are called numeric constants ... Scroll bars are useful controls and allow the user to enter information which ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 18
Provided by: mariamoni
Category:

less

Transcript and Presenter's Notes

Title: Numbers, Variables and strings


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

2
  • 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.

3
  • 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
  • Varnum
  • 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.

4
  • 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 2distance
    75, (abc)/6.
  • Example The following program displays the value
    of an expression.
  • Private Sub cmdCompute_Click()
  • picResults.Cls
  • A5
  • B4
  • picResults.Print A(2B)
  • End Sub

5
  • Run, and then click the command button. The
    following is displayed in the picture box.
  • 30
  • If var is a variable, then the statement
  • varexpression
  • First evaluates the expression on the right side
    and then assigns its value to the variable. For
    this reason a statement such as
  • nn1
  • Is meaningful. In terms of memory locations, the
    statement retrieves the value of n from ns
    memory location, uses it to compute n1, and then
    places the sum back into ns memory location.

6
  • 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
  • strVar1xy..z
  • Assigns the string constant xy..z to the
    variable.

7
  • 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
  • strVar2strVar1
  • 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
  • Interestrate0.0655
  • principal100
  • Phrase The balance after a year is
  • picBalance.Print phrase(1interestrate)principal
  • End Sub

8
  • 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 .

9
  • 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.

10
  • 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.TextstrVar
  • 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.

11
  • 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
  • numVarVal(txtBox.Text)
  • And
  • txtBox.TextStr(numVar)
  • Can be used to assign the contents of the text
    box to the numeric variable numVar and vice
    versa.

12
  • 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.
  •  

13
  •      
  •  

Form Name frmConversion Caption
Conversion   Command Button Name cmdExit Capt
ion Exit Text Box Name txtMiles   Text
Box Name txtKilometers  
Label Name lblMiles Caption Miles
Label Name lblKilometers Caption
Kilometres Horizontal Scroll Bar Name hsbMiles
14
  • 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

15
  • 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

16
  •  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

17
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com