0010101001010100101010101010100111010011001011010100010001011110100100101010111110011010100110011101110100111101010101010101001001010101010101010101010000111110101001010101010101010101010101001010101010101010101010101010101010101001010101010101010101010010101101001010010101001011011001010101010101010101010101010010101010101010010101010101010
Lesson 2: Language Metastructure and Pseudocode
01010101000101010010101110111010111001010010100010001010101010010101010101010101010101010010000101101011011111101010010110110101010010101001010101011101001101010101010101010010001010101111100101010100101010101001010101010101001010101010010101010101010101010101101001010010101010101011110101010101101010100101010101001010101010101010101010010110101001010101001010101010Part 1: Universal Features of Programming LanguagesOk, let's get into it then. A programming language hinges on three things: variables, comparisons, and the commands that take those and create a process flow out of them.
Variables are literally labels that the computer uses to refer to a spot in memory for remembering something. That way, you can have a spot to store a bit of information like a number, and you can have a meaningful label like "Age" that you can use the word "Age" to refer to that number, and then, you can use that label to do things with a number for somebody's age. They are called variables because that spot in memory can have its contents changed any time you want, and in fact, that's a lot of what programs do, is take that info and do stuff to it. Data Types are the different types of variables, from different types of numbers to text and more, and using them well is a major task in making your program efficient and fast.
Operators and Comparisons bring this all into the realm of math, by arranging our variables into equations. For example, we can make a decision if someone's age is over a certain number, like if your age is over 21(or 18, or whatever), then you are legally allowed to drink alcohol. We can also figure out a person's age by subtracting their birth year from this year. This is the kind of simple baby step that I mentioned earlier.
Commands are the framework that surround all of that. There will be commands for creating a variable, setting or assigning a value to that variable, and then making decisions based on that variable. There will also be commands for being able to cycle through a bunch of options (mostly called loops), and options to request or process input and output operations (such as processing keystrokes or mouse movements and clicks, or writing something to the screen). Syntax is the specific pattern that all of this must match in order to work. Syntax is a curse word to most programmers, because most of the bugs you will hunt down in your career as a programmer will boil down to tiny stupid mistakes with syntax.
Now, let's dive a little deeper into each of those.
Variables and Data TypesVariables will generally have a type associated with them. The most common types are "number" (usually further split into integers and some kind of floating point - a number with a decimal, but that point can be anywhere in the number, so it is called floating), "string" (any kind of text - a literal string of characters run together into, say, a sentence - and also including single characters and sub-divisions like that), "boolean" (literally yes/no or off/on, sometimes, that's all you need), and some way of handling empty or nonexistent stuff (generally referred to as "empty", "null", and "undefined", which are often NOT the same thing). There will also be some functionality for arrays, and sometimes they're considered data types, and sometimes objects, but we will get into that later. Some languages are really explicit with this stuff, and others really don't care a lot at all. That's one of the ways that each language can differ from each other.
Splitting number up, sometimes you will not just have integer and float, but each of them will also have small and big versions of them. For integers, they are generally referred to as "short" and "long". A short integer in Visual Basic, for example, can hold numbers from -32,768 through 32,767, while a long integer can hold from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. Similarly, floating point numbers are usually split into "single" precision and "double" precision. Again using Visual Basic as an example, a "single" can hold from -3.4028235E+38 through -1.401298E-45 for negative values and from 1.401298E-45 through 3.4028235E+38 for positive values, and a "double" can hold from -1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values and from 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values.
Splitting strings up, you can have strings that are specifically designed for Unicode as versus plain ASCII, and you will usually have a type that only holds a single character. Some languages take strings really seriously and have a ton of ways to play with them, and others do not.
Which actually leads right into the last "type" of variable, arrays. Arrays are collections of variables. For example, you can consider a string to be an array of characters. In the string "Hello", the first position has the letter "H", the second has the letter "e", and so on. You can get information about the first position, for example the ASCII Decimal Code for "H" is 72, and that information can be useful by itself. Or, you could consider the string or array as a whole, and it paints a different picture. Most languages will have a special notation to them, for example "variableName [index]", where "index" is the position of the individual variable within the array. Also, most arrays start with an index of 0, so the first index is 0, the second is 1, etc. So in our variable myWord that contains "Hello", myWord[0] = "H", myWord[1]="e", and so on. Again, some languages are fine with arrays being a data type, and others are adamant that they are NOT, and we'll get more into that when we cover objects.
And, while I'm mentioning objects, I'll give you a brief taste, though they are complicated, and only implemented in some of the more advanced languages. They are similar to arrays, in that they can contain a number of other variables, but they can also contain code, from functions to get and set the variables within to the ability to change the contents in complex or unique ways, or interact with other objects. Let's take the brief example of an object "Person", which has within it a variable "Age". A couple of functions would allow you to get and change "Age", but it might also have a function to just advance "Age" by one, and that function would be called "Birthday".
There is one last comment I have on Variables, and that is the limitations on variable names. For example, most languages require that a variable start with a letter. Many require that variable names not have spaces in them, though they will usually allow dashes "-" and underscores "_". One practice that has evolved over the years is called Camel Case, and this is where a variable can consist of several words, but they are run together, and the first letter of each next word is capitalized. Thus the variable I just used "myWord". The oldest languages had limits on the length of the names as well. Good luck coming up with something descriptive and easy to remember when you only have 8 letters. Most modern languages have limits like 128 or even 256 characters, so hitting that limit is nothing you will generally have to worry about, though. Also, being as descriptive as you can with your variables is important to the readability of it, for you to understand what you are doing, and for anyone else looking at it to have a clue how your stuff works. Thus myOriginalWordEntry is great to grab the initial word, and hang onto it for further processing while finalProcessedWordResult gathers the output of your process.
Operators, Comparisons, and Common Issues With ThemOperators and Comparisons have their own section here because they have special importance in programming languages. Operators are part of the math of programming, and many work exactly like they do in regular math. You want to add two numbers or two variables, simply use a "+". Minus is similarly logical, although generally, division is done with a slash "/", and multiplication is done with an asterisk "*"The reason for the asterisk, is to make sure that the operator is not a letter, which will signify a variable (so using "x" for multiplication is a no-no).
Equals is also VERY important, and is so important that it sometimes isn't even considered with the Operators, and is instead considered a command, or usually just covered twice. Equals as an operator can be part of the set of Comparisons, like less-than "<", and greater-than">" so that you can test if things are the same. In fact, many languages go beyond the logical greater-than-or-equal-to ">=" and less-than-or-equal-to "<=", to dividing equals itself into "==" meaning if two things are equivalent, and "===" meaning two things are identical. This becomes an issue when you consider Data Types, because the number 6 can be equivalent to the string "6" (6 == "6" is true), but it will not be identical, because one is a number, and the other is a string (6 === "6" is false). As a command, equals is usually used for assignment. "myVariable = value" means that value is put into myVariable. So, "myNumber = 6" is a command in the fact that it has asked the computer to put the number 6 into the spot in memory labeled myNumber. Data types can also trip up more simple comparisons, too. How would you decide if the string "Hello" was greater than the number 3? Some programming languages would fail, and others have well defined logic for figuring something as obscure as that out.
There are also unique operators that exist only for programming, such as NOT, AND, OR, NAND, NOR, XAND, and XOR. Generally, when working with these, you are considering true/false conditions, which correspond to the binary 0 (false), and 1 (true). Generally, you will not go beyond the simple NOT, AND, and OR. NOT flips whatever you are working with, so NOT True = False, and NOT 0 = 1. AND is only true (1) if both of the things compared are true (1), thus 1 AND 1 is true (1), and 0 AND 1 is false (0), 1 AND 0 is false (0), and 0 AND 0 is false (0) - it essentially finds the minimum . OR is only true (1) when at least one of the things compared are true (1), thus 1 OR 1 is true (1), 0 OR 1 is true (1), 1 OR 0 is true (1), and 0 OR 0 is false (0) - it essentially finds the maximum. All of this applies, even when you are working on higher levels of logic. Testing for (myVariableA == 10 AND myVariableB == 20) would only result in a true if both the smaller comparisons are also true. The other 4 (NAND, NOR, XAND, and XOR) are combinations of those, and while used in older programming languages, have become purely the realm of circuit design in modern usage. This is partly because describing them and using them is hellishly complicated, and more than I can cover in the course of this class.
Commands and Syntax - Bringing It All TogetherFinally, commands - the actual heart of any programming language. These are the things that you use to tell a computer what to do, and they come in three general types: Setup Commands, Decision and Looping Commands, and Complex Functions
Setup Commands are exactly what they sound like. They will create your structures and prepare them for use. Many languages require you to set up your variables and objects before you use them (and specify their Data Type), so this is where we are at in the overall process. The word used for this part of the process varies from language to language, but some of the common ones are "declare", "initialize", "dimension", or "create instance". The Commands that do this are usually some abbreviation of the preferred descriptive word. Visual Basic, for example, refers to it as dimensioning, and the command is "Dim" (So, we would start any code for myName with "Dim myName as String")
Decision Commands are also fairly straightforward. These are where much of the real power of the language comes into play, as you can make decisions based on any of your variables, other numbers that are relevant, or objects. These are spearheaded by the infamous if-then-else structure, where you start out with an if statement that is connected to a comparison of some sort, and then a block to run if the condition evaluates to true, and a block to run if the condition results in a false (when all "else" fails
of course). Most of the time there is also the option for an "else if" (or bunch of them) to have more than one test in a particular piece of code. Loops, similarly, allow a comparison to limit the execution of a block of code, but in this case, it isn't deciding which block to run, but instead, how many times to execute a single block. The for-next structure has an independent counter cycling through a specified number of times. The do-while structure waits for some comparison to be satisfied by the changes made by the block of code, and usually has two forms to let the test come at the beginning of a loop or at the end of it. Finally, the simplest decision commands are things like goto, which doesn't compare anything, but just tells the computer to move to another area in your code, and continue processing from there.
Complex functions soak up the rest of the commands. Commands to generate a random number, commands to determine the type or status of a variable, commands to calculate mathematical extremes like trigonometric functions and roots and logs and such, commands to chop up and manipulate variables in a number of ways (like formatting a string like a date, or like money, or like a percent, etc.), commands to interact with the screen, draw stuff, grab info from the mouse, connect to a remote server, you get the idea.
Finally, we have Syntax to consider. Syntax is the specifics of how the language is built. Typical Syntax issues are things like these: How you end a line, and whether you can stack multiple commands in a line or not. Whether a variable has to be declared, and where and how that is done, or the exact rules for handling variables when declaring them is NOT required. Limits on variable names (as I mentioned before), and rules on exactly how different Data Types act, and how they can be compared. These are some of the most distinct differences between languages.
Discussion and Homework are
HERE.