Title: Chapter 3c Math Calculations and Operators
1Chapter 3cMath Calculations and Operators
3c
IS 2020
CI 9008 George Zolla
Prof
. Gary Porter
grporter_at_
nps
.navy.mil
Naval Postgraduate School
Monterey, CA
2Assigning Values to Variables
- Variablename value
- Where
- variablename is the descriptive name of the
variable - is the assignment operator
- value is the value the variable will contain
- Examples
- Number1 5
- FirstName Steve
- LoanAmount 67.38
- Length 17.8
- Note Order is important. Variable name always on
the left, and value on the right. -
3Declaring Variables
- Variable Declaration Statement
- Dim variable name as data type
- Dim LastName as String
- Dim Quantity as Integer
- Dim Quantiy as Integer, LastName as String
4 Visual Basic Arithmetic
Operators Mathematics Math Example Visual
Basic Visual Basic Example Exponentiation
52 (caret) 52 Negation, ? ?5 -
(minus sign) -5 Multiplication, ? 5 ? 2
(asterisk) 52 Division, ? 5 ? 2 /
(forward slash) 5/2 Addition, 5
2 (plus sign) 52 Subtraction, ? 5
? 2 - (minus sign) 5-2 Integer Division \
(backward slash) 5\2 Modulo Division Mod 5 Mod
2
Dim sglMean as Single Dim Intdigit as Integer Dim
sglNum as Single Dim strFirstName as String Dim
strFullName as String Intdigit 5 sglNum
4.4 strFirstName Visual sglMean (Intdigit
3) / 2 sglNum sglNum 2 Intdigit digit Mod
3 strFullName strFirstName Basic
5Visual Basic Arithmetic Operator Precedence
Rules (Order of
Operations) 1) Exponentiation () 2) Negation
(-) 3) Multiplication and division (, /) 4)
Integer division (\) 5) Modulo division
(Mod) 6) Addition and subtraction (, -) 7)
String concatenation () When multiplication and
division occur together in an expression, each
operation is evaluated as it occurs from left to
right. Likewise, when addition and subtraction
occur together in an expression, each operation
is evaluated in order of appearance from left to
right. The string concatenation operator () is
not an arithmetic operator, but it does fall
after all arithmetic operators in
precedence. Parenthetical expressions may be
used to override predefined precedence rules.
X 5 4 10 / 5 3 4
X (5 4) (10 / 5) (3 4)
6String Assignment
- Dim firstName as String
- Dim lastName as String
- Dim fullName as String
- firstName Tom
- fastName Smith
- fullName Tom Smith or
- fullName firstName lastName
- Concatenation Operator
7- Common Visual Basic String Functions
- LCase(x) Returns the string x converted to
lowercase. - Left(x, n) Returns the leftmost n characters of
string x. - LTrim(x) Returns the string x without leading
spaces. - Mid(x, s, n) Returns the middle n characters of
string x starting at character number s. - Right(x, n) Returns the rightmost n characters of
string x. - RTrim(x) Returns the string x without trailing
spaces. - Trim(x) Returns the string x without leading and
trailing spaces. - UCase(x) Returns the string x converted to
uppercase. - Len(x) Returns the number of characters in
string x or number of bytes require to store
variable x. - Trim example
8More on the Val Function
- When Val(argument) evaluates argument string
- it starts at the left-most character of the
string and starts converting - digit, decimal point or sign characters to
numeric format. - Upon reaching another type character of the end
of the string the Val function stops.
9Examples of Val
- Argument
- 0
- 123.45
- 100
- 1,000
- A123
- 123A
- -123
- 123
- 12.34.5
- Return value
- 0
- 123.45
- 0
- 1
- 0
- 123
- -123
- 123
- 12.34
10Date Time
- Date Function (Pg 103, 636)
- Date
- lblDate.Caption Date
- lblDate.Caption FormatDateTime(Date,
vbLongDate) - Time Function (Pg 103)
- Time
- lblTime.Caption Time
- lblTime.Caption FormatDateTime(Time, vbLongTime)
11Payment
- Payment function (Pg. 639)
- Pmt(interest/month, total months, -loan amount)
- lblMonthlyPayment.Caption Pmt(sngMonthlyRate,
sngMonths, curAmount) - sngMonthlyRate Val(txtRate.Text) /1200
- sngMonths Val(txtYears.Text) 12
- curAmount -Val(txtAmount.Text)
12Formatting Data
- FormatCurrency (Pg. 100)
- FormatNumber
- FormatPercent
- ForamtDateTime (Date example)