Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © Don Kussee 1410-Ch4 #521 CNS 1120 Chapter 4 Performing Calculations & Manipulating Data 1120-Ch4.PPT.

Similar presentations


Presentation on theme: "Copyright © Don Kussee 1410-Ch4 #521 CNS 1120 Chapter 4 Performing Calculations & Manipulating Data 1120-Ch4.PPT."— Presentation transcript:

1 Copyright © Don Kussee 1410-Ch4 #521 CNS 1120 Chapter 4 Performing Calculations & Manipulating Data 1120-Ch4.PPT

2 Copyright © Don Kussee 1410-Ch4 #522 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

3 Copyright © Don Kussee 1410-Ch4 #523

4 4

5 5 Disk 1

6 Copyright © Don Kussee 1410-Ch4 #526 Disk 2

7 Copyright © Don Kussee 1410-Ch4 #527 Expressions 145 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

8 Copyright © Don Kussee 1410-Ch4 #528 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

9 Copyright © Don Kussee 1410-Ch4 #529 Relationships Statement Expressions Arithmetic - String - Logical FunctionsOperators Values 1 or more 0 or more

10 Copyright © Don Kussee 1410-Ch4 #5210 Statements Dim x As Integer ‘Not an expression Dim s As String ‘Not an expression x = (((8 + 9)*2)+2)/12 ‘Math expression x = Sqr(x) ‘Function expression s = “Quick ” & “Fox” ‘String expression Evaluation of right side is done first Then assignment to left side occurs 3 = x

11 Copyright © Don Kussee 1410-Ch4 #5211 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

12 Copyright © Don Kussee 1410-Ch4 #5212 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()

13 Copyright © Don Kussee 1410-Ch4 #5213 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)

14 Copyright © Don Kussee 1410-Ch4 #5214 Computer Terms Statement Evaluation Binary Operator Unary Operator Modulus Integer division Scientific notation Function Argument

15 Copyright © Don Kussee 1410-Ch4 #5215 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

16 Copyright © Don Kussee 1410-Ch4 #5216 Integer Divisions & Modulus 6.33 6 38 / 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

17 Copyright © Don Kussee 1410-Ch4 #5217 Unary Operators Positive + x = + 7 Negative - x = -13 Unary & Binary operators –x = 7 + -2 –x = - (-7 + 13) + (-3 - -4)

18 Copyright © Don Kussee 1410-Ch4 #5218 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

19 Copyright © Don Kussee 1410-Ch4 #5219 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

20 Copyright © Don Kussee 1410-Ch4 #5220 Operator Precedence Rule 1Parenthesis ( ) 2Exponential ^ 3Unary operators + - 4Mult. & Div. * / 5Integer Division \ 6Modulus MOD 7Add & Sub + - 8Concatenation & 9 Comparison = A Comparison B Comparison < C Comparison > D Comparison <= E Comparison >= Z Assignment =

21 Copyright © Don Kussee 1410-Ch4 #5221 Operator Precedence Rule FUnicode String Like GObject compare Is HLogical Not ILogical And JLogical Or KLogical Xor LLogical Eqv MLogical Imp Z Assignment =

22 Copyright © Don Kussee 1410-Ch4 #5222 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

23 Copyright © Don Kussee 1410-Ch4 #5223 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))

24 Copyright © Don Kussee 1410-Ch4 #5224 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( ))+1 0 to 299 v = Fix(300 * Rnd( )) 17 to 92 v = Fix(75 * Rnd( ))+17 Randomize ‘ starts Rnd ( ) in different place use Randomize only once per program

25 Copyright © Don Kussee 1410-Ch4 #5225 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

26 Copyright © Don Kussee 1410-Ch4 #5226 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

27 Copyright © Don Kussee 1410-Ch4 #5227 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

28 Copyright © Don Kussee 1410-Ch4 #5228 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

29 Copyright © Don Kussee 1410-Ch4 #5229 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)

30 Copyright © Don Kussee 1410-Ch4 #5230 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

31 Copyright © Don Kussee 1410-Ch4 #5231 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

32 Copyright © Don Kussee 1410-Ch4 #5232 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

33 Copyright © Don Kussee 1410-Ch4 #5233 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

34 Copyright © Don Kussee 1410-Ch4 #5234 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

35 Copyright © Don Kussee 1410-Ch4 #5235 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

36 Copyright © Don Kussee 1410-Ch4 #5236 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

37 Copyright © Don Kussee 1410-Ch4 #5237 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

38 Copyright © Don Kussee 1410-Ch4 #5238 Unicode New code, allows 65,000 different shapes First 256 values = ANSI code Japanese, Cyrillic, Arabic characters can be added Java uses this code

39 Copyright © Don Kussee 1410-Ch4 #5239 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

40 Copyright © Don Kussee 1410-Ch4 #5240 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

41 Copyright © Don Kussee 1410-Ch4 #5241 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)

42 Copyright © Don Kussee 1410-Ch4 #5242 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 6 ? = -9 > 7 ? = “day1” > “day2” ? = -7 > -9 ? = 3 + 7 < 4 + 6

43 Copyright © Don Kussee 1410-Ch4 #5243 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

44 Copyright © Don Kussee 1410-Ch4 #5244 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 ????

45 Copyright © Don Kussee 1410-Ch4 #5245 And T F T T F F F F Test 1 And truth table Test2

46 Copyright © Don Kussee 1410-Ch4 #5246 Or truth table Or T F T T T F T F Test 1 Test 2

47 Copyright © Don Kussee 1410-Ch4 #5247 Xor truth table Xor T F T F T F T F Test 1 Test 2

48 Copyright © Don Kussee 1410-Ch4 #5248 Not unary truth table Not T F F T Test 1

49 Copyright © Don Kussee 1410-Ch4 #5249 Practice (7 >= 5) Or (P > R) ‘Parentheses not required (Not (6<>8)) And (Not(-7 >-4)) (9 MOD 6 4 ^ 1 / 2 ) (2 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)

50 Copyright © Don Kussee 1410-Ch4 #5250 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

51 Copyright © Don Kussee 1410-Ch4 #5251 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.

52 Copyright © Don Kussee 1410-Ch4 #5252 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


Download ppt "Copyright © Don Kussee 1410-Ch4 #521 CNS 1120 Chapter 4 Performing Calculations & Manipulating Data 1120-Ch4.PPT."

Similar presentations


Ads by Google