Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

15
Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic

Transcript of Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Page 1: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Tutorial 6

Introducing Variables, Memory Concepts & Arithmetic

Page 2: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Variables

Holds data Controls for Design Programming, Variables

for Code Programming Text Property Data Variable Manipulate data w/o showing to users Store data w/o adding or using controls Hold numbers, data & time, text, and etc.

Continue

Page 3: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

One type of variable can take the same type of data. E.g. intCount = 15 (OK) intCount = “Pat” (not OK) intCount = “15” (not OK)

Must be declared. E.g.Dim intCount As IntegerDim sngPrice As Single

Continue

Page 4: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Steps of Using Variables

Declare Input: Assign a value by reading in from a

control or other source Process: Use or manipulate Output: Put the value to a control or other

object

Page 5: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Example 1:

Dim intTotal As Integer

intTotal = Val(txtTotal.Text)

intTotal = intTotal + 10

txtTotal.Text = Str(intTotal)

Page 6: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Example 2:

Dim intTotal As Integer

Dim intExtra As Integer

intTotal = Val(txtTotal.Text)

intExtra = 10

intTotal = intTotal + intExtra

txtTotal.Text = Str(intTotal)

Page 7: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Example 3:

Dim intTotal As Integer

Dim intExtra As Integer = 10

intTotal = Val(txtTotal.Text)

intTotal = intTotal + intExtra

txtTotal.Text = Str(intTotal)

Page 8: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Data Types of VariablesData Type Memory Allocation Value Range

Boolean 2 bytes True or False.

Char 2 bytes 0 through 65535 (unsigned).

Date 8 bytes 0:00:00 on January 1, 0001 through 11:59:59 PM on December 31, 9999.

String Depends on platform 0 to approximately 2 billion Unicode characters.

Short 2 bytes -32,768 through 32,767.

Integer 4 bytes -2,147,483,648 through 2,147,483,647.

Long 8 bytes -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807.

Single 4 bytes -3.4028235E+38 through -1.401298E-45 for negative values; 1.401298E-45 through 3.4028235E+38 for positive values.

Double 8 bytes -1.79769313486231570E+308 through-4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values.

Decimal 16 bytes 0 through +/-79,228,162,514,264,337,593,543,950,335 with no decimal point;0 through +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest nonzero number is+/-0.0000000000000000000000000001 (+/-1E-28).

Page 9: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Integer

Stored as 32-bit (4-byte) integers ranging in value from -2,147,483,648 through 2,147,483,647.

Provides optimal performance on a 32-bit processor, as the smaller integral types are slower to load and store from and to memory.

You can convert the Integer data type to Long, Single, Double, or Decimal without encountering a System.OverflowException error.

Page 10: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Single

Stored as IEEE 32-bit (4-byte) single-precision floating-point numbers ranging in value from -3.4028235E+38 through -1.401298E-45 for negative values and from 1.401298E-45 through 3.4028235E+38 for positive values. Single-precision numbers store an approximation of a real number.

Can be converted to the Double or Decimal data type without encountering a System.OverflowException error.

Page 11: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

String

Stored as sequences of 16-bit (2-byte) numbers ranging in value from 0 through 65535

Each number represents a single Unicode character. A string can contain up to approximately 2 billion (2 ^ 31) Unicode characters

The first 128 code points (0–127) of Unicode correspond to the letters and symbols on a standard U.S. keyboard

The second 128 code points (128–255) represent special characters, such as Latin-based alphabet letters, accents, currency symbols, and fractions

The remaining code points are used for a wide variety of symbols

Page 12: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Prefixes for Variables

Data Type Prefix Data Type Prefix

Boolean bln Integer int

Byte byt Long lng

Currency cur Short sht

Date(time) dtm Single sng

Double dbl String str

Page 13: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Arithmetic Operators

Order Operator Mathematical Operation Example

1 ( ) Parentheses

2 ^ Exponentiation CubicSpace = Length ^ 38 = 2 ^ 3

3 - Negation - Num1

4 *or/

MultiplicationDivision

Total_Sales = Unit_Price * CountClassAve = TotalScores /

NumOfStudents

5 \ Integer Division (DIV). The whole number portion of the answer in a division. The decimal position is

dropped.

CarsCanFitIn = AreaOfLot \ CarDim10 = 215 \  2015 is leftover

Continue

Page 14: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Order Operator Mathematical Operation Example

6 MOD Integer Remainder Division. The whole number portion of

a remainder in a division. If the divider is greater than

the number it is dividing, then the number become the

remainder of the MOD division.

BoothSpace = 3 MOD 103 = 3 MOD 10

7 +or-

AdditionSubtraction

Total = Num1 + Num2Profit = Sales - Expenses

& String concatenation FullName$ = First$ & Last$

Page 15: Tutorial 6 Introducing Variables, Memory Concepts & Arithmetic.

Using Debugger

Demo