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#
// 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! w3wiki" ); } } } |
Output:
Hey! w3wiki Hey! w3wiki Hey! w3wiki Hey! w3wiki Hey! w3wiki Hey! w3wiki Hey! w3wiki Hey! w3wiki Hey! w3wiki .........
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!! ........