C# - Infinite Loop - GeeksforGeeks (2025)

Table of Contents
C# C# Similar Reads References

Last Updated : 21 Jul, 2021

Summarize

Comments

Improve

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! GeeksforGeeksHey! GeeksforGeeksHey! GeeksforGeeksHey! GeeksforGeeksHey! GeeksforGeeksHey! GeeksforGeeksHey! GeeksforGeeksHey! GeeksforGeeksHey! 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!!........


Next Article

C# Loops

ankita_saini

C# - Infinite Loop - GeeksforGeeks (2)

Improve

Article Tags :

  • C#

Similar Reads

  • 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 lo 2 min read
  • C# Loops Looping in a programming language is a way to execute a statement or a set of statements multiple times depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops. Loops are mainly divided into two categories 5 min read
  • C# Loops Looping in a programming language is a way to execute a statement or a set of statements multiple times depending on the result of the condition to be evaluated to execute statements. The result condition should be true to execute statements within loops. Loops are mainly divided into two categories 5 min read
  • C#- Nested loops Nested loops are those loops that are present inside another loop. In C#, nesting of for, while, and do-while loops are allowed and you can also put any nested loop inside any other type of loop like in a for loop you are allowed to put nested if loop. for Loop: The functionality of for loop is quit 3 min read
  • C# - Break statement In C#, the break statement is used to terminate a loop(for, if, while, etc.) or a switch statement on a certain condition. And after terminating the controls will pass to the statements that present after the break statement, if available. If the break statement exists in the nested loop, then it wi 4 min read
  • How to Initialize Array to 0 in C? Initializing an array to zero is a common practice in programming to ensure that all elements start with a known value. In C, there are several ways to initialize an array to zero. In this article, we will explore different methods to do so. Initialize Array to 0 in CThere are mainly two ways to ini 2 min read
  • Power of a Number in C In this article, we will learn how to write a C program to calculate the power of a number (xn). We may assume that x and n are small and overflow doesn't happen. C Program To Calculate the Power of a Number A simple solution to calculate the power would be to multiply x exactly n times. We can do t 2 min read
  • Interesting Infinite loop using characters in C We have already studied how a " for loop" works. Look at the following program: C/C++ Code int main(){ int i; for(i = 0; i < 128; i++){ printf("I am %d\n", i); } return 0; } In the above loop, the printf() statement will be executed 128 times. Now, look at the following prog 3 min read
  • How to Take Multiple Input in C? In C, we use scanf to take input from the user, and often it is necessary to take multiple inputs simultaneously. In this article, we will learn about how to take multiple inputs from the users in C. Multiple Inputs From a User in CTo take multiple inputs from users in C, we can declare an array to 2 min read
  • How to store words in an array in C? We all know how to store a word or String, how to store characters in an array, etc. This article will help you understand how to store words in an array in C. To store the words, a 2-D char array is required. In this 2-D array, each row will contain a word each. Hence the rows will denote the index 2 min read
  • Converting String to Long in C Here, we will see how to build a C Program For String to Long Conversion using strtol() function. Syntax: long int strtol(char *string, char **ptr, int base)The first argument is given as a stringThe second argument is a reference to an object of type char*The third argument denotes the base in whic 4 min read
  • How to Read From a File in C? File handing in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program. In this article, 2 min read
  • How to Initialize a Dynamic Array in C? In C, dynamic memory allocation is done to allocate memory during runtime. This is particularly useful when the size of an array is not known at compile time and needs to be specified during runtime. In this article, we will learn how to initialize a dynamic array in C. Initializing a Dynamic Arrays 2 min read
  • How to Take Operator as Input in C? In C, an operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. We may need to take operator as an input in some cases. In this article, we will learn how to take an operator as input in C. Get Input Operator in CTo take an operator as input in C, we 2 min read
  • C Program to Print Contents of File C language allows users to process the files in its programs. Reading a file is a step-by-step process in which we first have to prepare the file only after which we can start reading. In this article, we will learn how to read and print the contents of a file using C program. The simplest method to 4 min read
  • C Program to Print Contents of File C language allows users to process the files in its programs. Reading a file is a step-by-step process in which we first have to prepare the file only after which we can start reading. In this article, we will learn how to read and print the contents of a file using C program. The simplest method to 4 min read
  • C Program to Print Fibonacci Series The Fibonacci series is the sequence where each number is the sum of the previous two numbers of the sequence. The first two numbers are 0 and 1 which are used to generate the whole series. Example Input: n = 5Output: 0 1 1 2 3Explanation: The first 5 terms of the Fibonacci series are 0, 1, 1, 2, 3. 4 min read
  • C Program For Finding Length Of A Linked List Write a function to count the number of nodes in a given singly linked list. For example, the function should return 5 for linked list 1->3->1->2->1. Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Iterative Solution: 1) Initialize count as 0 2) Initia 2 min read
  • C/C++ program for calling main() in main() Given a number N, the task is to write C/C++ program to print the number from N to 1 by calling the main() function using recursion.Examples: Input: N = 10 Output: 10 9 8 7 6 5 4 3 2 1Input: N = 5 Output: 5 4 3 2 1 Approach: Use static variable to initialise the given number N.Print the number N and 2 min read
C# - Infinite Loop - GeeksforGeeks (2025)

References

Top Articles
Latest Posts
Recommended Articles
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 5539

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.