Infinite loops in C language
An infinite loop is a sequence of instructions or statements in a computer program executing endlessly either due to the loop having no terminating condition, a specified condition never be met or one that causes the loop to start over.

Infinite for loop

A for loop can be an infinite either due to condition statement or increment statement. It can be also possible that the condition parameter or increment statement again gets modified in the loop body. etc..
Infinite for loop
 1 // condition never met
 2 for(i = 0; i < -1; i++) {
 3 
 4 } 
 5 
 6 // missing or incorrect increment / decrement statement
 7 for(i = 0; i < 5; ) {
 8 
 9 }
 10 
 11 // modified in loop
 12 for(i = 0; i < 5; i++) {
 13    // modified in loop body
 14    i--;
 15 }

Infinite while loop

A while loop can be an infinite either due to incorrect condition which never met or conditional statements variable modified in the body of while loop. etc..
Infinite while loop
 1 // condition never met
 2 int i = 0;
 3 while(i < -1) {
 4    i++;
 5 }
 6 
 7 // defining infinite loop without termination
 8 while(1) {
 9    // never ending loop
 10 }
 11 
 12 // condition variable modified in loop body
 13 int i = 0;
 14 while(i < 5) {
 15    i++;
 16    // statements
 17    i = 1; 
 18 }
 19 
 20 // incorrect placement of continue statement
 21 int i = 0;
 22 while(i < 5) {
 23    // statements
 24    continue;
 25    i++;
 26 }

Infinite do.. while loop

Similar to while loop, a do.. while loop can be infinite due to incorrect condition which never met or conditional statements variable modified in the body of while loop. etc..
Infinite do.. while loop
 1 // condition never met
 2 int i = 0;
 3 do{
 4    i++;
 5 } while(i < -1) ;
 6 
 7 // defining infinite loop without termination
 8 do {
 9    // never ending loop
 10 } while(1);
 11 
 12 // condition variable modified in loop body
 13 int i = 0;
 14 do {
 15    i++;
 16    // statements
 17    i = 1; 
 18 } while(i < 5);
 19 
 20 // incorrect placement of continue statement
 21 int i = 0;
 22 do {
 23    // statements
 24    continue;
 25    i++;
 26 } while(i < 5);
Note : There are so many root cause where looping statement can be infinite which depends on condition and condition variable or incorrect order of statements.
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us