View
215
Download
1
Embed Size (px)
Citation preview
Programming Fundamentals
Floating Point Numbers
• Scientific notation98 = 0.98 x 102
204.5 = 0.2045 x 103
-0.082167 = -0.82167 x10-1
• Sign bit; Fraction (Mantissa); Exponent
S Exponent Fraction
Floating point arithmetic
• Addition & Subtraction0.45x102 + 0.32x10-1 =
-0.45x102 - 0.32x10-1 =
• Multiplication & Division0.45x102 ÷ 0.32x10-1 =
0.45x102 * 0.32x10-1 =
Data Types
• Type of quantities• Integer• Long Integer• Single• Double• String• Byte• Boolean• Currency• Date
Integer & Long Integer
• Integer– Uses 2 bytes ( 2x8=16 bits ) of memory
-32768 +32767 range– No commas, like 20,400– No decimals or fractions, like 2.45 or 4.00
• Long Integer– Uses 4 bytes
-2147483648 +2147483647 range
Single & Double
• Single– Use 4 bytes (32 bits)– Can have 7 significant digits
-3.4 E 38 3.4 E 38 range
• Double– Uses 8 bytes (64 bits)– Can have 15 significant digits
-1.8 D 308 1.8 D 308 range
Byte & Currency
• Byte– Unsigned numbers
0 255
• Currency– 8 Bytes
-922,337,203,685,477.5808 +922,337,203,685,477.5808
– No truncation & rounding
String
• Strings are non-numeric quantities, written within quotes– Examples
• ‘Islam is the solution to world problems’• ‘CS101’• ‘-423.45’• ‘Rs. 32,400.55’• ‘Osama bin Zaid’• ‘TB stands for Tooni’s Blare & a notorious disease’
Constants & Variables
• Each data type can be a constant or a variable
• Constant– The contents of the memory remain fixed– Numeric or String
pi 3.14..Speed Of Light 2.998e8k 1.38e-23Name Of My Hero “Muhammad (pbuh)”
e “-1.6e-19”
Constants & Variables
• Variables– The contents of a memory associated with a
variable is allowed to change
– A & B determine the contents of Sum and Product
Read ARead BSum = A + BProduct = A*B
Declarations
• Variables are declared at the beginning of a program using Dim– Reserve appropriate memory
• Examples– Dim MidtermScore As Integer– Dim h As single, Frequency As Single– Dim Energy As Double– Dim StudentName As String– Dim CourseTitle As String*40
Memory Allocation
A_Integer
A_Single
Variable Name Rules
• A variable name must begin with an alphabet.
• It should not be longer than 255 characters.
• Special words, such as, Dim, If, Else, Case, etc. are not permitted.
Variable Name Rules
• Some special characters are allowed within a variable name.
• A period (full stop), %, # and & are not allowed.
– Avoid special characters in a variable name.
Variable Name Rules
• Visual Basic does not distinguish between upper and lower case letters.
– AVARIABLE, AVariable, aVariable, avariable, etc. refer to the same memory location.
The Longest Variable Name
• EvenThoughItIsHardToWriteOrReadAndManyOfUsMayNeverSeeOrWriteSuchLongVariableNameYetVisualBasicAllowsTheUseOfTwoHundredFiftyFiveCharactersForTheVariableName0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
• 255 characters
Suffix Notation
Variable Data Type
Index% Integer
Counter& Long Integer
TaxRate! Single
Ratio# Double
Name$ String
Constants
• Constants can be named like variables– Or remain un-named
Const Name As String=“Muhammad (pbuh)”
Const c As Single = 2.998E8Const e As Double = -1.6D-1922.5
6/7
– An effort to change the contents of a named constant will result in an errorAssignment to constants not permitted
Operators
• + (plus) Addition Shift + =
• - (minus) Subtraction -• * (asterisk) Multiplication Shift + 8• / (slash) Division /• ^ (caret) Exponentiation Shift +
6• \ (back slash) Integer division \• Mod Integer remainder
Operations
• 2^3 = 8
• 10/8 = 1.25
• 10\8 = 1
• 8.6\2.7 = 3
• 10 Mod 8 = 2
• 2.3 Mod 2.1 = 0
• 2.3/1.2^2 = 2.3/1.44 = 1.5972
Hierarchy of Operations
1. (^) Exponentiation
2. (* or /) Multiplication & Division
3. (\) Integer Division
4. (Mod) Integer Remainder
5. (+ or -) Addition & Subtraction
• Parentheses are used to change the order of operation.
Expressions
(2*(a+b)^2 + 3*c^2)^(3/2)
(2*(a+b)^2 + 3*c^2)^3/2
• -x + y^2• -2^4 = -(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 Expressions
Amount = “Ten”
Denomination = “Thousand”
Amount & “ “ & Denomination & “ Rupees”
Ten Thousand Rupees
Amount + “ “ & Denomination + “ Rupees”
Ten Thousand Rupees
Expressions• How un = u^n is computed when n≠0?
Is n an integer?
n > 0
Compute a=u|n|
by multiplyingu, n times
Ans=1/a
Ans=a
Compute ans=n*log(u)
Yes
No
Yes
No
Logarithm of –ive numbers is not defined
Is u > 0?
Error Message
Yes
No
u can be +ive or -ive
Assignment
Salary = 12000Tax = 4/100*SalaryHouseRent = 1200NetSalary = Salary - Tax - HouseRent
Library Functions
• Abs• Y=Abs(-2) Y = 2
• Chr• Y=Chr(65) Y = “A”
• Exp• Y=Exp(2) Y = e^2
• Int• Y=Int(-2.9) Y = -2
Library Functions
• Rnd• Y=Rnd Y gets a random number 0≤Y<1
• Sgn• Y=Sgn(x)
• Str• Y=Str(4.2) Y = “4.2”
• Val• Y=Val(“-3”)+Val(“2”) Y = -3.1+2=-1.1
» The string within the Val must appear like a number
1 0
0 0
1 0
Y x
Y x
Y x
Library Functions
• Sin, Cos, Tan– Trigonometric functions
• Arguments must be in RADIANS– Sin(x), Cos(u), Tan(w)
• DateY=Date Current date mo/dy/Year
• SqrY=Sqr(4) Y = 2 = 4
Library Functions
• LcaseY=Lcase(“MyName”) Y=“myname”
• UcaseY=Ucase(“MyName”) Y = “MYNAME”
• LenY=Len(“MyName”) Y=6
Library Functions
• Left• Y=Left(“MyName”,3) Y=“MyN”
• Right• Y=Right(“MyName”,3) Y=“ame”
• Mid• Y=Mid(“MyName”,2,3) Y=“yNa”
• Log• Y=Log(72.4) Y=loge(72.4)
User-Defined Data Type
• User can define new data types by using standard data types
• ExampleType MemberName As StringID As StringDateOfBirth As DateDues As Single
End Type
User-Defined Data Type
• Then we could declare & use variables of type Customer as followsDim OldMember As MemberDim NewMember As MemberOldMember.Name Name of the Old Member
NewMember.DateOfBirthNewMamber.NameNewMember.Dues