chapter 3 - visual basic schneider numeric variables used to store numbers value is assigned by a...

18
Chapter 3 - Visual Basic Schneider Numeric Variables • Used to store numbers • Value is assigned by a statement of the form: numVar = expression • The variable must be on the left and the expression on the right.

Upload: chester-ross

Post on 19-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Numeric Variables

• Used to store numbers • Value is assigned by a statement of the form:

numVar = expression

• The variable must be on the left and the expression on the right.

Page 2: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Assignment Statement:

• The statement var = expr assigns the value of the expression to the variable

• tax = 0.02 * (income - 500 * dependents)• sum = 2 + x + 4.6 + y

Page 3: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Valid Numeric Variable Names:timeElapsed taxRatespeedncelsius

Page 4: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Invalid Numeric Variable Names:• maximum/average• 1stChoice• square yard

Page 5: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Valid Assignment Statements

• count = count + 1• num = 5 • count = count + num /2

Page 6: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Invalid Assignment Statements• 10 = count• count + 1 = count

Page 7: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Visual Basic Print Statement• Print is a method used to display data on the

screen or printer.• Can be used to display values of variables or

expressions

Page 8: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Examples of Print Statements

Private Sub cmdCompute_Click() picResults.Print 3 + 2 picResults.Print 3 - 2 picResults.Print 3 * 2 picResults.Print 3 / 2 picResults.Print 3 ^ 2 picResults.Print 2 * (3 + 4)End Sub

Page 9: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Examples of Print Statements• picOutput.Print speed• picOutput.Print taxRate• picOutput.Print “Class average is”; total / 3

Page 10: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Examples of Print Statements x = 15 y = 5picOutput.Print (x + y) / 2, x / y

Output: 10 3

Page 11: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

String Constants:

• A sequence of characters treated as a single item

• The characters in a string must be surrounded by double quotes (“ ”)

Page 12: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Valid String Constants

“A rose by any other name”“9W”“134.23”“She said, ‘stop , thief!’ ”

Page 13: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Invalid String Constants

‘Down by the Seashore’“134.24“She said, “Stop, thief!””

Page 14: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

String Variables• A string variable stores a string.• The rules for naming string variables are

identical to those for naming numeric variables.

• When a string variable is first declared, its value is the empty string.

Page 15: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

String Variable Example

Private Sub cmdShow_Click() picOutput.Cls phrase = "win or lose that counts." picOutput.Print "It's not whether you "; phrase picOutput.Print "It's whether I "; phraseEnd Sub

Page 16: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Concatenation• Two strings can be combined by using the

concatenation operation.• The concatenation operator is the ampersand

(&) sign.

Page 17: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Examples of Concatenation:• strVar1 = “Hello”

strVar2 = “World”picOutput.Print strVar1& strVar2

• txtBox.Text = “32” & Chr(176) & “ Fahrenheit”

Page 18: Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable

Chapter 3 - Visual Basic Schneider

Declaring Variable Types

• Use the Dim statement to declare the type of a variable.

• Examples: Dim number As Integer Dim flower As String

Dim interestRate As Single