Operators • Same as standard math operators with 2 exceptions – * for multiplication – ^ for exponentiation • Order of operations strictly observed – Please Excuse My Dear Aunt Sally – And • Mod operator returns the remainder – Example x = a mod b gives the remainder when a is divided by b • A series of numeric values and operators is know as an “expression” 2 + 6 -------- = ( 2 + 6 ) / 4 4
36
Embed
Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Operators
• Same as standard math operators with 2 exceptions– * for multiplication– ^ for exponentiation
• Order of operations strictly observed– Please Excuse My Dear Aunt Sally
– And
• Mod operator returns the remainder – Example x = a mod b gives the remainder when a is divided by b
• A series of numeric values and operators is know as an “expression”
2 + 6-------- = ( 2 + 6 ) / 4 4
Variables
• Data containers (bags or shoeboxes)
• Variable names – Up to 16,383 characters long, – Begin with a letter or an underscore, and – Consist of letters, digits, and underscores
only.
• Declared (created) with Dim statement– Dim varname as type
2 numeric variable types
• Integer: contains +/- whole numbers– Approx range = -2 billion to 2 billion
• Double: contains +/- floating point (decimal) numbers– Approx range = -1.8*10^308 and 1.8*10^308 – Use scientific notation for large values hence
“floating” point
Assignment Statements
• “=“ used to place data into variable (container)
varname = value (dump truck)
• Value can be – literal: 3.5 for example
• (know as constant in algebra)
– other variable– expression: 3 + 5 or x + 2
List Box
• Easy way to show output• Contains “items” displayed one per line• Use list box tool to place on form• Control contents with
– lst.Items.add ( ) method• Data placed inside parentheses displayed in box
– lst.Items.clear ( )• Removes all items from list
– With block• Shorthand• With..EndWith boundaries
With lstResults.Items .add(~~) or .clear( )EndWith
Characters
• Character = any written symbol– A..Z, a..z, 0..9, !, @, etc.– Spaces, return and tab– Some other special use items
String
• A sequence of characters
• String literal = “qwerty”
• String variables– Dim varName As String
• Dim today As String • today = "Monday"
Concatenation
• Joining strings
• & is the concatenation operator– Dim quote1, quote2, quote As String – quote1 = "The ballgame isn't over, " – quote2 = "until it's over." – quote = quote1 & quote2 – txtOutput.Text = quote & " Yogi Berra"
Using string variables
• If x, y, ..., z are characters and strVar is a string variable, then the statement– strVar = "xy...z"
• assigns the string literal xy...z to the variable and the statement– lstBox.Items.Add("xy...z")
• or– lstBox.Items.Add(strVar)
• are equivalent.
Using Text Boxes for Input and Output
• Input– strVar = txtBox.Text
• Output – txtBox.Text = strVar
Numeric to string conversion
• String data can not be used in calculations
• String data must be converted to numeric– dblVar = CDbl(txtBox.Text)– txtBox.Text = CStr(dblVar)
Private Sub btnAdd_Click(...) Handles Private Sub btnAdd_Click(...) Handles btnAdd.Click btnAdd.Click
'Display the sum of two numbers 'Display the sum of two numbers lstResult.Items.Clear() lstResult.Items.Clear()
ExplainPurpose() ExplainPurpose()
lstResult.Items.Add("") lstResult.Items.Add("")
DisplaySum(2, 3) DisplaySum(2, 3)
End Sub End Sub
Sub DisplaySum(ByVal num1 As Double, Sub DisplaySum(ByVal num1 As Double, ByVal num2 As Double) ByVal num2 As Double)
'Display numbers and their sum 'Display numbers and their sum lstResult.Items.Add("The sum of " & num1 lstResult.Items.Add("The sum of " & num1 & " and " & num2 & " is " & num1 + num2 & " and " & num2 & " is " & num1 + num2 & ".") & ".")
End Sub End Sub
Parameters and other variablesParameters and other variables
• Locate Locate Local Local VariableVariable definition definition
• Locate Locate Local Local VariableVariable use use
Sub CalculateDensity(ByVal state As String, Sub CalculateDensity(ByVal state As String, ByVal pop As Double, ByVal area As Double) ByVal pop As Double, ByVal area As Double)
Dim rawDensity, density As Double Dim rawDensity, density As Double
'The density (people per square mile) 'The density (people per square mile)
'will be displayed rounded 'will be displayed rounded
'to one decimal place'to one decimal place
rawDensity = pop / area rawDensity = pop / area
density = Math.Round(rawDensity, 1)density = Math.Round(rawDensity, 1)
'Round to one decimal place'Round to one decimal place
lstDensity.Items.Add("The density of " & state lstDensity.Items.Add("The density of " & state & " is " & density)& " is " & density)
lstDensity.Items.Add("people per square mile.") lstDensity.Items.Add("people per square mile.")
End SubEnd Sub
ByVal or ByRef ByVal or ByRef pages 154-157pages 154-157
• Only applicable when argument is a variableOnly applicable when argument is a variable• ByVal gives the called procedure a copy of the ByVal gives the called procedure a copy of the
argument’s valueargument’s value– Parameter and argument not connectedParameter and argument not connected– Changing parameter value Changing parameter value doesn’tdoesn’t affectaffect argument argument
valuevalue
• ByRef gives the called procedure the address of ByRef gives the called procedure the address of the argumentthe argument– Parameter and argument connectedParameter and argument connected– Changing parameter value Changing parameter value doesdoes affectaffect argument argument
valuevalue
Local and Class Variable ScopeLocal and Class Variable Scopepages 157 - 160pages 157 - 160
• Local – defined inside Sub .. End Sub pairLocal – defined inside Sub .. End Sub pair– Variable “exists” only while execution is Variable “exists” only while execution is
between its Sub .. End Subbetween its Sub .. End Sub– Variable undefined outside those boundaries.Variable undefined outside those boundaries.
• Class – defined outside any Sub .. End Class – defined outside any Sub .. End Sub pair but inside Class .. End ClassSub pair but inside Class .. End Class– Variable “exists” everywhere in programVariable “exists” everywhere in program– Value may be accessed and modified in any Value may be accessed and modified in any
procedureprocedure
Character relations
• American National Standards Institute – code for characters– Characters 0 – 9 => codes 48 – 57– Capital letters => codes 65 – 90– Small letters => codes 97 – 122
• 0 before A, A before a
Relational Operators
• Used to create conditions– Equal to =– Not equal to <>– Greater than >– Less than <– Greater than
or equal to >=– Less than
or equal to <=
Logical operators
• Used to combine conditions– And…..the combination is true only if both
conditions are true– Or…….the combination is true if either or both of
the conditions are true
– Not……produces the opposite value of the condition
-4 -3 -2 -1 0 1 2 3 4
Boolean Variables
• Can be set to true or false
• Once set can be used in place of condition
• Often used as flag
Repetition
• Group instructions that need to be repeated
• Create condition that controls number of repetitions
While
• Condition is true at start
• Continue until the condition is false
• Something in loop must happen to make condition false
Terminator
• While not terminated– “I’ll be back”
• Used to control input loop– Loop repeatedly asks for input of a certain
type, for example, positive numbers or a list of names.
– Choose terminator that would NOT occur naturally in the input.
Piggy Bank Piggy bank plus ¢
N=N+?
• Incrementor: – n=n+1
• Accumulator:– n=n+x
• Above is bad algebra but excellent computer
• A specific expected value for n could be used as terminator
For .. Next Loops
• Loop keeps count• Count ↑ or ↓• Stops when it finishes
counting• Could be done with a
while loop
• Initial value – number to count from
• Terminating value – number to count to
• Control variable – holds count, can be used in loop
Count to 5Dim n As Integer
For n = 1 To 5
lstTable.Items.Add(n & " " & n ^ 2)
Next
• n is the control variable• 1 is initial value• 5 is terminating value• n is used for output and
calculation
Start with any initial value
Dim pop As Double = 30000
Dim yr As Integer
For yr = 2006 To 2010lstTable.Items.Add(yr & “ “ & pop))
pop += 0.03 * pop
Next
Use any step
Dim n, s, As Double Dim index As Double n = CDbl(txtEnd.Text) s = CDbl(txtStep.Text)lstValues.Items.Clear() For index = 0 To n Step s
lstValues.Items.Add(index)
Next
Count down
Dim m, j As Integer, temp As String =""
m = info.Length
For j = m - 1 To 0 Step -1 temp &= info.Substring(j, 1)