C++ Infinite Loop

C++ Infinite Loop: This loop can also be called as “endless loop”. This loop in C++ can be defined as a piece of code which lacks the exit such that it repeats repeatedly. This loop occurs when it is always executing true. In C++, if you pass true in the do-while loop it will be known as “infinitive do-while loop”. Even if you are having an initialization and increment expression but in C++ we use the ‘for(; ;)’ construct to signify the infinite loop.

C++ Infinite Loop

In case if you want we can terminate this loop by pressing the Ctrl + C keys.

Example:

#include <iostream>
int main(){
   using namespace std;
      for( ; ; ){
      cout<<"This loop will never end"<<endl;
    }
  return 0;
}

Output:



This loop will never end
This loop will never end
This loop will never end
This loop will never end
This loop will never end
This loop will never end
This loop will never end
This loop will never end
This loop will never end
This loop will never end

Example 2:

#include <iostream>  
using namespace std;  
int main() {  
  do{    
       cout<<"Infinitive do-while Loop";    
     } while(true);     
}

Output:

Output Limit Exceeded