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” = ( ) / 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: 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)
Formatting Numeric Values FunctionString Value FormatNumber( , 1)12,345.6 FormatCurrency( , 2)$12, FormatPercent(0.185, 2)18.50% Usage lblWinPercent.text = FormatPercent( dblWinPercent, 2 ) txtSalary = FormatCurrency( dblSalary, 2)
Zone Formatting What is it about? 1.Setting up zones – setting up tab stops. 2.Force output into columns 3.Use formatting tags to format numbers How is it done? 1.Create zone string to define zones 2.Make zone string and data parameters to String.Format method 3.Use above as parameter to.Add method of Listbox
Dialog Boxes stringVar = InputBox(prompt, title) Causes a dialog box to pop up Text enter in dialog placed in stringVar
Dialog Box Sample Dim stringVar As String stringVar = InputBox("Please enter a number", "Just A Sample") Just like World.Ask in Alice
Procedures 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
Procedure syntax HeaderHeader –Reserved word “Sub” –Procedure name –Parentheses CodeCode End SubEnd Sub Sub ProcedureName() statement(s) End Sub
Procedure 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 procedureGenerally placed below last event procedure Non event procedures should be placed in a groupNon event procedures should be placed in a group
Parameter Passing Locate the callsLocate the calls –Arguments inside parentheses Locate the headerLocate the header –Parameters inside parentheses –ByVal replaces Dim Private Sub btnAdd_Click(...) Handles btnAdd.Click 'Display the sum of two numbers lstResult.Items.Clear() ExplainPurpose()lstResult.Items.Add("") DisplaySum(2, 3) End Sub Sub DisplaySum(ByVal num1 As Double, ByVal num2 As Double) 'Display numbers and their sum lstResult.Items.Add("The sum of " & num1 & " and " & num2 & " is " & num1 + num2 & ".") End Sub
Parameters and other variables Locate parameter definitionLocate parameter definition Locate parameter useLocate parameter use Locate Local Variable definitionLocate Local Variable definition Locate Local Variable useLocate Local Variable use Sub CalculateDensity(ByVal state As String, ByVal pop As Double, ByVal area As Double) Dim rawDensity, density As Double 'The density (people per square mile) 'will be displayed rounded 'to one decimal place rawDensity = pop / area density = Math.Round(rawDensity, 1) 'Round to one decimal place lstDensity.Items.Add("The density of " & state & " is " & density) lstDensity.Items.Add("people per square mile.") End Sub
ByVal or ByRef pages Only applicable when argument is a variableOnly applicable when argument is a variable ByVal gives the called procedure a copy of the argument’s valueByVal gives the called procedure a copy of the argument’s value –Parameter and argument not connected –Changing parameter value doesn’t affect argument value ByRef gives the called procedure the address of the argumentByRef gives the called procedure the address of the argument –Parameter and argument connected –Changing parameter value does affect argument value
Local and Class Variable Scope pages Local – defined inside Sub.. End Sub pairLocal – defined inside Sub.. End Sub pair –Variable “exists” only while execution is between its Sub.. End Sub –Variable undefined outside those boundaries. Class – defined outside any Sub.. End Sub pair but inside Class.. End ClassClass – defined outside any Sub.. End Sub pair but inside Class.. End Class –Variable “exists” everywhere in program –Value may be accessed and modified in any procedure
Character relations American National Standards Institute – code for characters –Characters 0 – 9 => codes 48 – 57 –Capital letters => codes 65 – 90 –Small letters => codes 97 – 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
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 5 Dim 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 = Dim yr As Integer For yr = 2006 To 2010 lstTable.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) Next
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