Chapter 4: Working with Loops


Overview

As you may have gathered by now, computers are not very bright. Let’s shout that out loud and say it from the rooftops: “COMPUTERS ARE NOT VERY BRIGHT!”

What do I mean by the statement that computers are not very bright considering that they do all kinds of difficult things? (There, I get to say “Computers are not very bright” again!)

The fact of the matter is that if you tell a computer what to do within the parameters of the kinds of things it can do, and if your instructions are given in the right language and are sufficiently precise, it’ll probably be able to do it. Put this way, it’s clear that computers by themselves, and innately, don’t have the ability to take initiative, behave creatively, exercise intuition—and do the myriad things that make up human intelligence. However, and it’s a big “however,” computer software can be written that allows computers to at least mimic these kinds of characteristics (and perhaps do more than merely mimic them) and to do many things better than people do. Perhaps, after reading this book, you’ll start on your journey toward writing this kind of intelligent software.

So, if computers are so bloody stupid, how come they can do so many wonderful things? An important part of the answer is that computers will perform repetitive tasks without complaint. Computers will and can do this repetition a great many times.

Thomas Alva Edison famously called genius 1 percent inspiration and 99 percent perspiration. Computers are very good at the perspiration part. They will keep doing one simple thing—this is called iterating —until they’re told to stop or they run out of computing resources.

The key to a great many computer programs is iteration (the process of iterating). The actual step performed can be quite simple. For example, a loop might compare two variables to see when they’re of equal value (this is the iteration). When they’re equal, code within the loop might do something. Then the loop might continue with an increment of one of the two variables after each comparison. But the net effect of the iterated simple action can be to achieve a quite sophisticated result.

From within a computer program, iteration usually looks like a loop (this is where I got the title for the chapter, “ Working with Loops.”) Loops are used to control execution flow by repeatedly executing a statement or a number of statements. You go back to the beginning—or loop—and do it again until a condition is met, usually with a small change to one of the variables controlling the condition with each increment.

Once again, this will be easier to understand when you see it in practice than in the abstract. This chapter starts by explaining the mechanics of the statements used to loop. In JavaScript, these statements are for, while, and do/while. (Although the specifics may vary in other computer languages, the concepts are essentially the same.)

With the mechanics down, I move on to show you how to use nested looping—placing one loop within another. (The example will use for loops inside other for loops, but you could achieve the same results with while loops.)

We won’t do too much with nested looping (I show you how to draw a Christmas tree on screen). But this introduction should stand you in good stead when you have to program with complex looping logic.

If this sounds like great fun, it is! So let’s get started with some preliminaries so we can move on to the fun stuff.




Learn How to Program Using Any Web Browser
Learn How to Program Using Any Web Browser
ISBN: 1590591135
EAN: 2147483647
Year: 2006
Pages: 115
Authors: Harold Davis

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net