32
Programming Fundamentals

Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

  • View
    215

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

Programming Fundamentals

Page 2: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 3: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; 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 =

Page 4: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

Data Types

• Type of quantities• Integer• Long Integer• Single• Double• String• Byte• Boolean• Currency• Date

Page 5: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 6: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 7: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 8: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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’

Page 9: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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”

Page 10: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 11: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 12: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

Memory Allocation

A_Integer

A_Single

Page 13: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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.

Page 14: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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.

Page 15: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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.

Page 16: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

The Longest Variable Name

• EvenThoughItIsHardToWriteOrReadAndManyOfUsMayNeverSeeOrWriteSuchLongVariableNameYetVisualBasicAllowsTheUseOfTwoHundredFiftyFiveCharactersForTheVariableName0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789

• 255 characters

Page 17: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

Suffix Notation

Variable Data Type

Index% Integer

Counter& Long Integer

TaxRate! Single

Ratio# Double

Name$ String

Page 18: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 19: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

Operators

• + (plus) Addition Shift + =

• - (minus) Subtraction -• * (asterisk) Multiplication Shift + 8• / (slash) Division /• ^ (caret) Exponentiation Shift +

6• \ (back slash) Integer division \• Mod Integer remainder

Page 20: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 21: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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.

Page 22: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 23: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

String Expressions

Amount = “Ten”

Denomination = “Thousand”

Amount & “ “ & Denomination & “ Rupees”

Ten Thousand Rupees

Amount + “ “ & Denomination + “ Rupees”

Ten Thousand Rupees

Page 24: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 25: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

Assignment

Salary = 12000Tax = 4/100*SalaryHouseRent = 1200NetSalary = Salary - Tax - HouseRent

Page 26: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 27: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 28: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 29: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

Library Functions

• LcaseY=Lcase(“MyName”) Y=“myname”

• UcaseY=Ucase(“MyName”) Y = “MYNAME”

• LenY=Len(“MyName”) Y=6

Page 30: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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)

Page 31: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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

Page 32: Programming Fundamentals. Floating Point Numbers Scientific notation 98 = 0.98 x 10 2 204.5 = 0.2045 x 10 3 -0.082167 = -0.82167 x10 -1 Sign bit; Fraction

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