Home / Questions / What is an infinite loop?
Explanatory Question

What is an infinite loop?

👁 789 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

A loop running continuously for an indefinite number of times is called the infinite loop.

An infinite loop in C language is a loop that continues to execute indefinitely, without any exit condition. It is created by using a logical condition that is always true, such as "1==1" or "i > 0". For example, the following code snippet creates an infinite loop:

Infinite For Loop:


for(;;){  
//code to be executed  
}  

It can also be created by ommiting the exit condition of the loop.


for(int i=0; ; i++){
   // code that runs indefinitely
}

Infinite while Loop:


while(1){  
//code to be executed  
}  

Infinite Do-While Loop:


do{  
//code to be executed  
}while(1);  

It is important to be careful when using infinite loops, as they can cause the program to crash or freeze if not designed properly.