Browse Definitions :

infinite loop (endless loop)

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.

Logic diagram of a while loop.
A while loop continues running until the specified condition -- in this case i ≤ 7 -- is no longer satisfied. A false response will cause the loop to end.

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:

  • A for loop repeats a block of code a known number of times. It iterates through a list and runs code once for each item in the list.
  • A while loop will continue to repeat as long as a specified condition is met. It checks for the condition before running the loop code.
  • A do loop runs code, then checks for the condition afterward.
  • A do-while loop is like a while loop but puts the condition at the end, so that the loop runs at least one time if the condition is not met. A while loop will not run if the condition is not met.

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:

  • The loop has no terminating condition.
  • The loop has a terminating condition that cannot be met.
  • The loop has a condition that instructs it to start over.

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:

  • No terminating condition is set, and the loop returns to student one and checks through the entire list over and over again.
  • The terminating condition is set to a number that does not fall into the list of 26 students, such as -1 or 316.
  • The loop contains instructions to return to student one sometime before 26 is reached or a break statement is written that causes an intentional interruption in the 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:

  • Embedded systems. A cartridge-based video game is an embedded system that uses an infinite loop. The game runs an infinite loop until the console is powered off because there is no underlying OS to return to.
  • Operating systems. OSes are infinite loops that continually check for user input and respond accordingly. The loop continues until the device is turned off or reset.
  • Web servers. Web servers are designed to take a request, return a webpage and then wait indefinitely for the next request.
  • Viruses. Malware can use infinite loops to overwhelm a computer's processing power and cause it to crash. This technique is used to carry out a distributed denial-of-service attack, which occupies a computer or network's resources so that legitimate users can't access them.

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

  • Q: Why did the programmer die in the shower?
  • A: Because the bottle said "lather, rinse, repeat."

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:

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.

This was last updated in February 2023

Continue Reading About infinite loop (endless loop)

  • voice over LTE (VoLTE)

    Voice over LTE (VoLTE) is a digital packet technology that uses 4G LTE networks to route voice traffic and transmit data.

  • ONOS (Open Network Operating System)

    Open Network Operating System (ONOS) is an OS designed to help network service providers build carrier-grade software-defined ...

  • telematics

    Telematics is a term that combines the words telecommunications and informatics to describe the use of communications and IT to ...

  • three-factor authentication (3FA)

    Three-factor authentication (3FA) is the use of identity-confirming credentials from three separate categories of authentication ...

  • cyber espionage

    Cyber espionage (cyberespionage) is a type of cyber attack that malicious hackers carry out against a business or government ...

  • role-based access control (RBAC)

    Role-based access control (RBAC) is a method of restricting network access based on the roles of individual users within an ...

  • project charter

    A project charter is a formal short document that states a project exists and provides project managers with written authority to...

  • leadership

    Leadership is the ability of an individual or a group of people to influence and guide followers or members of an organization, ...

  • transaction

    In computing, a transaction is a set of related tasks treated as a single action.

  • employee engagement

    Employee engagement is the emotional and professional connection an employee feels toward their organization, colleagues and work.

  • talent pool

    A talent pool is a database of job candidates who have the potential to meet an organization's immediate and long-term needs.

  • diversity, equity and inclusion (DEI)

    Diversity, equity and inclusion is a term used to describe policies and programs that promote the representation and ...

Customer Experience
  • sales development representative (SDR)

    A sales development representative (SDR) is an individual who focuses on prospecting, moving and qualifying leads through the ...

  • service level indicator

    A service level indicator (SLI) is a metric that indicates what measure of performance a customer is receiving at a given time.

  • customer data platform (CDP)

    A customer data platform (CDP) is a type of software application that provides a unified platform of customer information that ...