370 likes | 503 Views
Introduction to Programming. CNS 1120 Exam 1 Review. Programming Language Elements. Syntax = the rules to follow Semantics = the meaning of symbols Keywords = 340, we will use about 50 Operators = old friends, and a few new ones Procedures, functions and sub-routines Data types
E N D
Introduction to Programming CNS 1120 Exam 1 Review
Programming Language Elements • Syntax = the rules to follow • Semantics = the meaning of symbols • Keywords = 340, we will use about 50 • Operators = old friends, and a few new ones • Procedures, functions and sub-routines • Data types • Data structures
Language Translation • Computers can only run Machine Language CodeAll other code must be translated into machine language • Two processes are possible • Interpreted • Compiled
No Magic Numbers • Only 0, 1, -1, “” allowed as literal constants • Named constant easier to understand • Constant reduces a the chance for typos • Makes a change throughout the program easy and correctTAX_RATE goes from 0.081 to 0.086 • Const SHIPPLING_PER_TON = .081
Data types defined in VB • String • Variable length • Fixed length • Numbers • Integer, Long, Single, Double, Currency • Others - Variant • User defined
Number data types • Whole numbers 37 -842 • Integer - small • Long - big • Fractional numbers 37.7223 -842.01 • Single - Big • Double - Bigger • Currency Special Biggest • Date Special
Data type String • 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 • Dim ZipCode as String * 5 • ZipCode = “48058”
Data Type Integer • Whole number 89, -37, -32768, 32840 • Memory set aside 2 Bytes • Maximum Size +-32K (-32767 to +32768) • Dim Age as Integer • Age = 100 • Fractional values are dropped
Data Type Long (Integer) • Whole number 89, -37, -32768, 32840 • Memory Set aside 4Bytes • Maximum Size very big • -2,147,843,648 to 2,147,843,648 • Dim BankBallance as Long ‘4 bytes • BankBallance = 1000895 ‘ No comma’s
Data TypeSingle • Fractional number 89.6 -37.0 .00006 • Maximum - Big number (-3.4E-45 to 3.4E38) • Memory Set aside 4 Bytes • 7 Significant Digits • 1.7894288 .00017894288 17894288 • Dim Wage as Single • Wage = 5.90
Data TypeDouble • Fractional number 89.6 -37.0 .00006 • 1.7894288 .00017894288 17894288 • Maximum - Very big (-1.9E324 to 4.9E308) • Memory Set aside 8 Bytes • 15 Significant Digits • -.0009787658654388955 9875.86445679987 • Math takes six time
Data Type Currency • Fractional number with 4 decimal places • Maximum Very big (15.4 digits) • Memory Set aside 8 Bytes • Dim HouseLoan as Currency • HouseLoan = 111211121112765.9999 • Math takes six times
Variable Scope • 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
Variable Scope & Life • Procedure Declaration • Scope - local to procedure • Life - length of procedure • Form Declaration • Scope - global to all procedures in form • Life - of application • Global Declaration • Scope - global to application
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. Static Joe As Integer Dim Joe As Integer
Variable (or Const) Scope • What Variable can be accessed (read or changed) by a different domain • Local - Dim within a Control_Event( ) • can only be accessed it’s domain • Module -Dim in Form General Declarations • accessed by all the form’s controls domain-bad • Global -Global in Form Declaration • anyone can access - generally bad
Variable Scope • 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 same name as Modules/Globals • Computer looks local, then module, global • All Forms are Global
Variable’s Life • Local - while code runs • Except Static variables • Module - while the form is in memory • Global - while the Program runs • Allows re-use of memory
Public Vs Private code • Public changes all variables to Global • Allows the code to be used by others • Violates basic OOP concepts - very unusual
String Concatenation & • Adds a string to the end of another string • String1 = “The big bad” • String2 = “wolf.” • String3 = String1 & String2 • String3 now contains The big badwolf. • String4 = String3
Assignment operator = • 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 • Right side of equation is completely evaluated, then transferred over = • Auto-conversion will occur unless forced by programmer – But only in this language
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
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()
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
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
Unary Operators • Positive + x = + 7 • Negative - x = -13 • Unary & Binary operators • x = 7 + -2 • x = - (-7 + 13) + (-3 - -4)
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
Parenthesis ( ) Exponential ^ Unary operators + - Mult. & Div. * / Integer Division \ Modulus MOD Add & Sub + - Concatenation & Comparison = Comparison < > Comparison < Comparison > Comparison <= Comparison >= Assignment = Operator Precedence Rule
Unicode String Like Object compare Is Logical Not Logical And Logical Or Logical Xor Logical Eqv Logical Imp Assignment = Operator Precedence Rule
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
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
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)
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
And truth table And T F T T F F F F Test 1 Test2
Or truth table Or T F T T T F T F Test 1 Test 2
Xor truth table Xor T F T F T F T F Test 1 Test 2
Not unary truth table Not T F F T Test 1