Chapter 2_6

advertisement
EXPRESSIONS
Expressions 2.6
Expressions are used to calculate values. To create expression you need variables and the
operators.
The operators in Visual Basic include:
Addition +
Subtraction Multiplication *
Division /
Exponents ^
Mathematical Operations
Mathematical expressions many times involve multiple uses of different operators. When an
equation includes many operators the order of operations supercedes any order in the expression.
The order of operations works in Visual Basic the same way as it does in your math class. The
order of operations is also known as the order of precedence. This means that certain
mathematical functions will always be calculated before any other calculation.
The Order of Operations/Precedence could be best remembered by PEDMAS or BEDMAS.
Activity 2.6
Name the 6 orders of operation for arithmetics.
If there are two of the same operators in an expression/equation the all have equal precedence,
however they are evaluated from left to right in which they appear.
f_Calculation = Num1*Num2*Num3*Num4+Num6
Therefore Num1*Num2 would be calculated first.
In programming many times there are comparison or logical operators used along with the in the
arithmetic expressions. The order in this case is arithmetic, comparison and then logical
operators.
Comparison operators all have equal precedence; that is, they are evaluated in the left to right
order in which they appear. (VB help file) Arithmetic and logical operators are evaluated in the
following order of precedence:
Arithmetic
Comparison
Logical
Exponents ^
Equality =
Not
Negation -
Inequality <>
And
Multiplication
Less than <
Or
Integer Division \
Greater than >
Xor
Modular arithmetic Mod
Less than or Equal to <=
Eqv
and Division *, /
Addition and subtraction +,- Greater than or Equal to >= Imp
String concatenation &
Like
Is
The string concatenation operator (&) is not an arithmetic operator, but in precedence it does
fall after all arithmetic operators and before all comparison operators (see Unit 6). Similarly, the
Like operator, while equal in precedence to all comparison operators, is actually a patternmatching operator. The Is operator is an object reference comparison operator. It does not
compare objects or their values; it checks only to determine if two object references refer to the
same object.
Activity 2.6.1
Using Mathematical Functions
You will now create a small program that allows a user to enter in their name and income. Allow
the program to display taxes once the user clicks on the command button. The taxes are
multiplied by 40% since that is the average amount that most people working full time would
pay to the government.
Change the form's Caption to The Tax Man.
You will need the following:
1. 4 labels
2. 3 text boxes
3. 3 command boxes
SEE THE DIAGRAM BELOW
Label1
CAPTION: "Visual
Basic is Fun"
NAME: lblTitle
Command1
Double click on
command1
CAPTION: EXIT
NAME: cmdExit
Label2
Change the following:
CAPTION: Name
NAME: lblName
Text1
Change the following:
CAPTION: not available in for the text tool
NAME: txtName
TEXT: none
Label3
Change the following:
CAPTION: Income
NAME: lblIncome
Text2
Change the following:
CAPTION: not available in for the text tool
NAME: txtIncome
TEXT: none
Label4
Change the following:
CAPTION: Taxes
NAME:lblTaxes
Text3
Change the following:
CAPTION: not available for the text tool
NAME: txtTaxes
TEXT: none
Command2
Change the following:
CAPTION: "CALCULATE YOUR TAXES"
NAME: cmdCalcTaxes
Double Click on the command button and type in the following code.
Private Sub cmdCalcTax_Click()
' these variables are control variables
Dim Income As Integer, Taxes As Integer
Income = txtIncome.Text
Taxes = Int(Income *0.4)
txtTaxes.Text = taxes
End Sub
Int is a common mathematical function used in visual basic. Int() will return an integer value of
the number entered as the argument. With a negative number it returns the first integer less than
or equal to the number.
Command3
Change the following:
CAPTION: CLEAR
NAME: cmdClear
Sub cmdClear_Click()
txtTaxes.Text=""
txtIncome.Text = ""
EndSub
Activity 2.6.2
ADDING IN COLOUR
See appendix E for Lesson on Colour
Create a new label and place it anywhere on the form.
Change the following:
CAPTION: "Click Here for Fun"
NAME: lblResponse
Type in the following code:
Private Sub lblResponse_Click
Sample1.BackColor = QBColor(2)
lblReponse.BackColor=QBColor(13)
lblResponse.Caption = "Very Much Fun!"
End Sub
Run the program and click on CLICK HERE FOR FUN
Note what happens.
Sample1 is the name of the form and BackColor is one of its properties.
Try using QBColor(10) and then QBColor(7) in place of Sample1.BackColor in lblResponse.
Describe what happens in your computer science binders.
1. What does QBColor change?
2. What does Sample1.BackColor change?
3. What does lblResponse.BackColor change?
4. For Commonly Used Mathematical Functions See Appendix D
Review and New Code Statements
Code Statement
Explanation
m_TicketCost = 10.50
numeric data
Movie = "Batman"
string data
f_TipTotal = m_SubTotal
value of another variable
m_FinalCost = m_Price * m_Tax
value of an expression
Interest = m_Principal * 4.25 * Time
value of an expression
Download