2.1 LOGIC
The question of programming starts with the question of logic. All pointy ears aside; the idea of logic is not some complex philisophical concept that rules the daily life... logic is the question of the obvious, measurable things in front of you. The "Duh!!" factor. Logic is holding an apple and saying, "I am holding an apple." Philosophy is holding an apple and saying "This apple dreams of pies." So, the first lesson is… programming is only dealing with what is measureable. So lets get down to measure.
2.2 Truth or Dare
What is true? What is false? You can’t get away with any escapism when answering this question for computers. What is true is true and what is false is false, and understanding how this works is VERY important. The complexity comes when you are trying to chain together a number of questions together to get the answer that you want.
2.3 IF I only….
The first logic idea is the decision gate. This is the most useful and most common of all logical questions. IF some question is true, I want this to happen. But it can get more complex than that…. IF this is true, then I want this to happen, in all other cases, I want that to happen. To even more compilicate things, you can chain together a bunch of statements.
Here’s an example.. the hardest question in the Universe: Where togoto for lunch.
IF ( I have $20 in my pocket )
I will goto The Olive Garden
ELSE IF ( I have less than $20 but more than $10 )
I will goto Village Inn
ELSE IF ( I have less than $10 but more than $7 )
I will goto Subway
ELSE IF ( I have less than $7 but more than $2 )
I will goto Jack in the box
ELSE in all other cases…
I will go home and eat Peanut butter and jelly
2.4 Running in Circles (that’s…looping)
Looping is another very commong logical construct. In the most basic level, all loops have three parts: The beginning condition, the repeated portion and the exit condition. The beginning condition sets the starting values, and important part since your “exit condition” usually depends on what is set in the beginning. The repeated portion is one or many things that are done...repeatedly...every time the loop is executed. The exit condition is evaluated every time the loop is run to see if the conditions have changed to allow the loop to be finished.
There are some common mistakes in looping. Looping without a proper exit condition can effectivly go on without end. This would effectivly “crash” most programs, or even worse, eat up available memory until there’s nothing left. However, most operating systems now detect “runaway” programs and let you kill them before they do any damage.
2.5 The FOR loop
A “For” loop is one basic way to run through a series of numbers. In fact..to even do a for loop you MUST have an integer value to run through. So..basically, you can run a for loop from any point to any point that can fit in the integer variable you declair. “For 1 to 1000 do this” or “For –100 to +100 do that” and so forth.
Lets start out with a looping example. Twenty-five kittens are sitting in a room. One kitten has 25 fuzzy mice and wants to share with the other kittens.
For every fuzzy mice do
Give one kitten one mouse
Move to the next mouse
So…if these mice were numbered… the first mouse would be 1..next mouse would be 2…and so forth.
Now what if this kitten changed her mind and wanted her fuzzy mice back?
For every kitten do
Take one fuzzy mice
Move to the next kitten.
The trick with For loops is that the begginning and ending conditions are built right into the statement. We are moveing from one number to another number, and will not stop until we’re done.
2.6 While Loops
While loops come in two different flavors; the “do…while” loop that always runs once, or the “while” loop. Both of these are hinged on an exit condition. However, unlike “For” loops, while loops require that you set both the beginning and ending condition yourself, and isn’t taken care of by the loop itself. If you do not fulfil either condition you can have the loop that will never execute, or executes the wrong number of times, or never ends.
So our little kitten wants to make her own fuzzy mice. She knows how to make one, but doesn’t know how many she can make until she goes through all the materials and use it to make each mouse.
Beginning condition: Set out fuzzy mouse materials
LOOP: while fuzzy mouse materials last do the following
Collect materials to make one fuzzy mouse.
Make the fuzzy mouse
Move on to the next mouse
Ending condition: no more materials to make a full fuzzy mouse.
So, this loop will only end until the materials ran out. Well…what if we wanted to only work for four hours.
Beginning condition: Set out fuzzy mouse materials
LOOP: while fuzzy mouse materials last do the following AND working time is less than four hours
Collect materials to make one fuzzy mouse.
Make the fuzzy mouse
Move on to the next mouse
Ending condition: no more materials to make a full fuzzy mouse AND working time is less than four hours.
Only difference this time is that there is a double ending condition. Both have to be true in order to exit the loop.
2.7 Sentinel Value
One idea that is very old is the use of a “Sentinel” value. This is some sort of number, index, or condition that is unique enough to never happen in a normal case, and can be used as an “ending condition” for loops. Say, the use of “-1” when you’re dealing with the number of inventory, where you’ll never have (except in Enron-run companies) negative inventory.
3.0 Knowing the Truth and the False of it
One complex idea with logic is knowing when something is true and when something is false. This…may seem oddly obvious, but it is a bit more than that. When you have one statement, it’s either true or false. “The sky is blue” is a true or false statement statement. Its when you combine statements that you get into trouble. So how do you combine logical True/False questions?
3.1 AND, OR, NOT, and sometimes eXclusive OR
Your logical AND requires that both statements to be true for the whole combined statement to be true. “If (a AND b) then” will only execute if both “a” is true and “b” is true. In C++ and java, the AND is represented as two andpersand (&&). Pascal and Basic actually use the word “and” instead.
Your logical OR only requires that one or the other be true for the whole statement to be true. “If (a OR b) then” will execute if either “a” or “b” is true. In C++ and java, the OR is represented by two verticle bars (). Pascal and Basic, again, uses the word “OR.”
The NOT value is used to make a statement opposite of what it really is. If a statement is True, then NOT will make it false. This is very useful when trying to make your programs more readable. “while Not( boolHavingProblems) do” makes it very clear what you are trying to say.
Exclusive Or (often called “XOR”) is a different version of OR, in that if both values are the same (doesn’t matter if they are both true or both false) the result is false. That’s why its “exclusive.” “if (a XOR b) then” will only execute if either “a” or “b” is false and the other is true.
3.2 Truth Tables
A common logical exercise is to list a Truth Table. This is useful to figure out what can or can not happen in your software. Understanding the patterns in a truth table is very important. The way you read it is, each variable is assigned every possible combination of “True” and “False” to find out what the resutls will be.
A AND B Makes
T T T
T F F
F T F
F F F
A OR B Makes
T T T
T F T
F T T
F F F
NOT A Makes
T F
F T
A XOR B Makes
T T F
T F T
F T T
F F F
Now try to combine things into a complex set of conditions
A AND (B OR C) [what B OR C MAKES] MAKES
T (T T) T T
T (T F) T T
T (F T) T T
T (F F) F F
F (T T) T F
F (T F) T F
F (F T) T F
F (F F) F F
3.3 Translating True and False
True and false to a computer is basically either on or off, 1 or 0. The number 1 is assumed to be True, and the number 0 is assumbed to be False. In many languages it is possible to play tricks with this assumption by doing some simple math tricks. Two numbers are equal if you subtract them out and it becomes zero. SO if they are zero, the value can be resolved as a boolean expression, testing for “False.”
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment