ThePlace

Home ] Search ] Resources ] Site Map ] Contact Me ]
Dave's Information Technology Resource

Up ]

Computers & Programming ] Operating Systems ] Programming Environments ] Windows-Based Applications ] Objects and Programming ] [ Data and Variables ] Operational Expressions ] Functions and Procedures ] Programming Control ] Algorithms ] Files, Graphics & Databases ] Software Development ] 10 Things... ]

--- Data and Variables ---

Why do we have Data and Variables?

Computer data is the reason programs exist.  Data is the numbers, text, dates, and currency that we process to accomplish work.  Imagine a spreadsheet without the numbers or a word processor without text in the document. For the purposes of programming, we deal with data in ways that let us know what kind of information we are working with.  For example, a set of data might be two numbers that are added together to create a sum. 

Think of math.  An equation might be represented as follows:

A + B = C

In this example, A, B, and C are variables.  By substituting real numbers; you could solve the equation:

A=2, B=3

2 + 3 = 5

C=5

 

Variables in programs are used in a similar fashion.  They often represent data that is to be input to the program or store data generated by the program.  

 

Computer Data

Before we go much further, consider how data and ultimately, information is stored in a computer.

The basic unit of data in a computer is a bit.  A bit is a one or zero.  A bit can be used to represent two things: on or off, yes or no, true or false, and so on.  This is a very limited set of options.

Most computer systems depend on the byte for storing data.  A byte is eight bits, which can be used to represent 256 different things (you get 256 by taking 2 to the eighth power: 28).  

Now we can encode a lot of things:

Data Type Encoding Scheme

 

Text ASCII (American Standard Code for Information Interchange) Text -- A 7 bit standard to encode letters, numbers, and special characters.  128 characters can be encoded.

ANSI Text -- An 8 bit version of ASCII, enables 256 characters to be encoded.

Also UniCode: new 16 bit standard (65K characters)

 

Raster Graphics (Bitmaps) Each pixel of the image (an X, Y coordinate space) is assigned a color value.  Depending on the number of bytes used for each pixel color, the size of the image is determined.

For example:

bullet1 byte, 256 colors
bullet2 bytes, 65,536 (65K) colors (high color)
bullet3 bytes, 16 million plus colors (true color)

An image that is 800X600 (an SVGA computer screen) various colors depths would have the following sizes (add some for information about color palettes etc.):

bullet256 colors: 480,000 bytes
bullet65K colors: 960,000 bytes
bullet16 Million colors: 1.4 megabytes (MB)

This technique results in the "best possible" image depending on the actual image size.  Graphic file formats such as JPEG and GIF use compression to reduce the data sizes.

 

Audio Digital sound depends on capturing sound levels at a very rapid pace typically, thousands of times a second.

Digital audio is based on the following...

bulletCapturing sound at intervals ranging from 5000 to 44,000 times a second (kilohertz)
bulletEach "capture" is based on a byte (256 levels) or two bytes (65,536 levels)
bulletThere may be one (monaural) or two channels (stereo)

One minute of CD-Audio Quality (the best level) involves the following:

bullet60 seconds times
bullet2 bytes (for 65,536 levels) times
bullet2 channels (stereo) times
bullet44,500 hertz
bulletFor a total of about 10.5 MB

Lower grade (telephone quality) involves the following:

bullet60 seconds times
bullet1 byte (for 256 levels) times
bullet1 channel times
bullet11,000 hertz
bulletFor a total of about 660,000 bytes

As you can see, digital audio requires a significant amount of storage.  Audio files are often compressed by reducing the frequency, channels, and bytes, and by other techniques (MP3 for instance), but these always degrade the quality of the audio.

 

 

We often combine groups of bytes for various data storage situations:

bullet

2 bytes (216 = 65536 combinations) for digital audio and high color graphics.

bullet

3 bytes (224 = 16,777,216 combinations) for true color graphics.

bullet

 Kilobytes (KB) for 1024 bytes for bulk storage of data.

bullet

Megabytes (MB) for 1,048,576 bytes for bulk storage of data.

bullet

Gigabytes (GB) for 1,073,741,824 bytes for bulk storage of data.

bullet

By the way, there are also tera-, peta-, and even larger ways to reference large numbers of bytes.

Data is stored collectively in files which can be as small as few bytes or very, very large (even hundreds of megabytes).   

 

 

Data Types

