140 likes | 269 Views
Introduction to Variables. Remember that memory is a long series of boxes referred to by numerical addresses. We could allow any numeric memory location to be used in our algorithms to store data in memory and then retrieving the data when we needed it.
E N D
Introduction to Variables • Remember that memory is a long series of boxes referred to by numerical addresses. • We could allow any numeric memory location to be used in our algorithms to store data in memory and then retrieving the data when we needed it. • The problem with using numeric memory addresses directly is twofold. • Programs with more than a few pieces of data would drive you crazy trying to keep track of where you put it. • Different real world machines have different memeory structures. 1 8 x y
Introduction to Variables • We use the concept of a variable to assign a name to the memory location. • Lets call our data "x" and "y". • We use a data declaration to tell the computer that we are using variable names to help us store data. • Declarations need to specify the variable NAME, TYPE, and STRUCTURE. • The computer will assign memory locations to each variable: • Here, “x” is associated with memory location 1. • Here, “y” is associated with memory location 8. 1 8 x y
Data Types and Data Structures • When specifying data in a program we need to describe its name, type and its structure. • Data's type impose meaning onto data (semantics) and data's structure impose organization (syntax) onto data. • Data Type (definition): A label applied to data that tells the computer how to interpret and manipulate data. • Type tells the computer how much space to reserve for variables and how to interpret operations on them. • Data Structure (definition): The way data is organized logically. • Describes how different pieces of data are organized.
Data Types and Structures • Usually data type and structure are not related. • Example: Integer list[n] is an array of n integer values, while Boolean blist[n] is an array of n Boolean values. • The rule is that a structure can contain any type. • Data types typically have specific operations associated with them • Example: Operations assocaited with integers are add, subtract, multiply, divide. • Most data structures also have specific operations associated with them • Typically involve storing and retrieving data from the structure • programming languages that support the structure will provide you with these operations as functions built into the language. • If not provided, you can build the functions yourself.
Data Types • The computer always needs to know the kind, or TYPE of data you expect it to work with. • The type tells the computer how to store and manipulate the data. • Basic data types include: • Integers: The counting numbers. 1, 2, 3, 4, … etc • 1, 16, -34, 1024 are integers. • Numbers: All other numbers (sometimes called “floating point”) • 3.14159, -62.0, .125 are numbers. • Characters: The letters of the alphabet, plus punctuation, plus the numerals 0, 1, 2, …, 9. Usually designated using single-quotes. • ‘a’, ‘b’, ‘c, ‘8’, ‘(‘ are characters, but ’12’ is not. • Strings: Groups of characters, designated with double quotes. • “Hello, World!” is a string; “1024” is a string, but 1024 is not. • Booleans: True and False are the only booleans.
Data Types • Programming languages that require you to specify the types of data you are using are called TYPED LANGUAGES. • The act of specifying the type is called DECLARING the type. • Programming languages that do not require you to specify the types of data you are using are called UNTYPED LANGUAGES. • The computer tries to guess the type of data you want to use. • JavaScript is an untyped language. • Untyped languages can be easier for the programmer to write code with, but programmers are always concerned with data types when writing algorithms. • We will declare our data types when writing algorithms.
Data Types - Integers • There are two basic ways to classify numeric data: Integers, and Numbers. • Integers: The counting numbers. These are used when it is clear that the values required are whole numbers only. They can be negative or positive. • Example uses: • Used for math operations • Used to count distinct items (you can't have 4.5 people in class) • Used to count through loop iterations (you cannot enter a loop 4.5 times) • Used to index arrays (example: list[i] requires i to be an integer) • Typical operations apply: addition, multiplication, subtraction, division.
Data Types - Numbers • Numbers (Floating Point): The real numbers (also known as floating point because of the way a real number is stored in a computers memory). • Used for math operations. • Used for any value not limited to integer form. • Numbers: Examples: • Areas, lengths and measures. • Temperatures. • Fiscal ( $100.35 ) • Typical operations apply: addition, multiplication, subtraction, division. • Special operations are often provided by programming languages: modulo, square, square root, etc.
Data Types - Characters and Strings • Character: A single alphabetic letter or punctuation mark. • Uses ASCII code, which is a 1 byte in length (1 byte = 8 bits). • This means there are 28 = 256 distinct values allowed. • 52 values are taken up by small and capital letters. • 10 values taken up by digits 0 - 9. • The rest are punctuation and special characters. • String: A list (or array) of characters. • It actually a structure (array) but so commonly used that most languages provide it as a type. • Most languages provide some utility functions to manipulate strings, such as concatenate, print, slice, or get_character.
Data Types - Boolean • Named for mathemitican/logician Bool. • Boolean: Used for any situation where only two values allowed. • Used for “True-False” situations: tests for Do-Loops and If-Then-Else blocks. • Standard Boolean, or logical, operations allowed: Comparisons (equals, greater than, less than), logical AND, logical OR, and logical NOT.
Data Types - Boolean • Example: IF-THEN-ELSE - "test" must return a Boolean value IF (test) THEN foo1(); ELSE foo2(); ENDIF • Example: LOOPs - "test" must return a Boolean value. WHILE (test) DO foo1(): ENDDO
Data Types - Pointers • Pointer: A data type who's value is a memory address. • Pointers are used to "point" to places in the memory. • Used to allow you to make new variables "on the fly" and refer to the new variable by its location rather than its name. • You can assign pointers values just like any other variable. • Pointer: An integer pointing to a memory address. • Used to point to complex data structures. • Used to access memory quickly. • Since it is an integer, standard math operations apply.