CNS 1120CNS 1120
Exam 1 ReviewExam 1 Review
Programming Language Programming Language ElementsElements
Syntax = the rules to followSyntax = the rules to follow Semantics = the meaning of symbolsSemantics = the meaning of symbols KeywordsKeywords = = 340, we will use about 50340, we will use about 50 OperatorsOperators = = old friends, and a few new old friends, and a few new
onesones Procedures, functions and sub-routinesProcedures, functions and sub-routines Data typesData types Data structuresData structures
Language TranslationLanguage Translation
Computers can only run Machine Computers can only run Machine Language CodeLanguage CodeAll other code must be translated All other code must be translated into machine languageinto machine language
Two processes are possibleTwo processes are possible– InterpretedInterpreted– CompiledCompiled
NoNo Magic Numbers Magic Numbers
Only 0, 1, -1, “”Only 0, 1, -1, “” allowed as literal allowed as literal constantsconstants
Named constant easier to understandNamed constant easier to understand Constant reduces a the chance for Constant reduces a the chance for
typostypos Makes a change throughout the Makes a change throughout the
program easy and correctTAX_RATE program easy and correctTAX_RATE goes from 0.081 to 0.086goes from 0.081 to 0.086
Const SHIPPLING_PER_TON = .081Const SHIPPLING_PER_TON = .081
Data types defined in VBData types defined in VB
StringString– Variable lengthVariable length– Fixed lengthFixed length
NumbersNumbers– Integer, Long, Single, Double, CurrencyInteger, Long, Single, Double, Currency
Others - Others - VariantVariant User definedUser defined
Number data types Number data types
Whole numbers 37 -842Whole numbers 37 -842– IntegerInteger - small - small– LongLong - big - big
Fractional numbers 37.7223 -Fractional numbers 37.7223 -842.01842.01– SingleSingle - Big - Big– DoubleDouble - Bigger - Bigger– CurrencyCurrency Special Biggest Special Biggest– Date Date SpecialSpecial
Data type Data type StringString
Variable length - 2 Billion maxVariable length - 2 Billion max– Dim FirstName as StringDim FirstName as String– FirstName = “Don” FirstName= FirstName = “Don” FirstName=
“Victoria”“Victoria”– Size changes automaticallySize changes automatically– Size 10 bytes + 1 byte per letterSize 10 bytes + 1 byte per letter
Fixed length - 65,400 maxFixed length - 65,400 max– Dim ZipCode as String * 5Dim ZipCode as String * 5– ZipCode = “48058”ZipCode = “48058”
Data Type Data Type IntegerInteger
Whole number 89, -37, -32768, Whole number 89, -37, -32768, 3284032840
Memory set aside Memory set aside 2 Bytes2 Bytes Maximum Size +-Maximum Size +-32K32K (-32767 to (-32767 to
+32768) +32768) Dim Age as IntegerDim Age as Integer Age = 100Age = 100 Fractional values are droppedFractional values are dropped
Data Type Data Type Long (Integer)Long (Integer)
Whole number 89, -37, -32768, Whole number 89, -37, -32768, 3284032840
Memory Set aside Memory Set aside 4Bytes4Bytes Maximum Size Maximum Size very bigvery big
– -2,147,843,648 to 2,147,843,648 -2,147,843,648 to 2,147,843,648 Dim BankBallance as Long ‘4 bytesDim BankBallance as Long ‘4 bytes BankBallance = 1000895 ‘ No BankBallance = 1000895 ‘ No
comma’scomma’s
Data TypeData TypeSingleSingle
Fractional number 89.6 -Fractional number 89.6 -37.0 .0000637.0 .00006
Maximum - Big number (-3.4EMaximum - Big number (-3.4E-45-45 to to 3.4E3.4E3838))
Memory Set aside Memory Set aside 4 Bytes4 Bytes 7 Significant Digits7 Significant Digits
– 1.7894288 .00017894288 178942881.7894288 .00017894288 17894288 Dim Wage as SingleDim Wage as Single Wage = 5.90Wage = 5.90
Data TypeData TypeDoubleDouble
Fractional number 89.6 -Fractional number 89.6 -37.0 .0000637.0 .00006– 1.7894288 .00017894288 178942881.7894288 .00017894288 17894288
Maximum - Very big Maximum - Very big (-1.9E(-1.9E324324 to 4.9E to 4.9E308308)) Memory Set aside Memory Set aside 8 Bytes8 Bytes 15 Significant Digits15 Significant Digits
– -.0009787658654388955 -.0009787658654388955 9875.864456799879875.86445679987
Math takes six timeMath takes six time
Data Type Data Type CurrencyCurrency
Fractional number with Fractional number with 4 decimal 4 decimal placesplaces
Maximum Maximum Very bigVery big (15.4 digits) (15.4 digits) Memory Set aside Memory Set aside 8 Bytes8 Bytes Dim HouseLoan as Currency Dim HouseLoan as Currency HouseLoan = HouseLoan =
111211121112765.9999111211121112765.9999 Math takes six timesMath takes six times
Variable Scope Variable Scope
Depends on where and how declaredDepends on where and how declared– Scope How declaredScope How declared– LocalLocal - - Dim, ConstDim, Const, Static, ReDim, Static, ReDim– Module - Dim, PrivateModule - Dim, Private– GlobalGlobal - - GlobalGlobal, Public, Public
Right mouse click will show Right mouse click will show properties of variableproperties of variable
Variable Scope & LifeVariable Scope & Life
Procedure DeclarationProcedure Declaration– Scope - local to procedureScope - local to procedure– Life - length of procedureLife - length of procedure
Form DeclarationForm Declaration– Scope - global to all procedures in formScope - global to all procedures in form– Life - of applicationLife - of application
Global DeclarationGlobal Declaration– Scope - global to applicationScope - global to application
Static Vs DimStatic Vs Dim
Initializes Variable the first time only, Initializes Variable the first time only, then the value is maintained for the then the value is maintained for the remainder of the life of the program. remainder of the life of the program. Reuse of the variable (by entering Reuse of the variable (by entering the procedure), does not reinitialize the procedure), does not reinitialize the variable. Scope is unchanged.the variable. Scope is unchanged.
Static Joe As IntegerStatic Joe As Integer
Dim Joe As IntegerDim Joe As Integer
Variable (or Const) Scope Variable (or Const) Scope
What Variable can be accessed (read What Variable can be accessed (read or changed) by a different domainor changed) by a different domain
LocalLocal - Dim within a Control_Event( ) - Dim within a Control_Event( )– can only be accessed it’s domaincan only be accessed it’s domain
ModuleModule -Dim in Form General -Dim in Form General DeclarationsDeclarations– accessed by all the form’s controls accessed by all the form’s controls
domain-domain-badbad GlobalGlobal -Global in Form Declaration -Global in Form Declaration
– anyone can access - generally anyone can access - generally badbad
Variable Scope Variable Scope
Local variables Local variables can notcan not have the have the same name in the same proceduresame name in the same procedure
Local variables Local variables can havecan have the same the same name in different proceduresname in different procedures
Local variables Local variables can havecan have same name same name as Modules/Globalsas Modules/Globals
Computer looks local, then module, Computer looks local, then module, globalglobal
All Forms are GlobalAll Forms are Global
Variable’s LifeVariable’s Life
LocalLocal - while code runs - while code runs– Except Static variablesExcept Static variables
ModuleModule - while the form is in memory - while the form is in memory GlobalGlobal - while the Program runs - while the Program runs
Allows re-use of memoryAllows re-use of memory
Public Vs Private codePublic Vs Private code
Public changes all variables to GlobalPublic changes all variables to Global Allows the code to be used by othersAllows the code to be used by others Violates basic OOP conceptsViolates basic OOP concepts - very - very
unusualunusual
String Concatenation &String Concatenation &
Adds a string to the end of another Adds a string to the end of another string string
String1 = “The big bad”String1 = “The big bad” String2 = “wolf.”String2 = “wolf.” String3 = String1 String3 = String1 && String2 String2 String3 now contains The big String3 now contains The big
badwolfbadwolf.. String4 = String3String4 = String3
Assignment operator =Assignment operator =
Target = Source ‘ Target = Source ‘ required direction required direction – 3 = A if A = 7 and B = 2 then after A = 3 = A if A = 7 and B = 2 then after A =
B what is contained in both A and B ???B what is contained in both A and B ??? Target must be a variableTarget must be a variable Right side of equation is completely Right side of equation is completely
evaluated, then transferred over =evaluated, then transferred over = Auto-conversion will occur unless Auto-conversion will occur unless
forced by programmer – forced by programmer – But only in But only in this languagethis language
Functions have a return Functions have a return valuevalue
Dim str As StringDim str As String str = InputBox( “First Name” )str = InputBox( “First Name” ) Physically - the whole InputBox term Physically - the whole InputBox term
is replaced by the value that is is replaced by the value that is returned so it becomesreturned so it becomes
str = “Don”str = “Don” then the assignment is madethen the assignment is made
Conversion String to Conversion String to Number Number
Functions Val ( )Functions Val ( ) Changes from string to a number data typeChanges from string to a number data type Val(String)Val(String) changes a string a number. changes a string a number.
Combines over blanks, tabs, LF up to the Combines over blanks, tabs, LF up to the First non-number character (letter$, -)First non-number character (letter$, -)
String with no number String with no number firstfirst gets a value = gets a value = 00
Number outside acceptable value get Number outside acceptable value get Err 6Err 6 See also CInt(), CLng(), CSng(), CDbl()See also CInt(), CLng(), CSng(), CDbl()
Binary Arithmetic OperatorsBinary Arithmetic Operators
Addition Addition ++ 9 = 7 + 2 9 = 7 + 2 Subtraction Subtraction -- 5 = 7 - 2 5 = 7 - 2 Multiplication Multiplication ** 14 = 7 * 2 14 = 7 * 2 Division Division // 3.5 = 7 3.5 = 7 // 2 2 Exponentiation Exponentiation ^̂ 49 = 7 ^ 2 49 = 7 ^ 2 Integer Division Integer Division \\ 3 = 7 3 = 7 \\ 2 2 Modulo Modulo MODMOD 1 = 7 MOD 2 1 = 7 MOD 2
Integer Divisions & ModulusInteger Divisions & ModulusInteger Divisions & ModulusInteger Divisions & Modulus
6.33 6.33 6 6
38 38 // 6 6/ 38.00 38 6 6/ 38.00 38 \\ 6 6/ 38 6 6/ 38
-36-36 -36-36
20 remainder 220 remainder 2
-18 -18 2 = 38 2 = 38 MODMOD 66
20 ? = 29 20 ? = 29 MODMOD 55
- 18 - 18 ? = 16 ? = 16 MODMOD 12 = 84 12 = 84 MODMOD 20 20
Unary OperatorsUnary Operators
Positive Positive ++ x = + 7 x = + 7 Negative Negative -- x = -13 x = -13 Unary & Binary operatorsUnary & Binary operators
– x = 7 + x = 7 + --22– x = - (-7 + 13) + (-3 x = - (-7 + 13) + (-3 - - -4)-4)
Scientific or Exponential Scientific or Exponential NotationNotation
Scientific or Exponential Scientific or Exponential NotationNotation
3.083.08EE+12 = 3080000000000.00+12 = 3080000000000.00 3.08E3.08E-6-6 = 0.00000308 = 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
Operator Precedence RuleOperator Precedence Rule
1 Parenthesis ( )Parenthesis ( )2 Exponential ^Exponential ^3 Unary operators + -Unary operators + -4 Mult. & Div. * /Mult. & Div. * /5 Integer Division \Integer Division \6 Modulus MODModulus MOD7 Add & Sub + -Add & Sub + -8 Concatenation &Concatenation &
9 Comparison = Comparison = A Comparison < > Comparison < > B Comparison <Comparison <C Comparison >Comparison >D Comparison <=Comparison <=E Comparison >=Comparison >=
Z Assignment =Assignment =
Operator Precedence RuleOperator Precedence Rule
F Unicode String Unicode String LikeLike
G Object compare IsObject compare IsH Logical NotLogical NotI Logical Logical
AndAndJ Logical OrLogical OrK Logical XorLogical XorL Logical EqvLogical EqvM Logical ImpLogical Imp
Z Assignment Assignment ==
Comment’sComment’s
Used to document codeUsed to document code– Block comments tell what several lines will doBlock comments tell what several lines will do– Comment to tell that line itself Note: Page Comment to tell that line itself Note: Page
161161 REMREM as the first word in a line of code, as the first word in a line of code,
causes the computer to causes the computer to ignoreignore the whole the whole line of code line of code
REM the abbreviated symbol REM the abbreviated symbol ‘‘ can be can be used anywhere with in a line - rest of line used anywhere with in a line - rest of line ignored ignored
String OperatorsString Operators
&& (or + )(or + ) ConcatenationConcatenation operator operator Dim a As StringDim a As String Dim B As StringDim B As String a = “Tom”a = “Tom” B = “Jones”B = “Jones” lblX.caption = a & B ‘TomJoneslblX.caption = a & B ‘TomJones lblX.caption= a & “ “ & “Jones” ‘ Tom lblX.caption= a & “ “ & “Jones” ‘ Tom
JonesJones
String ComparisonsString Comparisons
( “Tom Brown” = “Tom brown” )( “Tom Brown” = “Tom brown” ) Strings are compared letter by letter untilStrings are compared letter by letter until
– A difference between the ANSI value of the two A difference between the ANSI value of the two characters in the same position is found characters in the same position is found FF
– One string ends and the other continues (diff) One string ends and the other continues (diff) FF
– Both strings end with no differences - Both strings end with no differences - TrueTrue 32 = Abs(UCase -LCase) 32 = Abs(UCase -LCase) 1, 0, -1 returned by StrComp(Str1, 1, 0, -1 returned by StrComp(Str1,
Str2,rule)Str2,rule)
String functionsString functions
LenLen(Str) ‘Returns number of characters (Str) ‘Returns number of characters ValVal (Str) ‘Ret. Numbers (first) in a string (Str) ‘Ret. Numbers (first) in a string CDbl(str) ‘ same Val but for internationalCDbl(str) ‘ same Val but for international If the function name ends in $, the the If the function name ends in $, the the
function returns a string function returns a string Str$Str$ (Num ) ‘Number to string (Num ) ‘Number to string
characterscharacters
AndAnd T F T F
T T FT T F
F F FF F F
Test 1Test 1
AndAnd truth table truth tableAndAnd truth table truth table
Test2Test2
OrOr truth table truth tableOrOr truth table truth table
OrOr T F T F
T T TT T T
F T FF T F
Test 1Test 1
Test 2Test 2
XorXor truth table truth tableXorXor truth table truth table
XorXor T F T F
T F TT F T
F T FF T F
Test 1Test 1
Test 2Test 2
NotNot unary truth table unary truth tableNotNot unary truth table unary truth table
NotNot T F T F
F TF T
Test 1Test 1