Copyright © Don Kussee 14 10-Ch4 #52 1 CNS 1120 Chapter 4 Performing Calculations & Manipulating Data 1120-Ch4.PPT
Copyright © Don Kussee 1410-Ch4 #52
1
CNS 1120Chapter 4
Performing Calculations & Manipulating Data
1120-Ch4.PPT
Copyright © Don Kussee 1410-Ch4 #52
2
Distribution to Non-Visual Basic computers
• You must include DLL's, OCX’s, ActiveX’s• In Professional Version
– Go to VB6.0 Directory– Start “Package & deployment Setup Wizard”
• Could be put as a shortcut on the desktop
• Missile program took 2 disks to store 13 files
Copyright © Don Kussee 1410-Ch4 #52
3
Copyright © Don Kussee 1410-Ch4 #52
4
Copyright © Don Kussee 1410-Ch4 #52
5
Disk 1
Copyright © Don Kussee 1410-Ch4 #52
6
Disk 2
Copyright © Don Kussee 1410-Ch4 #52
7
Expressions145
• Arithmetic expression result in number– Arithmetic calculations
• String expression result in a string– Manipulate string data4
• Logical expression result in true or false– Choose an action (path) from several choices– Often includes arithmetic & string expressions– False is always 0, true is any non 0 -37 .0006
Copyright © Don Kussee 1410-Ch4 #52
8
Programming Concept words
• Expression - series of numeric/logical/string/operator symbols the computer understand and can process
• Statement - tells the computer to do one or more actions, often includes expressions
• Operator - a symbol that the computer understands means to do some one thing
• Function - a more complex operation
Copyright © Don Kussee 1410-Ch4 #52
9
Relationships
Statement
Expressions
Arithmetic - String - Logical
FunctionsOperators Values
1 or more
0 or more 0 or more 0 or more
Copyright © Don Kussee 1410-Ch4 #52
10
Statements
Dim x As Integer ‘Not an expressionDim s As String ‘Not an expressionx = (((8 + 9)*2)+2)/12 ‘Math expressionx = Sqr(x) ‘Function expressions = “Quick ” & “Fox” ‘String expression• Evaluation of right side is done first• Then assignment to left side occurs 3 = x
Copyright © Don Kussee 1410-Ch4 #52
11
Functions have a return value
• Dim str As String• str = InputBox( “First Name” )• Physically - the whole InputBox term is
replaced by the value that is returned so it becomes
• str = “Don”• then the assignment is made
Copyright © Don Kussee 1410-Ch4 #52
12
Conversion String to Number Functions Val ( )
• Changes from string to a number data type• Val(String) changes a string a number.
Combines over blanks, tabs, LF up to the First non-number character (letter$, -)
• String with no number first gets a value = 0• Number outside acceptable value get Err 6• See also CInt(), CLng(), CSng(), CDbl()
Copyright © Don Kussee 1410-Ch4 #52
13
Conversions Number to String Str$( )
• Changes any numerical data type to a String value (for use using label or textbox). Note this will happen automatically if you don’t force the change. I require you to force the change you want. Most languages do!
• String = Str$(number)• Label1.Caption = Str$(number)
Copyright © Don Kussee 1410-Ch4 #52
14
Computer Terms
• Statement• Evaluation• Binary Operator• Unary Operator• Modulus• Integer division• Scientific notation
• Function• Argument
Copyright © Don Kussee 1410-Ch4 #52
15
Binary Arithmetic Operators
• Addition + 9 = 7 + 2• Subtraction - 5 = 7 - 2• Multiplication * 14 = 7 * 2• Division / 3.5 = 7 / 2• Exponentiation ^ 49 = 7 ^ 2• Integer Division \ 3 = 7 \ 2 • Modulo MOD 1 = 7 MOD 2
Copyright © Don Kussee 1410-Ch4 #52
16
Integer Divisions & Modulus
6.33 638 / 6 6/ 38.00 38 \ 6 6/ 38 -36 -36 20 remainder 2 -18 2 = 38 MOD 6 20 ? = 29 MOD 5 - 18 ? = 16 MOD 12 = 84 MOD 20
Copyright © Don Kussee 1410-Ch4 #52
17
Unary Operators
• Positive + x = + 7• Negative - x = -13• Unary & Binary operators
– x = 7 + -2– x = - (-7 + 13) + (-3 - -4)
Copyright © Don Kussee 1410-Ch4 #52
18
QUIZ
• ? = 22 / 6• ? = 22 \ 6• ? = 13 MOD 4• ? = 64 / 2• ? = 60 \ 21• ? = 56 MOD 2• ? = 12 / 13
• ? = 99 \ 100• ? = 99 / 100• ? = 23 MOD 3• ? = 642 MOD 2• ? = 512 MOD 16• ? = 511 MOD 16• ? = 513 MOD 16
Copyright © Don Kussee 1410-Ch4 #52
19
Scientific or Exponential Notation
• 3.08E+12 = 3080000000000.00• 3.08E-6 = 0.00000308• -4.9E+1 = -49.• ? = 6.8E-3 • ? = 7.99998E+3• ? = 9.999999E-13• ? = 1.8E+308
Copyright © Don Kussee 1410-Ch4 #52
20
Operator Precedence Rule
1 Parenthesis ( )2 Exponential ^3 Unary operators + -4 Mult. & Div. * /5 Integer Division \6 Modulus MOD7 Add & Sub + -8 Concatenation &
9 Comparison = A Comparison < > B Comparison <C Comparison >D Comparison <=E Comparison >=
Z Assignment =
Copyright © Don Kussee 1410-Ch4 #52
21
Operator Precedence Rule
F Unicode String LikeG Object compare IsH Logical NotI Logical AndJ Logical OrK Logical XorL Logical EqvMLogical Imp
Z Assignment =
Copyright © Don Kussee 1410-Ch4 #52
22
Comment’s
• Used to document code– Block comments tell what several lines will do– Comment to tell that line itself Note: Page 161
• REM as the first word in a line of code, causes the computer to ignore the whole line of code
• REM the abbreviated symbol ‘ can be used anywhere with in a line - rest of line ignored
Copyright © Don Kussee 1410-Ch4 #52
23
Some Arithmetic Functions
• Abs ( value ) returns the absolute value• Sgn ( value ) returns the sign of the value• Fix (value) truncates any fractional part• Exp ( value ) converts to Log base e I.e.
7.9048053225245E+206 = Exp( 476.4)• Log ( value ) converts base e number to a
number example 748 = Log(Exp(748))
Copyright © Don Kussee 1410-Ch4 #52
24
Random number Rnd ( )
• Random numbers are often required • Rnd() returns a number v >= 0, v < 1• For a specific number inclusive
1 to 500 v = Fix(500 * Rnd( ))+10 to 299 v = Fix(300 * Rnd( ))17 to 92 v = Fix(75 * Rnd( ))+17
• Randomize ‘ starts Rnd ( ) in different placeuse Randomize only once per program
Copyright © Don Kussee 1410-Ch4 #52
25
Overflow errors - Err 6
• Program stops - error is given• For Integer or Long variables• The memory set aside for a variable will not
hold the number assigned I.e Dim c As Integer c = 33000
Copyright © Don Kussee 1410-Ch4 #52
26
Round off errors, No stop no error but answer can be wrong
• Data types Single or Double does not round• The computer can not store a base 10 value
correctly as a base 2 format number• Data type Currency does round
– x = 4,407,351,698,474.15– Single x = 4,407,351,000,000.00 x 10 12
– Double x = 4,407,351,698,474.10 x 10 12
– Currency x = 4,407,351,698,474.1500
Copyright © Don Kussee 1410-Ch4 #52
27
Conversion from Single & Double to Integer & Long
• Moving from one data type to a other type• Dim C As Integer : Dim D As Single• No error occurs because it is the wrong type,
the number is just adjusted for C = 2.9 the fraction is truncated C = 2 C = 6.999999 C = 6 D = 89 the figure is adjusted to D = 89.0 E = -37 if E of type currency E= -37.0000
Copyright © Don Kussee 1410-Ch4 #52
28
String Operators
• & (or + ) Concatenation operator• Dim a As String• Dim B As String• a = “Tom”• B = “Jones”• lblX.caption = a & B ‘TomJones• lblX.caption= a & “ “ & “Jones” ‘ Tom Jones
Copyright © Don Kussee 1410-Ch4 #52
29
String Comparisons
• ( “Tom Brown” = “Tom brown” )• Strings are compared letter by letter until
– A difference between the ANSI value of the two characters in the same position is found F
– One string ends and the other continues (diff) F– Both strings end with no differences - True
• 32 = Abs(UCase -LCase) • 1, 0, -1 returned by StrComp(Str1, Str2,rule)
Copyright © Don Kussee 1410-Ch4 #52
30
String Comparisons
• Same operators are used• x = 486 = “486Mhertz” ‘ true ? = operator• Auto-conversion of string to get Integer
data type, so problem is now 486 = 486?– Spaces, CrLf, tabs skipped over ‘ 5 9 6=596
• X = 486 = “CPU486” ‘ type mismatch
Copyright © Don Kussee 1410-Ch4 #52
31
String Comparison
• Figure 4-6 on page 175• If ( “Jim B” = “Jim D”) Then• ? If ( “Jim b” > “Jim D”) Then• ? If ( “Jim B” < “Jim D”) Then • ? If ( “Jim Brown” < “Jim D”) Then• ? If ( “7” > “4” ) Then• ? If (“122 main” > “128 main”) Then
Copyright © Don Kussee 1410-Ch4 #52
32
String functions
• Len(Str) ‘Returns number of characters • Val (Str) ‘Ret. Numbers (first) in a string• CDbl(str) ‘ same Val but for international• If the function name ends in $, the the
function returns a string • Str$ (Num ) ‘Number to string characters
Copyright © Don Kussee 1410-Ch4 #52
33
Format ( ) function
• Used to format output for ease of reading• Can format strings or date & time• Will insert $ + 001,987.9700 Format$( )
‘adds $, commas, sign, number of places for numbers or zeros
• Several standard functions available - or can be custom designed
Copyright © Don Kussee 1410-Ch4 #52
34
String functions continued
• Function returns a string then function$• Left$( ) ‘returns left most characters• Right$( ) ‘returns right most characters• Mid$( ) ‘returns middle characters• LCase$( ) ‘Converts upper letters to lower• UCase$( ) ‘Converts lower letters to upper
Copyright © Don Kussee 1410-Ch4 #52
35
String functions continued
• LTrim$( ) ‘Removes leading spaces• RTrim$( ) ‘Removes trailing spaces• Trim$( ) ‘Removes leading & trailing• String$( ) ‘Repeats a character• Asc() ‘Convert character to ANSI value• Chr$( ) ‘Convert ANSI value to character• Instr$( ) ‘Find sub-string in a string
Copyright © Don Kussee 1410-Ch4 #52
36
ASCII - ANSI table - page 175
• Shapes for a code number ? =“a”, ? =“A”, ? =“B”, ? = “2” , ? = “3”, ? = “4”
• Different shape depending on language• Non-printable letters
– Space . tab backspace bell lf ff cr• Carriage return & Line feed - vbCrLF • Vertical tab
Copyright © Don Kussee 1410-Ch4 #52
37
ANSI code comments
• Difference between A and a is 32 65 - 97 = 32• Bell = 7 backspace = 8• Space = 32 a = 97• Period = 46 z = 122• character 1 = 49 A = 65• character 9 = 57 Z = 90
Copyright © Don Kussee 1410-Ch4 #52
38
Unicode
• New code, allows 65,000 different shapes• First 256 values = ANSI code• Japanese, Cyrillic, Arabic characters can be
added• Java uses this code
Copyright © Don Kussee 1410-Ch4 #52
39
TextBox txt
• 52 Properties– Text– Appearance & location– BorderStyle– MaxLength (d=32,000)– MultiLine– ScrollBars– Password– SelLength, SelText
• 23 Events– Change– Click– DragOver– KeyPress
• 11 Methods– Move– Refresh– ZOrder
Copyright © Don Kussee 1410-Ch4 #52
40
TextBox properties
• The TextBox is the preferred input object• Text is the input (& output) property• No Caption Property• Enabled - grayed out, does not work• Visible - can you see it• Locked - can the user change the text• Usual event - Change, Click
Copyright © Don Kussee 1410-Ch4 #52
41
Ok & Cancel Buttons
• Command buttons with Captions• Default = “Hooks” object to the return key• Cancel = “Hooks” object to the Esc key• Enable = False (grayed out, non functional)• Visible = False (not visible on form)
Copyright © Don Kussee 1410-Ch4 #52
42
Logical Expressions
• Results of logical expressions result in a False ( a 0 Value) or True (any non-zero) and is stored as a -1 (Book wrong page 189)
• Binary operators <, <=, >, >=, =, < >• ? = 7 < 9 ? = 6<= 7 ? = 6 < >6• ? = -9 > 7 ? = “day1” > “day2”• ? = -7 > -9 ? = 3 + 7 < 4 + 6
Copyright © Don Kussee 1410-Ch4 #52
43
Assignment Operator Vs Equality Test Operator
• = is The assignment operator, and is a Unary operator (first operator after variable)
• = is an Equality Test operator, but is a Binary operator (not first operator after)– Pascal uses := for assignment, = for test– C, C++, Java use = = for tests, = for assignment– Operator overloading occurs frequently 6 --5– How do we tell between to, two, and too
Copyright © Don Kussee 1410-Ch4 #52
44
Boolean Type Operators
• Not Unary operator• And Binary operator• Or Binary operator• Xor Binary operator• = (“a” > “b”) And (5 = r)• = (7 < 9) Or (6 = 5) Or (Not (7 = 13))• Boolean operators precedence is ????
Copyright © Don Kussee 1410-Ch4 #52
45
And T F
T T F
F F F
Test 1Test 1
And truth table
Test2Test2
Copyright © Don Kussee 1410-Ch4 #52
46
Or truth table
Or T F
T T T
F T F
Test 1Test 1
Test 2Test 2
Copyright © Don Kussee 1410-Ch4 #52
47
Xor truth table
Xor T F
T F T
F T F
Test 1Test 1
Test 2Test 2
Copyright © Don Kussee 1410-Ch4 #52
48
Not unary truth table
Not T F
F T
Test 1Test 1
Copyright © Don Kussee 1410-Ch4 #52
49
Practice
(7 >= 5) Or (P > R) ‘Parentheses not required(Not (6<>8)) And (Not(-7 >-4))(9 MOD 6 < 2 ^ 3) Or (8 \ 3 > 4 ^1/2)
(2 < 3) Or (7 < 8) Or (6 > 3) Or ( -4 < -1)(3>2) And (4<3) Or (-9 <-5)(6>3) And (3>1) And (7>6)(Not(6>3)) And (-7<-3) Xor (6=6)
Copyright © Don Kussee 1410-Ch4 #52
50
Additional functions
• BValue = IsNumeric (variable name) • IsDate( ) IsMissing( ) IsNull( )• IsEmpty( ) IsArray( ) IsError( )
• Ternary Operator - tests first term• IIf (3 < 2 , 2 +3 , 2*3)
returns 2nd term if true, 3rd term if false
Copyright © Don Kussee 1410-Ch4 #52
51
windows Access keys
• A combination of Alt and some other key• Denoted in menus with underlined letter• Hooked up in VB automatically
– Placing a “&” preceding any letter– Unique to a Form– Allowed in Captions
• cmdExit.caption = “E&xit” is displayed as Exit and click event occurs when Alt & X key are pressed.
Copyright © Don Kussee 1410-Ch4 #52
52
Chapter 4 Keywords
• ANSI/ASCII /Unicode• Arithmetic expression• String expression• Logical Expression• Binary Operator• Unary Operator• Ternary Operator• Comparison Operator
• InputBox ( ) function
• Text box control• CarrageReturn/LineFeed• Case of Characters• Concatenation• Function - return value