Tuesday, April 19, 2011

LESSON 1: Algebra for Programming

There isn't really much to say for this quite yet. But you need a basic understanding of algebra. Don't get scared! When I say basic... I mean really basic.

What are variables, references, and identifiers?
Think back to grade school. Does this look familiar?

x = 10, y = 5

x + y = 15

If you understood what just happened there, then you're doing pretty good already.

In math, we call x and y "variables". In programming, we call them that too. But more specifically, we call them "references". They are a placeholder for a value. In this example 'x' held the value of 10 and 'y' held the value of 5. So saying x + y is the same as saying 10 + 5. And of course, 10 + 5 = 15 so x + y = 15 as well.

The reasons why we call them references is pretty broad, but in short we do it because

A) Sometimes these variables are not variable in that they do not "vary".
B) They don't actually hold a value

When you declare a variable or reference in programming, what you're actually doing is reserving space in memory that will contain the data in which you are referring to. What's the difference? Why does it matter? We'll get to that later. All that matters at this stage is knowing what I'm talking about when I say "variable" or "reference".

You will also see variables referred to as "identifiers". Identifiers are the same thing. You're essentially just making a name for the placeholder.

Okay, so back to declaring variables. In programming, "declaring" a variable is telling the computer to reserve memory in advance for the value that will be stored inside of this variable. By doing this, we allow ourselves to refer to that value, and in most cases, manipulate that value.

Lets practice:

x = 10
y = 2
z = 4

what is x + z - y?

answer: 12

what is (x / y) + z?

answer: 9 - don't forget, you always perform operations inside parenthesis first! Order of operations will be covered in a later lesson.

In programming, you generally want to be descriptive about your identifiers. Name them in a way that you will know what is contained within them.

myAge = 31
yourAge = 25
minimumAge = 21

what is myAge - yourAge?

answer: 6

what is yourAge + minimumAge?

answer: 46

--------------------------------------

In programming, the equation that you use  isn't generally designed to be solved. Its not like algebra where you say:

2x + y = 5, find the value of y.    **see later note

When you write an equation, you do it so that we can work with whatever the result of the equation turns out to be. For example, assume i have 3 variables: myAge, yourAge, and differenceInAge. Let's set values to everything but the last one

myAge = 31
yourAge = 25
differenceInAge = ?

We have two variables that now hold a value and a third that is completely empty and holds no value at all (also known as NULL). If i perform some arithmetic on the first couple variables, I'll end up with another number, floating in limbo. What do I do with it? In programming, there is a number of things you could do with that number. For the sake of this example, lets take that answer and store it into the null variable, differenceInAge:

myAge - yourAge = differenceInAge

Now what we're saying here is that the result of the math operation on the left side of the "=" sign is the SAME as the value stored in the variable "differenceInAge":

31 - 25 = 6 so... differenceInAge is no longer null, its value is 6.

There is one slight problem with this equation. Its syntax is wrong! In programming, the operation being performed MUST be on the RIGHT side of the "=" sign:

differenceInAge = myAge - yourAge

The logic behind this can be self-explained by reading this expression as follows: "differenceInAge BECOMES myAge minus yourAge." Meaning the variable differenceInAge now carries the same value as the resulting value of myAge - yourAge.

**back to our algebra example:
2x + y = 5
If we were to follow the syntax rules, we could still solve this:

y = (2 * x) - 5

Now, y holds a value and can be used. This is where your knowledge of algebra can come in handy. In order for this to fit the syntax, we had to isolate y.
More of this to come in later lessons.


So thats pretty much all you need to know about Algebra as it applies to programming... for now. Pretty easy, right?

Summary and add'l notes
  • Variables, References, and Identifiers are all pretty much the same thing. They are place holders or identifying names for placeholders.
  • Once a value is stored inside of a variable, the variable can mostly be used as if it was the raw value itself. This isn't ALWAYS true, but we will cover more about this once we talk about Data Types.
  • When naming your variable, choose something descriptive so that you will know what is contained (or supposed to be contained) within that variable, at a glance.
  • The results of any operation can be stored into another variable.
  • An equation will also be referred to as an "expression"
  • "Syntax" is how we describe the format of an expression. Most things must be written in a certain way for the compiler to understand what it is you're trying to say.

No comments:

Post a Comment