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.
· 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
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.
· 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.