Time Server
Post by
Author Syukra

Published on Dec 16, 2024

Last updated on Apr 11, 2025

Estimated reading time: 5 minute

Infinite Loop in Programming

Infinite Loop

In programming, an infinite loop is a construct in which a particular block of code is executed repeatedly without stopping. Often, the exit condition for the loop is never met, causing the loop to continue for the duration of the program. Although seemingly simple at first glance, infinite loops can cause a variety of problems in software.

How Are Infinite Loops Formed?

Infinite loops can be formed in several ways, most notably in commonly used loop structures such as for, while, or do-while. Let’s look at the following simple example:

  1. for loop:
for i in range(10):
print("Hello, World!")

In the example above, although we did not explicitly write an infinite loop, if we change the condition limit in range(10) to never end, for example range(0, 10, -1), the loop will become infinite if there is no change in the logic for the stop condition.

  1. while loop:
i = 0
while i < 10:
print("Hello, World!")

If the condition i < 10 is not modified inside the loop (e.g., there is no i += 1), this loop will be an infinite loop.

  1. do-while loop (Python doesn’t have this loop directly, but you can emulate it):
i = 0
while True:
print("Hello, World!")
i += 1
if i >= 10:
break

If the condition if i >= 10 is missing, this loop will continue to repeat without stopping.

Common Causes of Infinite Loops

  1. Incorrect Condition: The condition to exit the loop may be incorrect or never reached.
  2. Unupdated Variable: The variable used in the loop condition is not updated correctly.
  3. Incorrect Logic: Logical errors in loop variable calculations or updates.

Avoiding Infinite Loops

  1. Check Loop Condition: Make sure the loop condition can reach a stopping state.
  2. Update Variables Correctly: Make sure the variables used in the loop condition are updated at each iteration.
  3. Use a Debugger: Debugging tools can help trace loop execution and variable conditions.
  4. Write Unit Tests: Test the loop with various conditions to ensure there are no undetected infinite loops.

Impact of Infinite Loops

  1. Excessive Resource Usage: Loops that do not terminate will continue to use CPU and memory, which can affect system performance.
  2. Hanging Program: The program may stop responding if the loop cannot be completed.
  3. Data Loss: In applications that process data, infinite loops can cause data loss or unexpected results.

Infinite Loop Examples in Other Languages

Here are examples of infinite loops in several different programming languages:

1. Python

while True:
print("This is an infinite loop in Python")

Here, while True will always evaluate to True, so the loop never terminates.

2. JavaScript

while (true) {
console.log("This is an infinite loop in JavaScript");
}

Like in Python, while (true) ensures that the loop will continue to run forever.

3. Java

public class InfiniteLoopExample {
public static void main(String[] args) {
while (true) {
System.out.println("This is an infinite loop in Java");
}
}
}

By using while (true), this loop will continue to repeat without stopping.

4. C++

#include <iostream>

int main() {
while (true) {
std::cout << "This is an infinite loop in C++" << std::endl;
}
return 0;
}

Similar to the other examples, while (true) in C++ ensures the loop never ends.

5. C#

using System;

class Program {
static void Main() {
while (true) {
Console.WriteLine("This is an infinite loop in C#");
}
}
}

while (true) in C# will also make the loop run continuously.

6. Ruby

loop do
puts "This is an infinite loop in Ruby"
end

loop do is Ruby’s way of making a loop that never ends.

7. PHP

<?php
while (true) {
echo "This is an infinite loop in PHP";
}
?>

Using while (true) in PHP will also cause the loop to run endlessly.

8. Swift

while true {
print("This is an infinite loop in Swift")
}

while true in Swift works the same as any other infinite loop.

Infinite Loop Example In Pseudocode

Pseudocode is a way of writing algorithms that are not tied to the syntax of a particular programming language, making it easy to understand in general.

1. Using True Conditions

WHILE True
PRINT "This is an infinite loop in pseudocode"
END WHILE

Here, WHILE True is a condition that is always true, so the loop will continue to run without stopping.

2. Using Conditions That Are Never Reached

SET counter TO 0
WHILE counter < 10
PRINT "This is an infinite loop in pseudocode"
// There is no command to change the counter value
END WHILE

In this example, even though the loop condition (counter < 10) should be reached and cause the loop to stop, because the value of counter is never changed in the loop, this condition will always be true and the loop will be infinite.

3. Loops with Input-Based Conditions

WHILE user_input IS NOT "exit"
PRINT "Enter command (type 'exit' to exit):"
READ user_input
END WHILE

In this example, if user_input is never equal to “exit”, the loop will continue. To make it an infinite loop, the condition to change user_input to “exit” will never occur.

4. Loops with Always True Conditions

REPEAT
PRINT "This is an infinite loop with REPEAT in pseudocode"
UNTIL False

Here, a loop with REPEAT and the condition UNTIL False will always repeat the contents of the loop because the condition used is False, which is never met.

5. Loops with Ineffective Variable Changes

SET i TO 0
WHILE i < 10
PRINT "This is an infinite loop in pseudocode"
SET i TO i - 1 // Ineffective changes
END WHILE

In this example, the value of i is decremented each iteration, but this will cause the value of i to become negative and the condition i < 10 to remain true, so the loop never terminates.

That’s all the articles from Admin, hopefully useful… Thank you for stopping by…

Tag: #Programming
Share Article

Follow My Social Media