Download presentation
Presentation is loading. Please wait.
Published byErick Dawson Modified over 9 years ago
1
CNS 1120 Exam 1 Review
2
Programming Language Elements Syntax = the rules to follow Syntax = the rules to follow Semantics = the meaning of symbols Semantics = the meaning of symbols Keywords = 340, we will use about 50 Keywords = 340, we will use about 50 Operators = old friends, and a few new ones Operators = old friends, and a few new ones Procedures, functions and sub-routines Procedures, functions and sub-routines Data types Data types Data structures Data structures
3
Language Translation Computers can only run Machine Language Code All other code must be translated into machine language Computers can only run Machine Language Code All other code must be translated into machine language Two processes are possible Two processes are possible –Interpreted –Compiled
4
No Magic Numbers Only 0, 1, -1, “” allowed as literal constants Only 0, 1, -1, “” allowed as literal constants Named constant easier to understand Named constant easier to understand Constant reduces a the chance for typos Constant reduces a the chance for typos Makes a change throughout the program easy and correctTAX_RATE goes from 0.081 to 0.086 Makes a change throughout the program easy and correctTAX_RATE goes from 0.081 to 0.086 Const SHIPPLING_PER_TON =.081 Const SHIPPLING_PER_TON =.081
5
Data types defined in VB String String –Variable length –Fixed length Numbers Numbers –Integer, Long, Single, Double, Currency Others - Variant Others - Variant User defined User defined
6
Number data types Whole numbers 37 -842 Whole numbers 37 -842 –Integer - small –Long - big Fractional numbers 37.7223 -842.01 Fractional numbers 37.7223 -842.01 –Single - Big –Double - Bigger –Currency Special Biggest –Date Special
7
Data type String Variable length - 2 Billion max Variable length - 2 Billion max –Dim FirstName as String –FirstName = “Don” FirstName= “Victoria” –Size changes automatically –Size 10 bytes + 1 byte per letter Fixed length - 65,400 max Fixed length - 65,400 max –Dim ZipCode as String * 5 –ZipCode = “48058”
8
Data Type Integer Whole number 89, -37, -32768, 32840 Whole number 89, -37, -32768, 32840 Memory set aside 2 Bytes Memory set aside 2 Bytes Maximum Size +-32K (-32767 to +32768) Maximum Size +-32K (-32767 to +32768) Dim Age as Integer Dim Age as Integer Age = 100 Age = 100 Fractional values are dropped Fractional values are dropped
9
Data Type Long (Integer) Whole number 89, -37, -32768, 32840 Whole number 89, -37, -32768, 32840 Memory Set aside 4Bytes Memory Set aside 4Bytes Maximum Size very big Maximum Size very big –-2,147,843,648 to 2,147,843,648 Dim BankBallance as Long ‘4 bytes Dim BankBallance as Long ‘4 bytes BankBallance = 1000895 ‘ No comma’s BankBallance = 1000895 ‘ No comma’s
10
Data Type Single Fractional number 89.6 -37.0.00006 Fractional number 89.6 -37.0.00006 Maximum - Big number (-3.4E -45 to 3.4E 38 ) Maximum - Big number (-3.4E -45 to 3.4E 38 ) Memory Set aside 4 Bytes Memory Set aside 4 Bytes 7 Significant Digits 7 Significant Digits –1.7894288.00017894288 17894288 Dim Wage as Single Dim Wage as Single Wage = 5.90 Wage = 5.90
11
Data Type Double Fractional number 89.6 -37.0.00006 Fractional number 89.6 -37.0.00006 –1.7894288.00017894288 17894288 Maximum - Very big (-1.9E 324 to 4.9E 308 ) Maximum - Very big (-1.9E 324 to 4.9E 308 ) Memory Set aside 8 Bytes Memory Set aside 8 Bytes 15 Significant Digits 15 Significant Digits –-.0009787658654388955 9875.86445679987 Math takes six time Math takes six time
12
Data Type Currency Fractional number with 4 decimal places Fractional number with 4 decimal places Maximum Very big (15.4 digits) Maximum Very big (15.4 digits) Memory Set aside 8 Bytes Memory Set aside 8 Bytes Dim HouseLoan as Currency Dim HouseLoan as Currency HouseLoan = 111211121112765.9999 HouseLoan = 111211121112765.9999 Math takes six times Math takes six times
13
Variable Scope Depends on where and how declared Depends on where and how declared –Scope How declared –Local - Dim, Const, Static, ReDim –Module - Dim, Private –Global - Global, Public Right mouse click will show properties of variable Right mouse click will show properties of variable
14
Variable Scope & Life Procedure Declaration Procedure Declaration –Scope - local to procedure –Life - length of procedure Form Declaration Form Declaration –Scope - global to all procedures in form –Life - of application Global Declaration Global Declaration –Scope - global to application
15
Static Vs Dim Initializes Variable the first time only, then the value is maintained for the remainder of the life of the program. Reuse of the variable (by entering the procedure), does not reinitialize the variable. Scope is unchanged. Initializes Variable the first time only, then the value is maintained for the remainder of the life of the program. Reuse of the variable (by entering the procedure), does not reinitialize the variable. Scope is unchanged. Static Joe As Integer Dim Joe As Integer
16
Variable (or Const) Scope What Variable can be accessed (read or changed) by a different domain What Variable can be accessed (read or changed) by a different domain Local - Dim within a Control_Event( ) Local - Dim within a Control_Event( ) –can only be accessed it’s domain Module -Dim in Form General Declarations Module -Dim in Form General Declarations –accessed by all the form’s controls domain- bad Global -Global in Form Declaration Global -Global in Form Declaration –anyone can access - generally bad
17
Variable Scope Local variables can not have the same name in the same procedure Local variables can not have the same name in the same procedure Local variables can have the same name in different procedures Local variables can have the same name in different procedures Local variables can have same name as Modules/Globals Local variables can have same name as Modules/Globals Computer looks local, then module, global Computer looks local, then module, global All Forms are Global All Forms are Global
18
Variable’s Life Local - while code runs Local - while code runs –Except Static variables Module - while the form is in memory Module - while the form is in memory Global - while the Program runs Global - while the Program runs Allows re-use of memory Allows re-use of memory
19
Public Vs Private code Public changes all variables to Global Public changes all variables to Global Allows the code to be used by others Allows the code to be used by others Violates basic OOP concepts - very unusual Violates basic OOP concepts - very unusual
20
String Concatenation & Adds a string to the end of another string Adds a string to the end of another string String1 = “The big bad” String1 = “The big bad” String2 = “wolf.” String2 = “wolf.” String3 = String1 & String2 String3 = String1 & String2 String3 now contains The big badwolf. String3 now contains The big badwolf. String4 = String3 String4 = String3
21
Assignment operator = Target = Source ‘ required direction Target = Source ‘ required direction –3 = A if A = 7 and B = 2 then after A = B what is contained in both A and B ??? Target must be a variable Target must be a variable Right side of equation is completely evaluated, then transferred over = Right side of equation is completely evaluated, then transferred over = Auto-conversion will occur unless forced by programmer – But only in this language Auto-conversion will occur unless forced by programmer – But only in this language
22
Functions have a return value Dim str As String Dim str As String str = InputBox( “First Name” ) str = InputBox( “First Name” ) Physically - the whole InputBox term is replaced by the value that is returned so it becomes Physically - the whole InputBox term is replaced by the value that is returned so it becomes str = “Don” str = “Don” then the assignment is made then the assignment is made
23
Conversion String to Number Functions Val ( ) Changes from string to a number data type 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$, -) 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 String with no number first gets a value = 0 Number outside acceptable value get Err 6 Number outside acceptable value get Err 6 See also CInt(), CLng(), CSng(), CDbl() See also CInt(), CLng(), CSng(), CDbl()
24
Binary Arithmetic Operators Addition + 9 = 7 + 2 Addition + 9 = 7 + 2 Subtraction - 5 = 7 - 2 Subtraction - 5 = 7 - 2 Multiplication * 14 = 7 * 2 Multiplication * 14 = 7 * 2 Division / 3.5 = 7 / 2 Division / 3.5 = 7 / 2 Exponentiation ^ 49 = 7 ^ 2 Exponentiation ^ 49 = 7 ^ 2 Integer Division \ 3 = 7 \ 2 Integer Division \ 3 = 7 \ 2 Modulo MOD 1 = 7 MOD 2 Modulo MOD 1 = 7 MOD 2
25
Integer Divisions & Modulus 6.33 6 6.33 6 38 / 6 6/ 38.00 38 \ 6 6/ 38 -36 -36 -36 -36 20 remainder 2 20 remainder 2 -18 2 = 38 MOD 6 -18 2 = 38 MOD 6 20 ? = 29 MOD 5 20 ? = 29 MOD 5 - 18 ? = 16 MOD 12 = 84 MOD 20 - 18 ? = 16 MOD 12 = 84 MOD 20
26
Unary Operators Positive + x = + 7 Positive + x = + 7 Negative - x = -13 Negative - x = -13 Unary & Binary operators Unary & Binary operators –x = 7 + -2 –x = - (-7 + 13) + (-3 - -4)
27
Scientific or Exponential Notation 3.08E+12 = 3080000000000.00 3.08E+12 = 3080000000000.00 3.08E-6 = 0.00000308 3.08E-6 = 0.00000308 -4.9E+1 = -49. -4.9E+1 = -49. ? = 6.8E-3 ? = 6.8E-3 ? = 7.99998E+3 ? = 7.99998E+3 ? = 9.999999E-13 ? = 9.999999E-13 ? = 1.8E+308 ? = 1.8E+308
28
Operator Precedence Rule 1 Parenthesis ( ) 2 Exponential ^ 3 Unary operators + - 4 Mult. & Div. * / 5 Integer Division \ 6 Modulus MOD 7 Add & Sub + - 8 Concatenation & 9 Comparison = A Comparison A Comparison B Comparison < C Comparison > D Comparison <= E Comparison >= Z Assignment =
29
Operator Precedence Rule F Unicode String Like G Object compare Is H Logical Not I Logical And J Logical Or K Logical Xor L Logical Eqv M Logical Imp Z Assignment =
30
Comment’s Used to document code 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 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 REM the abbreviated symbol ‘ can be used anywhere with in a line - rest of line ignored
31
String Operators & (or + ) Concatenation operator & (or + ) Concatenation operator Dim a As String Dim a As String Dim B As String Dim B As String a = “Tom” a = “Tom” B = “Jones” B = “Jones” lblX.caption = a & B ‘TomJones lblX.caption = a & B ‘TomJones lblX.caption= a & “ “ & “Jones” ‘ Tom Jones lblX.caption= a & “ “ & “Jones” ‘ Tom Jones
32
String Comparisons ( “Tom Brown” = “Tom brown” ) ( “Tom Brown” = “Tom brown” ) Strings are compared letter by letter until 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) 32 = Abs(UCase -LCase) 1, 0, -1 returned by StrComp(Str1, Str2,rule) 1, 0, -1 returned by StrComp(Str1, Str2,rule)
33
String functions Len(Str) ‘Returns number of characters Len(Str) ‘Returns number of characters Val (Str) ‘Ret. Numbers (first) in a string Val (Str) ‘Ret. Numbers (first) in a string CDbl(str) ‘ same Val but for international CDbl(str) ‘ same Val but for international If the function name ends in $, the the function returns a string If the function name ends in $, the the function returns a string Str$ (Num ) ‘Number to string characters Str$ (Num ) ‘Number to string characters
34
And T F T T F T T F F F F F F F Test 1 And truth table Test2
35
Or truth table Or T F Or T F T T T T T T F T F F T F Test 1 Test 2
36
Xor truth table Xor T F T F T T F T F T F F T F Test 1 Test 2
37
Not unary truth table Not T F F T F T Test 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.