Lecture 4 - Suraj @ LUMS
Download
Report
Transcript Lecture 4 - Suraj @ LUMS
Programming Fundamentals
Chapter 4
Floating Point Numbers
Scientific notation
98 = 0.98 x 102
204.5 = 0.2045 x 103
-0.082167 = -0.82167 x10-1
Sign bit; Fraction (Mantissa); Exponent
S
Sign
Bit
Exponent
Fraction
Floating point arithmetic
Addition & Subtraction
0.45x102 + 0.32x101 =
-0.45x102 - 32x101 =
Multiplication & Division
0.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.4E38 3.4E38 range
Double
– Uses 8 bytes (64 bits)
– Can have 15 significant digits
-1.8D308 1.8D308 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”
“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.998e8
k 1.38e-23
Name 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
Read A
Read B
Sum = A + B
Product = A*B
– A & B determine the contents of Sum and
Product
Declarations
Variables are declared at the beginning of a
program using Dim
– Reserve appropriate memory
Examples
–
–
–
–
–
Dim
Dim
Dim
Dim
Dim
MidtermScore As Integer
h As single, Frequency As Single
Energy As Double
StudentName As String
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
EvenThoughItIsHardToWriteOrReadAndMa
nyOfUsMayNeverSeeOrWriteSuchLongVari
ableNameYetVisualBasicAllowsTheUseOfT
woHundredFiftyFiveCharactersForTheVaria
bleName0123456789012345678901234567
89012345678901234567890123456789012
34567890123456789012345678901234567
89
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.998E8
Const e As Double = -1.6D-19
22.5
6/7
– An effort to change the contents of a named
constant will result in an error
Assignment to constants not permitted
Operators
+ (plus)
- (minus)
* (asterisk)
/ (slash)
^ (caret)
\ (back slash)
Mod
Addition
Subtraction
Multiplication
Division
Exponentiation
Integer division
Integer remainder
Shift + =
Shift + 8
/
Shift + 6
\
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.
2.
3.
4.
5.
(^)
(* or /)
(\)
(Mod)
(+ or -)
Exponentiation
Multiplication & Division
Integer Division
Integer Remainder
Addition & Subtraction
Parentheses are used to change the order
of operation.
Expressions
[2(a b)2 3c2 ]3 2
(2*(a+b)^2 + 3*c^2)^(3/2)
[2(a b) 3c ]
3*c^2)^3/2
2
2
(2*(a+b)^2 +
x y
2
-x + y^2
-2^4 = -(2^4) = -16
(-2)^4 = 16
2 3
String Expressions
Amount = “Ten”
Denomination = “Thousand”
Amount & “ “ & Denomination & “ Rupees”
Ten Thousand Rupees
Amount + “ “ + Denomination + “ Rupees”
Ten Thousand Rupees
Assignment
Salary = 12000
Tax = 4/100*Salary
HouseRent = 1200
NetSalary = 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 = -3
Library Functions
Rnd
Y=Rnd
Sgn
Y=Sgn(x)
Str
Y=Str(4.2)
Y gets a random number 0≤Y<1
Y 1 x 0
Y 0 x0
Y 1 x 0
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
Library Functions
Sin, Cos, Tan
– Trigonometric functions
Arguments must be in RADIANS
– Sin(x), Cos(u), Tan(w)
Date
Y=Date Current date
mo/dy/Year
Sqr
Y=Sqr(4)
Y=2=
4
Library Functions
Lcase
Y=Lcase(“MyName”) Y=“myname”
Ucase
Y=Ucase(“MyName”) Y = “MYNAME”
Len
Y=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)