Regularity

From Maze

(Difference between revisions)
Jump to: navigation, search
(→Iteration)
(→GOTO and the Unbounded Loop)
Line 43:Line 43:
10 PRINT works as it does because of its repetition of the PRINT statement, repetition that is unconditional: It will continue until something outside the program interrupts it.
10 PRINT works as it does because of its repetition of the PRINT statement, repetition that is unconditional: It will continue until something outside the program interrupts it.
−
Programming languages contain two basic types of loops: bounded (also called "finite") and unbounded (or "infinite"). 10 PRINT has the latter kind. If one is writing a program that is intended to exit and to produce some result, an unbounded loop represents a mistake on the programmer's part. The unbounded loop can be used to keep an application (usually an interactive one) running until the system is shut off or rebooted, though, even on a single-threaded system such as the Commodore 64.
+
At a high level, programs can contain two types of loops: bounded (also called "finite") and unbounded (or "infinite"). 10 PRINT has the latter kind, an infinite loop. If one is writing a program that is intended to exit and to produce some result, an unbounded loop represents a mistake on the programmer's part. The unbounded loop does have legitimate uses, however. It can be used to keep an application, usually an interactive one, running and accepting input until the system is shut off or rebooted. This can be done even on a single-threaded system such as the Commodore 64.
−
Bounded loops are those that end under certain conditions. The exact conditions vary; some will continue until the program reaches a pre-defined exit state while others execute a specific number of times no matter what the result. No matter what the exit conditions are for a specific loop, the fact that there are exit conditions at all implies the programmer is expecting some kind of change in the program to be introduced as a result of the loop's execution.
+
Bounded loops are those that end under certain conditions. The exact conditions vary; some will continue until the program reaches a pre-defined exit state while others execute a specific number of times no matter what the result. No matter what the exit conditions are for a specific loop, the fact that there are exit conditions at all implies the programmer is expecting some kind of change in the program to be introduced as the program executes.
A common use of finite loops is to create an iterative process. Iteration is a special type of loop where the result of one pass through the loop influences the result of succeeding passes. The simplest example of an iterative process is nothing more than counting. Beginning with an initial value, 1, and adding 1 to it to produces 2. Taking the output from that operation, 2, and performing the same operation again, adding 1, results in 3, and so on until the sequence 1, 2, 3… emerges. Counting to 10, 100, or for one hour no matter what number you end up at are all finite iterative processes; counting until you pass out is an infinite iterative process, even if you never quite make it to infinity.
A common use of finite loops is to create an iterative process. Iteration is a special type of loop where the result of one pass through the loop influences the result of succeeding passes. The simplest example of an iterative process is nothing more than counting. Beginning with an initial value, 1, and adding 1 to it to produces 2. Taking the output from that operation, 2, and performing the same operation again, adding 1, results in 3, and so on until the sequence 1, 2, 3… emerges. Counting to 10, 100, or for one hour no matter what number you end up at are all finite iterative processes; counting until you pass out is an infinite iterative process, even if you never quite make it to infinity.

Revision as of 16:41, 18 July 2011

View this revision of Regularity.

Personal tools