Download presentation
Presentation is loading. Please wait.
Published byKira Comber Modified over 9 years ago
1
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
2
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
3
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
4
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
5
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
6
Characters Character = any written symbol –A..Z, a..z, 0..9, !, @, etc. –Spaces, return and tab –Some other special use items
7
String A sequence of characters String literal = “qwerty” String variables –Dim varName As String Dim today As String today = "Monday"
8
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"
9
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.
10
Using Text Boxes for Input and Output Input –strVar = txtBox.Text Output –txtBox.Text = strVar
11
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)
12
Formatting Numeric Values FunctionString 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)
13
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
14
Dialog Boxes stringVar = InputBox(prompt, title) Causes a dialog box to pop up Text enter in dialog placed in stringVar
15
Dialog Box Sample Dim stringVar As String stringVar = InputBox("Please enter a number", "Just A Sample") Just like World.Ask in Alice
16
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
17
Procedure syntax HeaderHeader –Reserved word “Sub” –Procedure name –Parentheses CodeCode End SubEnd Sub Sub ProcedureName() statement(s) End Sub
18
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
19
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
20
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
21
ByVal or ByRef pages 154-157 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
22
Local and Class Variable Scope pages 157 - 160 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
23
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
24
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<=
25
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
26
Boolean Variables Can be set to true or false Once set can be used in place of condition Often used as flag
27
Repetition Group instructions that need to be repeated Create condition that controls number of repetitions
28
While Condition is true at start Continue until the condition is false Something in loop must happen to make condition false
29
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.
30
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
31
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
32
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
33
Start with any initial value Dim pop As Double = 30000 Dim yr As Integer For yr = 2006 To 2010 lstTable.Items.Add(yr & “ “ & pop)) pop += 0.03 * pop Next
34
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
35
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
36
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.