WhatIs.com

infinite loop (endless loop)

By Ben Lutkevich

What is an infinite loop?

An infinite loop -- sometimes called an endless loop -- is a piece of code that lacks a functional exit so that it repeats indefinitely. In computer programming, a loop is a sequence of instructions that is continually repeated until a certain condition is reached.

Infinite loops can be used intentionally or can occur as the result of a programming error or a bug. A pseudo-infinite loop is one that looks as if it will be infinite but stops at some point.

The term infinite loop is sometimes used to describe an endless iteration situation in DevOps feedback loops and software development processes.

What is a loop in programming?

A loop in computer programming is created when a sequence of instructions repeats until a certain terminating condition is reached. Typically, a defined process is completed -- such as getting an item of data and changing it -- and then a condition is checked, such as whether a counter has reached a prescribed number. Once the prescribed number is reached the loop ends.

Loops are used to execute the same code multiple times. For example, code that checks the attendance status of each student in a classroom could return a Y for present and an N for absent. The program would need to run through the class roster of 26 students and do this for each member of the class. The program's stopping condition would occur after 26 students had been checked.

There are different types of loops, and different programming languages have different loop syntaxes. Some types of loops include the following:

No matter the loop type or language syntax, most loops have an exit condition that tells the loop when to end.

How do infinite loops work?

Infinite loops continue as long as the exit condition doesn't occur. A loop without an exit condition happens when one of the following conditions exists:

In the attendance checking program example, the 26 students are listed in alphabetical order and labeled numerically, with the first student on the list being number one and the last student being number 26. In this case, the loop would normally be set to terminate after the loop runs 26 times. However, the following criteria could turn it into an infinite loop:

In all cases, if the presence of the specified condition cannot be ascertained, the next instruction in the sequence tells the program to return to the first instruction and repeat the sequence. This typically continues until the program terminates automatically after a certain amount of time, or the operating system (OS) terminates the program with an error.

Why are infinite loops used?

An infinite loop often results from a programming error. For example, the conditions for exit are incorrectly written.

However, there are instances when an infinite loop is used intentionally, enabling a program to run continuously. Some examples include the following:

The following joke captures the spirit of infinite loops such as malware:

The programmer, thinking like a computer, presumably lathered and rinsed repeatedly until they died of exhaustion, starvation or drowned.

Infinite loop examples

The following is an example of infinite loop code in Python:

i=1
while i <= 7
	print ("still looping")

In this loop, the program checks the value of i, then says that if i is less than or equal to 7, the program will print the phrase "still looping." The exit condition is if i is greater than 7. Since there is nothing changing the value of i, this exit condition cannot be fulfilled. The program will print the phrase "still looping" indefinitely until the computer is shut off or crashes.

Infinite loops can appear in a variety of computing contexts and are used in a variety of programming languages. In some contexts, loop conditions can cause a cybersecurity breach. Cybersecurity professionals should learn these 5 essential programming languages to be able to stop these threats and others effectively.

07 Feb 2023

All Rights Reserved, Copyright 1999 - 2024, TechTarget | Read our Privacy Statement