Top Banner
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

Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Dec 14, 2015

Download

Documents

Kira Comber
Welcome message from author
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
Page 1: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 2: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 3: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 4: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 5: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 6: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Characters

• Character = any written symbol– A..Z, a..z, 0..9, !, @, etc.– Spaces, return and tab– Some other special use items

Page 7: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

String

• A sequence of characters

• String literal = “qwerty”

• String variables– Dim varName As String

• Dim today As String • today = "Monday"

Page 8: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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"

Page 9: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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.

Page 10: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Using Text Boxes for Input and Output

• Input– strVar = txtBox.Text

• Output – txtBox.Text = strVar

Page 11: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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)

Page 12: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Formatting Numeric Values

Function String Value

FormatNumber(12345.628, 1) 12,345.6

FormatCurrency(12345.628, 2) $12,345.63

FormatPercent(0.185, 2) 18.50%

Usage

lblWinPercent.text = FormatPercent( dblWinPercent, 2 )

txtSalary = FormatCurrency( dblSalary, 2)

Page 13: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Zone Formatting

What is it about?1. Setting up zones – setting up tab stops.2. Force output into columns3. Use formatting tags to format numbers

How is it done?1. Create zone string to define zones2. Make zone string and data parameters to

String.Format method3. Use above as parameter to .Add method of Listbox

Page 14: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Dialog Boxes

stringVar = InputBox(prompt, title)

• Causes a dialog box to pop up

• Text enter in dialog placed in stringVar

Page 15: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Dialog Box Sample

Dim stringVar As String

stringVar = InputBox("Please enter a number", "Just A Sample")

Just like World.Ask in Alice

Page 16: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

ProceduresProcedures

• As In Alice but with a different nameAs In Alice but with a different name

• Break code down in small segmentsBreak code down in small segments

• Reuse in different parts of programsReuse in different parts of programs

Page 17: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Procedure syntaxProcedure syntax

• HeaderHeader– Reserved word “Sub”Reserved word “Sub”– Procedure nameProcedure name– ParenthesesParentheses

• CodeCode

• End SubEnd Sub

Sub ProcedureName()

statement(s)

End Sub

Page 18: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Procedure PlacementProcedure Placement

• Between Class .. End ClassBetween Class .. End Class

• But not between any Sub .. End Sub pairBut not between any Sub .. End Sub pair

• Generally placed below last event Generally placed below last event procedureprocedure

• Non event procedures should be placed in Non event procedures should be placed in a groupa group

Page 19: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Parameter PassingParameter Passing

• Locate the callsLocate the calls– Arguments inside Arguments inside

parenthesesparentheses

• Locate the headerLocate the header– Parameters inside Parameters inside

parenthesesparentheses– ByVal replaces ByVal replaces

DimDim

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

Page 20: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Parameters and other variablesParameters and other variables

• Locate parameter Locate parameter definitiondefinition

• Locate parameter Locate parameter useuse

• 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

Page 21: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 22: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 23: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 24: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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 <=

Page 25: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 26: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Boolean Variables

• Can be set to true or false

• Once set can be used in place of condition

• Often used as flag

Page 27: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Repetition

• Group instructions that need to be repeated

• Create condition that controls number of repetitions

Page 28: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

While

• Condition is true at start

• Continue until the condition is false

• Something in loop must happen to make condition false

Page 29: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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.

Page 30: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 31: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 32: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 33: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 34: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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

Page 35: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

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)

Next

Page 36: Operators Same as standard math operators with 2 exceptions –* for multiplication –^ for exponentiation Order of operations strictly observed –Please Excuse.

Reading Assignments

• Read section 3.2• Read section 3.3• Read section 3.4• Read section 3.5• Read section 4.1 • Read section 5.1 • Read section 5-2• Read section 6-1• Read section 6-3