C# – Infinite Loop

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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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");
    }
}
}

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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!!"); 
}

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.