An endless cycle in programming is a cycle written in such a way that the condition for exiting it is never satisfied. A program that has entered an infinite loop is sometimes said to be looped [1] .
The idea of an infinite loop plays an important role in the concept of the turing completeness of programming languages: on the one hand, any cycle can be represented as an infinite loop, in the body of which there is a check for the exit condition and the exit command from the cycle, on the other hand, any program can be written with help:
- endless cycles
- exit loop commands
- branching operators (
if-then), - sequences of commands executed one after another.
Examples
Infinite loop in Pascal :
//Option 1
while true do
begin
{do something}
if {exit condition from an infinite loop} then break
end ;
// Option 2
repeat
{do something}
until false ;
For C-like languages:
//option 1
for (;;) {
/ * do something * /
}
// option 2
while ( true ) {
/ * do something * /
}
The language of Hell (as well as a number of its descendants) have a special construction that describes an infinite loop:
loop
- do something
end loop ;
In addition, Ada allows you to exit immediately from several nested loops, and also has a conditional form of the exit statement, which allows you to avoid using the branch operator:
Out_Cycle :
loop
...
loop
...
exit Out_Cycle when Logic_Exp ;
- is equivalent
if Logic_Exp then
exit Out_Cycle ;
end if ;
...
end loop ;
...
end loop Out_Cycle ;
--from here the program execution will continue
--after executing the instruction exit Out_Cycle;
Practice
Programs from which there is no way out (for example, operating systems , microcontroller firmware ) usually represent an endless loop.
In writing programs that solve real user problems, endless loops, as a rule, are one of the sources of unstable program operation. Meanwhile, when writing algorithmic programs, that is, programs that solve certain problems of applied computer science and are not directly related to practical (or rather theoretical) tasks, the use of infinite loops is a very good professional technique.
So, for example, when solving problems at olympiads in computer science (programming) of various levels, the main task of the participant is to write programs that solve the proposed algorithmic problems in the allotted time. As a rule, such tasks are solved using cycles. Obviously, the participant does not have enough time to think about the conditions for exiting the cycle (which should be indicated in the so-called while cycle). Therefore, a very useful trick is the use of modified infinite loops.
This technique is based on the fact that every modern programming language offers a number of operators that allow you to interrupt the execution of the loop body not after the next iteration, but during the next execution (for example, Break in Delphi , EXIT FOR in BASIC , etc.). To save time, the participant writes an infinite while with the True condition ( while True do ... ), and then, as necessary, writes condition checking statements in the loop body that interrupt the loop execution with Break-like statements if necessary.
Sometimes (for example, in scripts for controlling the characters of computer games ), exiting a program is an interpreter stop. So the developer should not explicitly write down the exit condition - which means that the program turns into an endless loop. This principle is adopted, for example, in Game Maker , in some games for programmers .
Notes
- ↑ The use of this verb went far beyond programming, and it is often applied to subjects in the sense of characterizing incorrect behavior.