Open In App

C# – Infinite Loop

Improve
Improve
Like Article
Like
Save
Share
Report

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
// infinite 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
// infinite 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!!
........


Last Updated : 21 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads