Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Error’d: You Talkin’ to Me?

      September 20, 2025

      The Psychology Of Trust In AI: A Guide To Measuring And Designing For User Confidence

      September 20, 2025

      This week in AI updates: OpenAI Codex updates, Claude integration in Xcode 26, and more (September 19, 2025)

      September 20, 2025

      Report: The major factors driving employee disengagement in 2025

      September 20, 2025

      Development Release: Zorin OS 18 Beta

      September 19, 2025

      Distribution Release: IPFire 2.29 Core 197

      September 19, 2025

      Development Release: Ubuntu 25.10 Beta

      September 18, 2025

      Development Release: Linux Mint 7 Beta “LMDE”

      September 18, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Student Performance Prediction System using Python Machine Learning (ML)

      September 21, 2025
      Recent

      Student Performance Prediction System using Python Machine Learning (ML)

      September 21, 2025

      The attack on the npm ecosystem continues

      September 20, 2025

      Feature Highlight

      September 20, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Hyprland Made Easy: Preconfigured Beautiful Distros

      September 20, 2025
      Recent

      Hyprland Made Easy: Preconfigured Beautiful Distros

      September 20, 2025

      Development Release: Zorin OS 18 Beta

      September 19, 2025

      Distribution Release: IPFire 2.29 Core 197

      September 19, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»How Infinite Loops Work in C++

    How Infinite Loops Work in C++

    August 2, 2025

    In C++, a loop is a part of code that is executed repetitively until the given condition is satisfied. An infinite loop is a loop that runs indefinitely, without any condition to exit the loop.

    In this article, we will learn about infinite loops in C++, their types and causes, and their applications.

    Here’s what we’ll cover:

    1. What is an Infinite Loop in C++?

    2. Types of Infinite Loops in C++

    3. Common Causes of Accidental Infinite Loops in C++

    4. Applications of Infinite Loops in C++

    5. Using Infinite Loops To Take User Input in C++

    6. Conclusion

    What is an Infinite Loop in C++?

    An infinite loop is any loop in which the loop condition is always true, leading to the given block of code being executed an infinite number of times. They can also be called endless or non-terminating loops, which will run until the end of the program’s life.

    Infinite loops are generally accidental and occur due to some mistake by the programmer. But they are pretty useful, too, in different kinds of applications, such as creating a program that does not terminate until a specific command is given.

    Types of Infinite Loops in C++

    There are several ways to create an infinite loop in C++, using different loop constructs such as while, for, and do-while loops. Here, we will explore each method and provide examples.

    • Infinite While Loops

    • Infinite For Loops

    • Infinite do-while Loops

    1. Infinite Loop using While Loop

    This is the most popular type of while loop due to its simplicity. You just pass the value that will result in true as the condition of the while loop.

    Syntax:

    while(1)
        or
    while(true)
    

    Example Code:

    <span class="hljs-comment">// Example of Infinite loop in C++ using for loop</span>
    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
    <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
    
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">// Infinite loop using while</span>
        <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
            <span class="hljs-built_in">cout</span> << <span class="hljs-string">"This is an infinite loop."</span> << <span class="hljs-built_in">endl</span>;
        }
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
    }
    

    Output:

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    ………………….

    Infinite Loop using For Loop

    In a for loop, if we remove the initialization, comparison, and update conditions, then it will result in an infinite loop.

    Syntax:

    for(;;)
    

    Example Code:

    <span class="hljs-comment">//Example of Infinite loop in C++ using for loop</span>
    
    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
    <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
    
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">// Infinite loop using for loop</span>
        <span class="hljs-keyword">for</span> (;;) {
            <span class="hljs-built_in">cout</span> << <span class="hljs-string">"This is an infinite loop."</span> << <span class="hljs-built_in">endl</span>;
        }
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
    }
    

    Output:

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    ……………………….

    Infinite Loop using do-while Loop

    Just like the other two loops discussed above, we can also create an infinite loop using a do-while loop. Although this loop is not preferred much due to its longer syntax.

    Syntax:

    do{
    }while(1)
    

    Example Code:

    <span class="hljs-comment">// Infinite loop in C++ using do-while loop</span>
    
    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
    <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
    
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
       <span class="hljs-comment">// infinite do-while loop</span>
        <span class="hljs-keyword">do</span> {
            <span class="hljs-built_in">cout</span> << <span class="hljs-string">"This is an infinite loop."</span> << <span class="hljs-built_in">endl</span>;
        } <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>);
    
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
    }
    

    Output:

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    ……………………….

    Common Causes of Accidental Infinite Loops in C++

    Infinite loops can be both intentional and accidental. Accidental infinite loops are those which were not intended by the programmer but are caused due to some error in the program.

    Following are some of the errors that may cause infinite loops in your programs unintentionally:

    1. Missing Update Statements

    Infinite loops are caused when you forget to add an update condition inside the loop, which will terminate the loop in the future. The following program illustrates such a scenario:

    Example Code:

    <span class="hljs-comment">// Infinite loop caused due to missing update statement</span>
    
    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
    <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
    
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>;
        <span class="hljs-keyword">while</span> (i < <span class="hljs-number">5</span>) {
            <span class="hljs-built_in">cout</span> << i <<<span class="hljs-built_in">endl</span>;
            <span class="hljs-comment">// Missing update: i++;</span>
        }
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
    }
    

    Output:

    3

    3

    3

    3

    3

    3

    3

    ……………………

    To fix the above code, we can add an update condition inside the loop like this:

    <span class="hljs-comment">// fixed code</span>
    
    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span>
    <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span> ;
    
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>;
    <span class="hljs-keyword">while</span> (i < <span class="hljs-number">5</span>) {
        <span class="hljs-built_in">cout</span> << i << <span class="hljs-built_in">endl</span>;
        i++; <span class="hljs-comment">// add the condition</span>
    }
    
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span> ; 
    
    }
    

    Output:

    3

    4

    Incorrect Loop Conditions

    The conditions mentioned inside the loop body are crucial to terminate a loop. An incorrect loop condition can result in an infinite loop. The following program illustrates such a scenario:

    Example Code:

    <span class="hljs-comment">// Infinite loop caused due to incorrect loop conditions</span>
    
    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
    <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
    
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>;
        <span class="hljs-keyword">while</span> (i >= <span class="hljs-number">0</span>) {  
            <span class="hljs-built_in">cout</span> << <span class="hljs-string">"Hello AnshuAyush "</span> << <span class="hljs-built_in">endl</span>;
    
        }
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
    }
    

    Output:

    Hello AnshuAyush

    Hello AnshuAyush

    Hello AnshuAyush

    Hello AnshuAyush

    Hello AnshuAyush

    ……………………..

    To fix the above code, we can update i inside the loop to eventually make the condition false:

    <span class="hljs-comment">// fixed code </span>
    
    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string"><iostream></span></span>
    <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span> ;
    
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
    <span class="hljs-keyword">int</span> i = <span class="hljs-number">2</span>;
    <span class="hljs-keyword">while</span> (i >= <span class="hljs-number">0</span>) {  
        <span class="hljs-built_in">cout</span> << <span class="hljs-string">"Hello AnshuAyush"</span> << <span class="hljs-built_in">endl</span>;
        i--; <span class="hljs-comment">// loop will stop</span>
    }
    
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span> ; 
    
    }
    

    Ouptut:

    Hello AnshuAyush

    Hello AnshuAyush

    Hello AnshuAyush

    Logical Errors in the Loop

    In many scenarios, infinite loops are caused by small logical errors in the code. The following program illustrates such a scenario:

    Example Code:

    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
    <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
    
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>; i ><span class="hljs-number">2</span>; i += <span class="hljs-number">2</span>) {  
            <span class="hljs-built_in">cout</span> <<<span class="hljs-string">"This is an infinite loop"</span> << <span class="hljs-built_in">endl</span>;
        }
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
    }
    

    Output:

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    This is an infinite loop.

    ……………………….

    To fix the above code, we can either use a decreasing condition or use an incrementing loop condition.

    Decreasing condition:

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>; i > <span class="hljs-number">0</span>; i--) {
        <span class="hljs-built_in">cout</span> <<<span class="hljs-string">"This is NOT an infinite loop"</span> << <span class="hljs-built_in">endl</span>;
    }
    

    Increasing condition:

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">3</span>; i < <span class="hljs-number">10</span>; i += <span class="hljs-number">2</span>) {
        <span class="hljs-built_in">cout</span> <<<span class="hljs-string">"Loop will end when i reaches 10"</span> << <span class="hljs-built_in">endl</span>;
    }
    

    Applications of Infinite Loops in C++

    Infinite loops do not only occur by accident, as I mentioned above. You can also create them on purpose for different use cases. The following are some of the common applications where you might use infinite loops intentionally:

    • Event loops: Many Graphical User Interfaces (GUIs) use infinite loops to keep the program running and responsive to user actions.

    • Server applications: Web servers use infinite loops to continuously listen to client connections or requests.

    • Embedded systems: Embedded systems, such as microcontrollers, frequently use infinite loops as their main program loops to continuously respond to external events.

    • User inputs: Infinite loops are also used to wait for valid user inputs. The loop keeps running until a valid input is provided by the user. We’ll look at an example of this one.

    Using Infinite Loops to Take User Input in C++

    Infinite loops are commonly used in scenarios where a program needs to continuously take user input until a specific condition is met, such as exiting the program or getting a valid user input. The following program demonstrates how we can take user input from the user until a specific condition is met:

    Example Code:

    <span class="hljs-comment">// C++ Program to take user input from users using infinite loops</span>
    
    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><iostream></span></span>
    <span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string"><string></span></span>
    <span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> <span class="hljs-built_in">std</span>;
    
    <span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-built_in">string</span> input;
    
        <span class="hljs-keyword">while</span> (<span class="hljs-literal">true</span>) {
            <span class="hljs-built_in">cout</span> << <span class="hljs-string">"Enter a command (type 'exit' to quit): "</span>;
            getline(<span class="hljs-built_in">cin</span>, input);
    
            <span class="hljs-keyword">if</span> (input == <span class="hljs-string">"exit"</span>) {
            <span class="hljs-comment">// Exit the loop if the user types 'exit'</span>
                <span class="hljs-keyword">break</span>; 
            }
    
            <span class="hljs-built_in">cout</span> << <span class="hljs-string">"You entered: "</span> << input << <span class="hljs-built_in">endl</span>;
            <span class="hljs-comment">// Process the input</span>
        }
        <span class="hljs-built_in">cout</span> << <span class="hljs-string">"Program exited."</span> << <span class="hljs-built_in">endl</span>;
        <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
    }
    

    Output:

    Enter a command (type ‘exit’ to quit): Anshu

    You entered: Anshu

    Enter a command (type ‘exit’ to quit): Ayush

    You entered: Ayush

    Enter a command (type ‘exit’ to quit): exit

    Program exited.

    Conclusion

    Infinite loops aren’t always dangerous. They can be very useful when used with proper control, like break statements or condition checks. But if you use them carelessly, they can crash your program.

    So just make sure you check your loop conditions and test your code using print statements between the programs to discover any unexpected behavior. In sum, infinite loops can be very powerful when handled carefully but can be very risky if left unchecked.

    If you are beginner in C++, I’ve covered many programming topic in detail on the TutorialsPoint platform, where I regularly write about beginner-friendly programming concepts.

    📚 Other C++ tutorials you may like:

    • How to Calculate the Absolute Sum of Array Elements in C++?

    • Efficient Binary Search in C++: First and Last Index of an Element

    • TCS NQT Asked Question: Find Sum of Array Elements Between Two Indices in C++

    Source: freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleHow to Deploy a Next.js API to Production using Sevalla
    Next Article The Case for End-to-End Engineering Education: Preparing Institutions for a Dynamic Future

    Related Posts

    Development

    Student Performance Prediction System using Python Machine Learning (ML)

    September 21, 2025
    Development

    The attack on the npm ecosystem continues

    September 20, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Opera’s Android browser just got a major tab management upgrade

    News & Updates

    Microsoft Copilot scores low on AI IQ tests — but that’s not the full story

    News & Updates

    Microsoft scraps Windows 11’s simplified Taskbar system tray layout after negative feedback from testers — but I don’t understand why

    News & Updates

    Security Isn’t a Tool—It’s a Mindset: Kapil Yewale’s Vision for Resilient Cyber Systems

    Development

    Highlights

    Representative Line: Identifying the Representative

    May 19, 2025

    Kate inherited a system where Java code generates JavaScript (by good old fashioned string concatenation)…

    Russian APT29 Exploits Gmail App Passwords to Bypass 2FA in Targeted Phishing Campaign

    June 19, 2025

    SoftBank dethroned Microsoft as OpenAI’s largest investor, pushing the ChatGPT maker’s market cap to $300 billion — but reportedly buried itself in debt

    April 2, 2025

    I wasn’t interested in the Google Pixel 10, but this potential feature changes everything

    July 30, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.