Filling out a form to request a loop - ENTERING THE SECOND REALM - JavaScript in Plain Language (2015)

JavaScript in Plain Language (2015)

PART II: ENTERING THE SECOND REALM

2.5 Filling out a form to request a loop

A loop is a subroutine that repeats a sequence of programming instructions over and over until it gets a halt command.

We have seen how to create a function to store code that runs every time we call the function.

We can also store code in a loop that repeats itself for a determined number of times. Then if we want to reuse the loop at will, we can insert the loop into a function and call the function later at any time to run the loop without having to re-write another loop script.

· Beware of infinite loops.

Always make sure you design a way to stop the loop. That should be your first decision when creating a loop: when and how should this loop end? JavaScript will only end it when it runs out of memory. We must instruct JavaScript to end it much earlier than that!

· If you ever run into trouble while testing, click on the universal key sequence CTRL c to stop the program. Most of the times it will stop it.

How long do we want our loop to repeat itself? Just once, 5 times, 100 times, or until a certain condition happens like for example when the value of a certain variable becomes something other than the original value.

All this sounds so abstract, right? Let’s remove the abstractness out of the equation.

What no one tells you about loops

We don’t really create a loop ourselves. This mechanism already exists in the JavaScript library located in your browser and it exists in several different styles (for loops, while loops, do while loops, etc.).

What we have to do is to fill in a request form for a certain loop style, and then provide instructions to JavaScript for what we want it to accomplish while the loop runs.

Yes, there are several styles of loops in the JavaScript library. They all loop around but how they loop can make a difference in your choice of loop style selection.

One common style is the for loop. The for loop repeats itself for a determinate number of times.

Then we have the while loop. A while loop repeats itself while something remains true. If that something is always true, the while loop will run forever. Sometimes we want a loop to run forever, like when the computer scans the keyboard waiting for a human to press a key. If the human does not press a key any time soon, the while loop will keep scanning. Don’t worry about memory because this type of scanning is not using much memory, it is just looking around like a night watchman.

JavaScript also has in its library a loop style known as do while. A do while loop is slightly different than the regular while. The do part makes it run once whether the condition is true or false.

Why does a do while loop run at least once? Because the while condition part of the loop only shows up at the end of the script. So JavaScript will run the loop and then at the end checks the condition to see if it should run a second time. It is like saying “shoot first and ask questions later”.

· Remember: in order to use a loop we must fill in a loop style application.

We will cover all these styles of loops in due time. For now let’s talk about the for loop.

Don’t burn your brains out trying to memorize something you don’t yet need. This was just a brief introduction. Now let’s get our hands dirty by creating loop projects.