What is a variable in computer programming? How do computers think when they're just a bunch of tiny electric currents on a chip? How are numbers put into the memory cells of a computer? http://youtube.com/watch?v=V0aBwgLRNXI Electric CurrentsA Computer is really just electric currents on a chip.If electricity is running through a current, we call that a 1. If no electricity is running through it, we call that a 0. Everything-- operating systems, programs, videos-- is built from these 0s and 1s. Each 'current' is called a bit. A bit holds either a 0 or a 1. We'll talk about binary numbers in another lecture. Computers understand numbers, not words. 0s and 1s. But people aren't so great with 0s and 1s. High-Level Programming Languages and VariablesJava, Python, C and Ruby are examples of High-Level Programming Languages.High-level programming languages allow people to use symbolic names instead of just 0s and 1s. A computer’s memory is a contiguous sequence of slots, or memory cells. All types of data-- whole numbers, floating point numbers, strings of characters, boolean values-- can be stored in memory cells. A variable is a named memory cell. We call it a variable because the value in the cell can change. Memory cells can be of different sizes: 8 bits, 16 bits, 32 bits, 64 bits. Different sized cells are used to store different types of data. A cell of 8 bits is called a byte. An integer is a whole number. An integer is generally stored in 4 bytes, or 32 bits. A floating point number is a number with decimal points, e.g., 34.56. A floating point number is stored in 8 bytes. A character is a symbol on the keyboard. A character is generally stored in 2 bytes. ASCII and Unicode are protocols for how symbols are mapped to numbers. Computers assign addresses to locations in memory. Most memory systems are byte-addressable: an address is given to every byte, beginning with 0. Question: What bit does address 22 refer to? Assignment StatementsA common program statement is an assignment statement like the following: principal=10000 This means "put the number 10000 in the memory cell named 'principal'. The system keeps track of a mapping between variable name and memory cell. With high-level languages, the programmer need not be concerned with the actual address of a variable. 'principal' might be mapped to the address 8342. Then the assignment statement above would mean: put 10000 in memory cell 8342 The = in an assignment statement does not mean equals. It means 'is assigned'. We say 'the variable principle is assigned 10000'. A statement like: principal=principal + 1 is one that can mystify beginning programmers. It means: the memory cell principal is assigned its current value plus one. The right-hand expression of the assignment operator ‘=’ is evaluated first, then placed in the memory cell denoted by the left-side variable. In our sample, we first evaluate the right-hand side, 'principal + 1'. Part of evaluating is fetching the value of variables. In this case, the current value of principal is 10000 (from the first assignment statement). So we evaluate the right-hand side expression as 10000+1=10001. This value is then placed in the variable principal. Here's a simplified example of what happens when a program is executed: Program Codeprincipal=10000interestRate = 0.2 oneYearReturn=principal*interestRate Memory
Symbol Table
The program puts data in contiguous memory. So the variable 'principal' is at address 0, 'interestRate' at address 4, and oneYearReturn at address 12. In-Class Questions1. Why are the addresses in the table labeled 0,4,12 instead of 0,1,2,...2. Given the 'trace' shown above as example, show the main memory and symbol table after execution of the following: name="joe" x= 92 y = x+77 3. The main memory shown is too simplistic. In a real program, variable storage would not begin at address 0. How is memory for real computer applications organized? |