|
|
|
|
|
Programming Loops in Computer CodeCondition tested, counted and endless loops to control the flow of information.In this article, we shall be discussing loops – facilities offered by a programming language to allow the programmer to instruct the computer to repeat a task.
IntroductionA loop is a programming language construction that allows the programmer to instruct the computer to perform a certain instruction, or set of instructions over and over again. There are many different types, but some are omitted from programming languages by virtue of the fact that they are variations on an existing loop type, and as such the same behaviour can be achieved without offering a specific loop type to cover it. However, we cover the three types which are common to most programming languages:
Of these, only the condition tested loop is vital; the other two just give a more convenient way of achieving the same effect. Condition Tested LoopsA condition tested loop is one which repeats a set of instructions until a certain condition is reached. The test can be performed at the start of the loop (before any of the instructions are executed), during the loop, or at the end of the loop. Usually, the condition will be testing the result of executing the statements that are inside the loop. As such, loops are contained within a looping start statement (BEGIN, REPEAT, LOOP etc.) and a closing statement, which may be the condition (UNTIL , WHILE etc.) or not (END LOOP, for example). Counted LoopsA counted loop is one which allows the programmer to instruct the computer to perform a set of instructions x times, where x is usually an integer value, but some programming languages offer other data types. One could argue that the counted loop is just a condition tested loop which updates a counter and exits once a given value is reached. For this reason, some of the lesser programming languages dispense with the counted loop altogether. The major languages, however, retain the counted loop, which has a starting statement (usually the keyword FOR), which contains the condition (as in FOR I = 1 TO 10), and a closing statement (NEXT, END FOR etc.) which sends the execution back up to the top of the loop again. Endless LoopsAn endless loop goes round and round until one of three things happens:
Perhaps the most useless endless loop is the BASIC:
This single line does absolutely nothing. It also has an equivalent in all other programming languages, and can be constructed in even the most rudimentary of them. However, some endless loops serve a purpose, in message loops, for example, where it is necessary to continually monitor for incoming messages from the operating system. Their use is not recommended in multi-tasking environments in which the processor has not control over when a program is executing. An endless loop in the Windows environment, for example, can cause system crashes, and will certainly force the application to hang, as it needs to relinquish control to Windows, from time to time, to allow system events to take place. The message : use with caution, and only then if absolutely aware of the side effects. ConclusionThere are other aspects to flow control which allow conditional execution of code (see the Condition Testing tutorial) and the possibility for the programmer to define specific blocks of code for a specific purpose, and call the block by name (see the Functions, Procedures etc. tutorial) and mastering all three types is usually a pre-requisite for being able to use a programming language effectively. Navigation LinksMailing ListStay informed - sign up to the mailing list!
The copyright of the article Programming Loops in Computer Code in Computer Programming is owned by Guy Lecky-Thompson. Permission to republish Programming Loops in Computer Code in print or online must be granted by the author in writing.
|
|
|
|