![]() |
In C++, a loop is a part of code that is executed repetitively till 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++, its types and causes and what are its applications. Infinite Loop in C++An infinity loop is any loop in which the loop condition is always true leading to the given block of code being executed repeatedly infinite number of times. They can also be called the endless or non-terminating loop which will run till the programs life. Infinite loops are generally accidental that occurs due to some mistake by the programmer. But they are pretty useful too in different kind of applications such as creating a program that does not terminate till the 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.
1. Infinite Loop using While LoopIt is the most popular type of while loop due to its simplicity. We just pass the value that will result in true as the condition of the while loop. Syntax while(1) Example
This is an infinite loop. 2. Infinite Loop using For LoopIn for loop, if we remove the initialization, comparison and updation condition, then it will result in infinite loop. Syntax for(;;) Example
This is an infinite loop. 3. Infinite Loop using do-while LoopJust like other two loops, we can also create an infinite loop using do while loop. Although, this loop is not preferred much due to longer syntax. Syntax do{ Example
This is an infinite loop. Common Causes of Accidental Infinite Loops in C++Infinite loops can be both intentional and accidental. Accidental loops are those which was not intended by the programmer but are caused due to some error in the program. Following are some of the causes due to which users may face infinite loops in their programs unintentionally: 1. Missing Update StatementsInfinite loops are caused when a user forgets to add an update condition inside the loop which will terminate the loop in the future. The following program illustrates such a scenario:
1 2. Incorrect Loop ConditionsThe conditions mentioned inside the loop body is very crucial to terminate a loop. Incorrect loop condition can result into an infinite loop. The following program illustrates such a scenario:
Hello Geeks 3. Logical Erros in the LoopIn many scenarios infinite loops are caused due to small logical errors in the code. The following program illustrates such a scenario:
This is an infinite loop Applications of Infinite Loops in C++Infinite loops does not only occur by accident, they are also created by the programmer for different purposes. Following are some of the common applications where the infinite loops are used intentionally:
Using Infinite Loops to Take User Input in C++Infinite loops are commonly used in scenarios where a program need 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:
Enter a command (type 'exit' to quit): Hello Time Complexity: O(1) Related ArticlesYou can read the following articles if you want to improve your understanding about the loops in C++: |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |