Computer programs manipulate values that are typically stored in variables. Real computers will big you access to a large contiguous block of memory cells (typically 8-bit bytes) each identified by a number called its address. In reality, the address is not a physical address but what is sometimes called a virtual address, allowing each program to operate as if though it had access to the whole machine. Basically, this is an illusion maintained for the convenience of the programmer.
In theory, each memory cell could be identified by number (e.g., #4, #20038, or #162456093) but as a convenience, symbols called variable names can be associated with addresses and used instead. the collection of associations between variable names and addresses (technically known as "bindings") is called a symbol table. there may be more than one symbol table at a given time, as we'll see below. Simple variables like this are sometimes called scalars. A scalar value may be an integer (like 4), a floating point value (like 3.14), a character (like 'x'), or possibly an address (also called a pointer). More complex objects include strings (like "hello"), arrays, usr defined types, and in some languages (like MUMPS or Perl) more complex objects called hash tables (or simply arrays in MUMPS). The notation for representing variables and working with them varies from language to language. In some languages (like C) these bindings are created explicitly through declarations, e.g., int x; though in other languages (like MUMPS) they are created implicitly through use. For example, a binding for the variable X is implicitly created by the MUMPS statement SET X=2 The notation for arrays varies from language to language, too. For example, in C, a 4 element array might consist of a[0], a[1], a[2] and a[3], where the [] notation is used to represent what is called indexing into the array. In some languages (e.g., Pascal) the indices need not start at 0, but could be (say) 3 through 7, but they're still contiguous integers. Other languages (like MUMPS) drop this requirement and allow strings as subscripts (e.g., A("HELLO")). Yet other languages (like Perl) distinguish between ordinary arrays and hash tables, or simply hashes. In Perl, a hash is simply an association between values as in %a{"one"} = 1 %a{"two"} = 2 etc. but MUMPS makes no special distinction, and you'd write A("ONE")=1 A("TWO")=2 etc. Now, what can you do with a variable? The most fundamental operation is setting the referenced memory cell to a certain value. This is called assignment, and is written in different ways in different languages a := 1; (Pascal) a = 1; (C) SET A=1 (MUMPS) You can also assign to one variable the value of another variable. Note, however, that you are NOT changing the variables to reference the same memory cell; instead, each variable continues to reference its own memory cell, but each has its own copy of the same value. Next, you can perform arithmetic (pronounced arith-MET-ic) operations on variables or literal values (like -3 or 2.71). The notation may differ a bit from language to language, but the basic one are addition: a + b subtraction: a - b multiplication: a * b division: a / b unary negation: -a exponentiation: commonly a**b or a^b Languages that support both integer and floating point values may include a truncating division (where 5/2 is 2, not 2.5) and a modulus operator ("5 mod 2 = 1" means that the remainder obtained when dividing 5 by 2 is 1). Again, the notation varies from language to language. Next, variables or other expressions can be combined to yield truth values (usually called Boolean values), and in many languages there is a separate Boolean data type. The notation varies from language to language, but using conventional mathematical notations 2 < 3 is true, and 2 > 3 is false. A computer program consists of a series of statements, like a = 1; b = 2; ... z = 26; but in order to do anything useful, a computer program needs to be able to alter its behavior based on the current values of variables. This is where control structures come in. The most basic ones are if, for, while and do. IF - This statement checks a condition and then executes a series of statements depending on whether or not the condition evaluates to true. For example, using Pasal-like syntax we might have if (x < 0) then begin x := -x end (If x is negative, switch its sign.) FOR - This statement is used for what is called iteration (performing an action over and over again). Using Pascal-like syntax we might have for x := 1 to 10 do begin x[i] := i end (This sets x[1] to 1, x[2] to 2, etc.) WHILE - This statment is used to execute another statement (or block of statements) so long as some condition is true. For example, the above loop might have been written i := 0; if (i < 10) do begin i := i + 1; x[i] := i end DO - The concept here is similar, but an action is repeated until a condition becomes false. Again, the same loop could have been written i := 1; do begin x[i] := i; i := i + 1 until (not (x < 10)) This is all you really need to write interesting programs (assuming your input values are already in memory and you're content to leave your output in memory when you're done), but it is useful to divide a program into subprograms. This is where additional subprograms come into play, because each subprogram may have its own symbol table with its own variable bindings. This is most clearly illustrated by a technique known as recursion, where a program calls itself over and over again until some termination condition is reached. Recall that in mathematics we define 5! (read "5 factorial") as 5! = 5 * 4 * 3 * 2 * 1 and, in general n! = n * (n - 1) * (n - 2) * ... * 1 Now, in this example I'll use a made up language function factorial (integer n in) returns integer begin n is private; if (n = 1) return 1; else return m * factorial (m - 1); end write factorial(3); This program will print out 6 but how does it work? Well, when we initially call factorial, the function gets its own symbol table in which n is bound to 3. Now, it is not true that 3 = 1, so factorial calls factorial and the new called function gets another private symbol table in which n is bound to 2. The process repeates with n bound to 1, but this time factorial returns 1 to the calling instance. That instance multiples 2 and 1, returning the result (2) to the calling instance, which returns 2 to the calling instance, which multiplies it by 3. Finally, the result (6) is printed out. This program could easily have been written with a simple FOR loop, but the point of this example is to show how each call may have associated with it its own symbol table. That symbol table is also known as the scope of the variable. That's about it. In different languages these constructs may be expressed differently, and they may be mde more general, but not in a way that adds any essential capability to the language. Now you know all about programming. === Gregory Woodhouse <[EMAIL PROTECTED]> "Design quality doesn't ensure success, but design failure can ensure failure." --Kent Beck ------------------------------------------------------- SF.Net email is Sponsored by the Better Software Conference & EXPO September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf _______________________________________________ Hardhats-members mailing list Hardhats-members@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/hardhats-members