Depending on the programming environment, there are various data types to support various applications.  At a minimum, data types include numbers and strings; however, most programming environments use many data types, including dates, currency, and various formats for numbers including integers and whole numbers.

 

There are some important things to notice about data types:

  1. The type of data that is represented (string, number, date, etc.).

  2. The amount of space, in bytes, required for storing a variable in memory (disk or RAM). ]

  3. The range for the data type; for example, integers in Visual Basic range from -32,768 to 32,767.

 

Generally speaking, data types include:

bullet

Numbers including integers (1,2 3, ...) and floating point (1, 1.567, 234.678, etc.)

bullet

Text or string (often in double quotes: "this is a string")

bullet

Boolean (true or false)

bullet

Date

bullet

Arrays (lists of numbers, text strings, etc.)

bullet

 Objects

bullet

Functions (a special case where a function is defined to return a specific kind of data).

 Depending on the programming language, the kinds of data types that can be used will vary.  

The following table summarizes data types found in Visual Basic (v. 6):

Data Type

Storage Size

Range

Byte

1 byte

0 to 255

Boolean

2 bytes

True or false

 

Numeric data (including date/time)…

Integer

2 bytes

-32,768 to 32,768

Long

(long integer)

4 bytes

-2,147,483,648 to 2,147,483,647

Single

(single-precision floating-point)

4 bytes

-3.402823E38 to -1.401298E-45 for negative values;

1.401298E-45 to 3.402823E38 for positive values

Double

(double-precision floating-point)

8 bytes

-1.79769313486232E308 to

-4.94065645841247E-324 for negative values;

 4.94065645841247E-324 to

 1.79769313486232E308 for

 positive values

Currency

(scaled integer)

8 bytes

-922,337,203,685,477.5808 to 922,337,203,685,477.5807

Decimal

14 bytes

+/-79,228,162,514,264,337,593,543,950,335 with no decimal point;

+/-7.9228162514264337593543950335 with 28 places to the right of the decimal;

smallest non-zero number is

+/-0.0000000000000000000000000001

Date

8 bytes

January 1, 100 to December 31, 9999

 

Object Reference…

Object

4 bytes

Any Object reference

 

String or text data…

String

(variable-length)

10 bytes + string length

0 to approximately 2 billion

String

(fixed-length)

Length of string

1 to approximately 65,400

 

Variant data (can be anything)…

Variant

(with numbers)

16 bytes

Any numeric value up to the range of a Double

Variant

(with characters)

22 bytes + string length

Same range as for variable-length String

User-defined

(using Type)

Number required by elements

The range of each element is the same as the range of its data type.

 

 

Variables

Variables are temporary holders of data in computer programs.  For example:

bullet

 The letter "i" could be a variable that holds an integer.

bullet

 "mystring" could be a variable for string.

In a computer program, "i" could be set to 10 and "mystring" set to "A dog is in the park."  Typically you declare the name of the variable and its type prior to using it. 

During program execution, variable values may change numerous times.

Some things you should consider in creating variables:

bullet

 Be aware of the kind of data that the variable will hold.  For example, some number data types are limited in range (e.g., integers in Visual Basic are limited to a range of -32,768 to +32,768 - if a larger number is used, it would not be supported by this data type - in fact, an error would be generated).

bullet

The variable name should be recognizable.  In some cases, the letter "i" or "j" is used for integers, a practice established by tradition from Fortran.  However, in most cases, you should name variables so they are easy to identify, for example, "lastname," "SSAN (for Social Security Account Number)," and "birthdate."

bullet

Be aware of naming rules for the environment you are in.  In many cases, variable names have no spaces and can only include letters and numbers.  In some cases, variable names are case sensitive.

bullet

You should never use overly long names such as "thisistheaddressfortheuser."  Imagine the typing errors that will haunt you while debugging your program!

 

 

Creating Variables in Programs

In most programming environments, the programmer defines or declares  variables when building an application.

For example, in Visual Basic, a variable is created as follows:

Dim lastname as string

And in C, an  integer variable is created as follows:

Int x;

And in JavaScript:

Var lastname

Variables are created prior to when they are used.  For example:

Dim X,Y,Z as integer  
X = 2
Y = 3
Z = X + Y

In some programming environments, variables can be implicitly created.  For example, in Visual Basic, you can create a variable simply by using it.  Unfortunately, the variable is usually defined a variant, which can handle any kind of data (numbers or text) and requires more memory than if you had simply declared the variable as an integer or string.

 

