Beginner's Guide:
"Expressions, Statements and Boolean logic" |
One of the most important things in programming is getting things straight. No matter what you want the computer to do for you, in order to be able to tell it what to do, you first have to be pretty clear yourself about what
you want it to do. This can be hard; people are quite used to handling
unspecific information, but most computer languages are not capable of
handling unspecific information, and if you want it to be able to handle
unspecific information you'll find that you'll have to write some very specific
routines to allow it to do so.
One major principle in the study of Artificial Intelligence is to study and analyse human thinking.
Another is finding a good way to implement the results of this study and analysis in a computer program. This
iswhat a ‘normal’ programmer does, too – the only difference is a slight shift of focus from the task at hand, e.g. finding an algorithm for efficiently mowing the lawn, to how a human being solves a task at hand, again, e.g. efficiently mowing the lawn. The good thing about studying the latter is that
| a) |
You might find a new algorithm by studying how a human approaches a problem (by extent, you can also study plants and animals, as happens a lot in medicine too) |
| b) |
You might find a general algorithm which helps you to find algorithms for specific situations, even if they aren’t always the most efficient – flexibility is also worth quite a lot. |
One way of dealing with the “how” in abstract terms, is by breaking up all the bits of information into little chunks that can be manipulated by simple actions.
For an example, let's look at the if .. then .. else ...
endif structure in the little 'guess my number' example program
if A = B then
print right$
else
print wrong$
endif |
'If A = B then' is a bit of logic. A and B are the little chunks that hold information (variables), and
if, equal (=) and then are statements of logic. These statements should make perfect sense to you, because we use that kind of logic every day in our language, albeit in a slightly less abstract form. You might ask me what I plan on doing tomorrow. I could answer “well,
if the sun shines, (then) I'll go to the beach, and otherwise (else)
I'll stay in and try to write a computer program that can influence the weather.” (Hmm, I wonder if there’s a movie in that … )
As you can see, the structure of my hypothetical (after all, it is winter and I
can't write programs that influence the weather - at best I can write something
that influences the weather report ;-) ) reply is pretty much the same as what we find in the program: if A (guessed number is the same as the random number, or the sun shines), then do this (go to the beach, or print right$), else do that (stay in, or print wrong$).
Another way of looking at this structure is according to
Boolean logic, derived from Boolean algebra. To grossly
summarize the concept of Boolean Logic, it basically operates on the basis of whether something is
true or false.
Let's look again at C = 1 + B. We have seen it being used as an assignment before, but we can also read it in a different way. For instance, if C points to the value of 2 and B points to the value of 1, prior to the point at which we look at the line 'C = 1 + B', then we can say that the expression C = 1 + B is True. For, C is 2, B + 1 is 2, and so they point to the same values, 2 and 2. The expression is then the same as 2 = 2. Since 2 is indeed the same as 2, the expression evaluates to 'True'.
If we look at the line starting with “If”, then a slightly more abstract version of that line would be the following:
if expression is true
Then
“Do something”
else
“Do something else”
endif |
Now, in the program, if A is 5 (the random number picked by the computer), and B is also 5 (the number guessed by the user), then the
expression A = B would evaluate to true, and whatever code
has been written at the position of “Do something” will be used by the
computer program. Otherwise, e.g. if A is 5, but the user guessed 3, then the expression would be “5 =
3”; since 5 isn't 3, the expression is false (= not true), and so the
computer program uses the part after the “else” statement.
The commands in the program we've used above, if, then, else and
endif, are also called Control of Flow Commands, because they determine in which order which commands of your program are going to be executed by the computer. We will discuss them in more detail in the next section.
(c) 2001 Arwin van Arum
Sony and Playstation 2 are registered trademarks owned by Sony Inc.
|