Yesterday I talked about teaching yourself programming. I said it's something anybody who understands "if/then" can do; you don't have to be a math whiz or a major-bigtime geek to learn to read and write code. I also said that today I'd present a first programming lesson. So let's get started.
a = 1;
How then can you say "equals"? Consider this:
a == 1;
When would you want to use "a == 1"? Consider this statement:
if (a == 1)
Notice that the top line is not a statement by itself. The semicolon comes at the end of the second line. Therefore the whole statement reads: "If the value of a is equal to 1, execute the function named doWhatever." (A function is just what you think it is: a named collection of statements that occurs elsewhere.) If a isn't equal to one, just skip the doWhatever() and do nothing.
Make sense so far? Good. In that case, it's time for a pop quiz. What's wrong with the following piece of code?
if (a = 1)
If that made any kind of sense, congratulate yourself. You've done your first bit of debugging.
Was any of it hard? Was any of it "rocket surgery"?
Let's recap. Here's what you learned:
1. A piece of code contains statements.
2. A statement ends with a semicolon.
3. You can have variables with names like 'a'.
4. The equals sign is actually an assignment operator.
5. But two equals-signs in a row means "equals."
6. The "if" keyword does what you think it does.
8. There are things called functions, which are basically just named collections of statements.
9. Code can be buggy without containing illegal syntax! It can be syntactically correct, yet logically flawed. And the flaw can be hard to spot.
That's a huge amount to learn in one lesson. But it really wasn't that hard, right?