Variable Scoping

Variables are typically used within program procedures to accomplish some task.  In some cases, variables are shared between procedures, even throughout an application.  

Variable scoping assumes you know something about how programs are organized into subroutines or functions.  We will talk more about this later.

In many programs, if a variable is declared within a procedure, it is localized to that procedure.  When the procedure is done executing, the variable is forgotten.  However, if the variable is declared outside of a particular procedure, it can be used in many procedures.  This is useful in situations where you need to share data between different procedures in a program.  Consider the following example:

Dim age as integer   'age is a variable that can be used anyhere

Sub aprocedure()

  Dim userage as integer  'userage is a local variable

  userage = 20       'userage is now equal to 20

  userage + 10 = age    'age is now equal to 30 

End sub

    'age is available to other procedures

The following illustration shows the relationship of variables (scoping) at the program, module, and routine levels.  Note that variable names used in routines can be reused in other routines, even as different data types.

 

   

Constants

A constant is a special kind of variable with a preset value that can be used throughout an application.  The advantage of a constant is that it is set in one place in the program, usually by the program itself.

System-Defined Constants

System-defined constants are often used to simplify the "remembering" of values that are used .  For example, the constant vbBlue in Visual Basic is the same as "&H00FF0000&", a hexadecimal number.

User-Defined Constants

The programmer can also declare constants for values that will be used over and over.  For example, Const myaddr as string = "111 Elm Str." results in storing the address in the variable "myaddr."

 

 

Arrays

In some situations, you may want to have a number of variables that represent a similar kind of data.  For example, you may need to collect several number of names and addresses.  You could create a variable for each name and address: name1, address1, name2, address2, name3, address3, and so forth.  But this can be difficult to keep track of in the program.  Also, you may not know how many variables to create.  And remember each variable takes up space whether you use it or not.  This is where arrays come in.

Imagine a "set of names."   The set called "names" could have several  entries, each of which could be numerically referenced.  For example:

 

Name(1) ="John"

Name(2)="Tommy"

Name(3)="Cindy"

 

This is an example of a one-dimensional array.  The array can be defined with a fixed set of potential entries.

Arrays can be multidimensional.  For example a 3 by 5 array called "codes" could be created and values applied:

 

10

22

45

67

89

12

14

17

67

104

33

45

78

99

111

If you called on codes(2,4)  you would get the value: 67 (second row, fourth item).

Some things to remember about arrays:

bullet

Multi-dimensional arrays can be confusing.

bullet

The first element of an array is the 0th element (most of the time-there are exceptions).

bullet

Dimensions are often dynamically resized and populated.    

 

User Defined Structures

Structures are often referred as:

bullet

user-defined variables

bullet

classes

bullet

objects

Structures are formed by combining other variables (strings, numbers, etc.) and even previously defined structures.

In some languages, the "type" is used:

type employee

name as string

empnumber as long

address as string

end type

In languages such as C, the "struct" keyword is used.

In this example, note that the employee data is made up of other data.

 

Stacks, Queues, and Linked Lists

Stacks are "lists" of data that operate on a last-in, first-out (LIFO) basis.  In other words, the last data put in, is the first data to be pulled out.  Stacks are often used by lower level languages to handle function calls and managing events (i.e., system interrupts).

Input Stack List Output
A in first
B is second
then C, D, E
A B C D E E out first
D out second
then C, B, A

 

Queues are also lists that operate on a first-in, first-out (FIFO) basis.  Things of a queue as a line to enter a theater.  The first person there, is the first to get seat.

Input Queue List Output
A in first
B is second
then C, D, E
A B C D E A out first
B is second
then C, D, E

 

Linked lists consist of lists in which each element of the list points to the next element.  This is efficient for managing the list because you merely need to check the pointers to and from the element to be added or deleted and adjust them.

Input Linked List Output
A in first
B is second
then C, D, E
A ->B-> C-> D-> E

to add X between B and C...

A ->B-> X->C-> D-> E

Any item can be accessed by adjusting the pointers (->)

 

 

Home ] Up ] Computer Architecture ] Database Bootcamp ] Visual BasicS ] Web Basics ] Web Multimedia ] Web Programming ] Advanced Web Topics ] Developing Web Sites ] XML Technology ] Web Glossary ]

Copyright © 1999 - 2005 
ThePlace - Written and Sponsored by Dave Hillman