Skip to content
Related Articles

Related Articles

Improve Article
C# – Infinite Loop
  • Last Updated : 14 Oct, 2020

Infinite Loop is a loop that never terminates or ends and repeats indefinitely. Or in other words, an infinite loop is a loop in which the test condition does not evaluate to false and the loop continues forever until an external force is used to end it. You can create an infinite loop:

  • Using for loop
  • Using while loop

Example 1:

In this example, we are using a while loop to create an infinite loop by setting the condition to true. It will make while loop to never evaluates to false and the loop repeats indefinitely.

C#




// C# program to illustrate 
// infinte loop
using System;
  
class GFG{
      
static public void Main ()
{
      
    // Creating infinite loop
    // using while loop
    while (true)
    {
          
        // This statement will be printed 
        // infinite times 
        Console.WriteLine("Hey! GeeksforGeeks");
    }
}
}

Output:

Hey! GeeksforGeeks
Hey! GeeksforGeeks
Hey! GeeksforGeeks
Hey! GeeksforGeeks
Hey! GeeksforGeeks
Hey! GeeksforGeeks
Hey! GeeksforGeeks
Hey! GeeksforGeeks
Hey! GeeksforGeeks
.........

Example 2:

In this example, we are using for loop to create an infinite loop.

C#




// C# program to illustrate 
// infinte loop
using System; 
  
class GFG{
      
public static void Main() 
      
    // Creating infinite loop
    // Using for loop
    for(;;) 
      
    // This statement will be printed 
    // infinite times 
    Console.WriteLine("Welcome GFG!!"); 
}

Output:

Welcome GFG!!
Welcome GFG!!
Welcome GFG!!
Welcome GFG!!
Welcome GFG!!
Welcome GFG!!
Welcome GFG!!
Welcome GFG!!
........



My Personal Notes arrow_drop_up
Recommended Articles
Page :