Programming FundamentalsProgramming Fundamentals
Chapter 4Chapter 4
Floating Point NumbersFloating Point Numbers
Scientific notationScientific notation98 = 0.98 x 1098 = 0.98 x 1022
204.5 = 0.2045 x 10204.5 = 0.2045 x 1033
-0.082167 = -0.82167 x10-0.082167 = -0.82167 x10-1-1
Sign bit; Fraction (Mantissa); ExponentSign bit; Fraction (Mantissa); Exponent
S Exponent Fraction
Sign Bit
Floating point arithmeticFloating point arithmetic
Addition & SubtractionAddition & Subtraction0.45x100.45x1022 + 0.32x10 + 0.32x1011 = =
-0.45x10-0.45x1022 - 32x10 - 32x1011 = =
Multiplication & DivisionMultiplication & Division0.45x100.45x1022 ÷÷ 0.32x10 0.32x10-1-1 = =
0.45x100.45x1022 * 0.32x10 * 0.32x10-1-1 = =
Data TypesData Types Type of quantitiesType of quantities
IntegerInteger Long IntegerLong Integer SingleSingle DoubleDouble StringString ByteByte BooleanBoolean CurrencyCurrency DateDate
Integer & Long IntegerInteger & Long Integer
IntegerInteger– Uses 2 bytes ( 2x8=16 bits ) of memoryUses 2 bytes ( 2x8=16 bits ) of memory
-32768 -32768 +32767 range +32767 range– No commas, like 20,400No commas, like 20,400– No decimals or fractions, like 2.45 or 4.00No decimals or fractions, like 2.45 or 4.00
Long IntegerLong Integer– Uses 4 bytes Uses 4 bytes
-2147483648 -2147483648 +2147483647 range +2147483647 range
Single & DoubleSingle & Double
SingleSingle– Use 4 bytes (32 bits)Use 4 bytes (32 bits)– Can have 7 significant digitsCan have 7 significant digits
-3.4E38 -3.4E38 3.4E38 range 3.4E38 range
DoubleDouble– Uses 8 bytes (64 bits)Uses 8 bytes (64 bits)– Can have 15 significant digitsCan have 15 significant digits
-1.8D308 -1.8D308 1.8D308 range 1.8D308 range
Byte & CurrencyByte & Currency
ByteByte– Unsigned numbersUnsigned numbers
0 0 255 255
CurrencyCurrency– 8 Bytes8 Bytes
-922,337,203,685,477.5808 -922,337,203,685,477.5808 +922,337,203,685,477.5808 +922,337,203,685,477.5808
– No truncation & roundingNo truncation & rounding
StringString
Strings are non-numeric quantities, written Strings are non-numeric quantities, written within quoteswithin quotes– ExamplesExamples
““Islam is the solution to world problems”Islam is the solution to world problems” ““CS101”CS101” ““-423.45”-423.45” ““Rs. 32,400.55”Rs. 32,400.55” ““TB stands for Tooni’s Blare & a notorious disease”TB stands for Tooni’s Blare & a notorious disease”
Constants & VariablesConstants & Variables
Each data type can be a constant or a Each data type can be a constant or a variablevariable
ConstantConstant– The contents of the memory remain fixedThe contents of the memory remain fixed– Numeric or StringNumeric or String
pi pi 3.14 3.14
Speed Of Light Speed Of Light 2.998e8 2.998e8
k k 1.38e-23 1.38e-23
Name Of My Hero Name Of My Hero ““Muhammad (pbuh)Muhammad (pbuh)””
e e ““-1.6e-19-1.6e-19””
Constants & VariablesConstants & Variables
VariablesVariables– The contents of a memory associated with a The contents of a memory associated with a
variable is allowed to changevariable is allowed to change
– A & B determine the contents of Sum and A & B determine the contents of Sum and ProductProduct
Read ARead BSum = A + BProduct = A*B
DeclarationsDeclarations
Variables are declared at the beginning of a Variables are declared at the beginning of a program using program using DimDim
– Reserve appropriate memoryReserve appropriate memory
ExamplesExamples– Dim MidtermScore As IntegerDim MidtermScore As Integer– Dim h As single, Frequency As SingleDim h As single, Frequency As Single– Dim Energy As DoubleDim Energy As Double– Dim StudentName As StringDim StudentName As String– Dim CourseTitle As String*40Dim CourseTitle As String*40
Memory AllocationMemory Allocation
A_Integer
A_Single
Variable Name RulesVariable Name Rules
A variable name must begin with an A variable name must begin with an alphabet.alphabet.
It should not be longer than 255 It should not be longer than 255 characters.characters.
Special words, such as, Special words, such as, Dim, If, Else, Dim, If, Else, CaseCase, etc. are not permitted., etc. are not permitted.
Variable Name RulesVariable Name Rules
Some special characters are allowed Some special characters are allowed within a variable name.within a variable name.
A period (full stop), %, # and & are not A period (full stop), %, # and & are not allowed.allowed.
– Avoid special characters in a variable name.Avoid special characters in a variable name.
Variable Name RulesVariable Name Rules
Visual Basic does not distinguish between Visual Basic does not distinguish between upper and lower case letters.upper and lower case letters.
– AVARIABLE, AVARIABLE, AVariableAVariable, , aVariableaVariable, , avariableavariable, etc. refer to the same memory , etc. refer to the same memory location. location.
The Longest Variable NameThe Longest Variable Name
EvenThoughItIsHardToWriteOrReadAndMaEvenThoughItIsHardToWriteOrReadAndManyOfUsMayNeverSeeOrWriteSuchLongVarinyOfUsMayNeverSeeOrWriteSuchLongVariableNameYetVisualBasicAllowsTheUseOfTableNameYetVisualBasicAllowsTheUseOfTwoHundredFiftyFiveCharactersForTheVariawoHundredFiftyFiveCharactersForTheVariableName0123456789012345678901234567bleName0123456789012345678901234567890123456789012345678901234567890128901234567890123456789012345678901234567890123456789012345678901234567345678901234567890123456789012345678989
255 characters255 characters
Suffix NotationSuffix Notation
VariableVariable Data TypeData Type
Index%Index% IntegerInteger
Counter&Counter& Long IntegerLong Integer
TaxRate!TaxRate! SingleSingle
Ratio#Ratio# DoubleDouble
Name$Name$ StringString
ConstantsConstants
Constants can be named like variablesConstants can be named like variables– Or remain un-namedOr remain un-named
Const Name As String=Const Name As String=““Muhammad (pbuh)Muhammad (pbuh)””
Const c As Single = 2.998E8Const c As Single = 2.998E8
Const e As Double = -1.6D-19Const e As Double = -1.6D-19
22.522.5
6/76/7
– An effort to change the contents of a named An effort to change the contents of a named constant will result in an errorconstant will result in an errorAssignment to constants not permittedAssignment to constants not permitted
OperatorsOperators
+ (plus)+ (plus) AdditionAddition Shift + =Shift + = - (minus)- (minus) SubtractionSubtraction -- * (asterisk)* (asterisk) MultiplicationMultiplication Shift + 8Shift + 8 / (slash)/ (slash) DivisionDivision // ^ (caret)^ (caret) ExponentiationExponentiation Shift + 6Shift + 6 \ (back slash) \ (back slash) Integer divisionInteger division \\ ModMod Integer remainderInteger remainder
OperationsOperations
2^3 = 82^3 = 8 10/8 = 1.2510/8 = 1.25 10\8 = 110\8 = 1 8.6\2.7 = 38.6\2.7 = 3 10 Mod 8 = 210 Mod 8 = 2 2.3 Mod 2.1 = 02.3 Mod 2.1 = 0 2.3/1.2^2 = 2.3/1.44 = 1.59722.3/1.2^2 = 2.3/1.44 = 1.5972
Hierarchy of OperationsHierarchy of Operations
1.1. (^) (^) ExponentiationExponentiation
2.2. (* or /)(* or /) Multiplication & DivisionMultiplication & Division
3.3. (\)(\) Integer DivisionInteger Division
4.4. (Mod)(Mod) Integer RemainderInteger Remainder
5.5. (+ or -)(+ or -) Addition & SubtractionAddition & Subtraction
Parentheses are used to change the order Parentheses are used to change the order of operation.of operation.
ExpressionsExpressions
(2*(a+b)^2 + 3*c^2)^(3/2)(2*(a+b)^2 + 3*c^2)^(3/2)
(2*(a+b)^2 + 3*c^2)^3/2(2*(a+b)^2 + 3*c^2)^3/2
-x + y^2-x + y^2 -2^4 = -(2^4) = -16-2^4 = -(2^4) = -16 (-2)^4 = 16(-2)^4 = 16
2 2 3 2[2( ) 3 ]a b c
2 2 3[2( ) 3 ]
2
a b c
2x y
String ExpressionsString Expressions
Amount = “Ten”Amount = “Ten”
Denomination = “Thousand”Denomination = “Thousand”
Amount & “ “ & Denomination & “ Rupees”Amount & “ “ & Denomination & “ Rupees”
Ten Thousand RupeesTen Thousand Rupees
Amount + Amount + “ ““ “ + Denomination + “ Rupees” + Denomination + “ Rupees”
Ten Thousand RupeesTen Thousand Rupees
AssignmentAssignment
Salary = 12000Salary = 12000
Tax = 4/100*SalaryTax = 4/100*Salary
HouseRent = 1200HouseRent = 1200
NetSalary = Salary - Tax - HouseRentNetSalary = Salary - Tax - HouseRent
Library FunctionsLibrary Functions
AbsAbs Y=Abs(-2)Y=Abs(-2) Y = 2Y = 2
ChrChr Y=Chr(65)Y=Chr(65) Y = “A”Y = “A”
ExpExp Y=Exp(2)Y=Exp(2) Y = e^2Y = e^2
IntInt Y=Int(-2.9)Y=Int(-2.9) Y = -3Y = -3
Library FunctionsLibrary Functions
RndRnd Y=RndY=Rnd Y gets a random number 0Y gets a random number 0≤Y<1≤Y<1
SgnSgn Y=Sgn(x)Y=Sgn(x)
StrStr Y=Str(4.2)Y=Str(4.2) Y = “4.2”Y = “4.2”
ValVal Y=Val(“-3”)+Val(“2”)Y=Val(“-3”)+Val(“2”) Y = -3.1+2=-1.1Y = -3.1+2=-1.1
The string within the Val must appear like a numberThe string within the Val must appear like a number
1 0
0 0
1 0
Y x
Y x
Y x
Library FunctionsLibrary Functions
Sin, Cos, TanSin, Cos, Tan– Trigonometric functionsTrigonometric functions
Arguments must be in RADIANSArguments must be in RADIANS– Sin(x), Cos(u), Tan(w)Sin(x), Cos(u), Tan(w)
DateDateY=DateY=Date Current date mo/dy/Year Current date mo/dy/Year
SqrSqrY=Sqr(4)Y=Sqr(4) Y = 2 = Y = 2 = 4
Library FunctionsLibrary Functions
LcaseLcaseY=Lcase(“MyName”) Y=Lcase(“MyName”) Y=“myname” Y=“myname”
UcaseUcaseY=Ucase(“MyName”) Y=Ucase(“MyName”) Y = “MYNAME” Y = “MYNAME”
LenLenY=Len(“MyName”) Y=Len(“MyName”) Y=6 Y=6
Library FunctionsLibrary Functions
LeftLeft Y=Left(“MyName”,3) Y=Left(“MyName”,3) Y=“MyN” Y=“MyN”
RightRight Y=Right(“MyName”,3) Y=Right(“MyName”,3) Y=“ame” Y=“ame”
MidMid Y=Mid(“MyName”,2,3) Y=Mid(“MyName”,2,3) Y=“yNa” Y=“yNa”
LogLog Y=Log(72.4) Y=Log(72.4) Y=log Y=logee(72.4)(72.4)