×

C# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on the user or not known In advance. Let's see a simple example of while loop.

Syntax
while(<condition>)
{
//statements
}
C# While loop example:
using System;
public class WhileLoopExample1
{
            public static void Main (string[] args)
            {
            int i=1;
                        while(i<10)
                        {
                                    Console.WriteLine(i);
                                    i++;
                        }
}
}
Output
1
2
3
4
5
6
7
8
9
C# nested while loop: Nested while loop is almost similar to nested for loop. We can use while loop inside a while loop. The inner will execute n number of time for each iteration of outer loop.
C#  Nested While loop Example:
using System;  
            public class WhileExample  
                {  
                  public static void Main(string[] args)  
                  {  
                      int i=1;    
                      while(i<=3)   
                      {  
                          int j = 1;  
                          while (j<=3)  
                                                  {  
                              Console.WriteLine(i+""+j);  
                              j++;  
                          }  
                          i++;  
                      }    
                 }  
               }
Output
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
C# infinite while loop example: If we put a condition in while loop that never fails then the loop becomes an infinite loop .
using System;
public class InfiniteWhileLoopExample{
public static void main (String[] args)
{
While(true)
{
            //some statements 
}
}
}

Related Topics

C# Abstraction

Abstraction is a mechanism by which we can hide the complexities and show only functionalities. C# provides an abstract keyword to declare a class and method as abstract. In C#,...

2 minutes read.

C# Arrays

Like in other programming languages, in C# the array can be defined as a collection of similar type of objects which have contiguous memory allocation. Array index starts from zero(0)....

3 minutes read.

C# Static Constructor

In C#, static constructor is the one which is used to initialize static fields. It can also be used to do the task which needs to be done once only....

3 minutes read.

C# Array Class

In C#, Array class deals with all array related operations. It provides methods for various purposes such as for sorting, searching, minimum, maximum, and many more. This class works as...

5 minutes read.

C# Command Line Arguments

In C#, Arguments to the main method can be sent by command line. Those arguments are called command line arguments. The string[] args variable holds the command line arguments Let's see...

1 minute read.

C# Namespace

C# Provides Concept of namespacing to organize your classes in a good manner. It makes the application easy to handle. It is like packaging of java. The System is a namespace...

1 minute read.

C# Constructors

In C#, Constructor is the special method with no return type. It is invoked automatically at the time of object creation. Its name must be the same as the class...

2 minutes read.

C# Access Modifiers

C# provides various keywords to control or restrict accessibility or scope of the data. We can apply access modifiers to functions and variables. There are five access modifiers in C#: Private ...

10 minutes read.

C# While Loop

while loop iterates a part of the code until the given condition fails. This is an entry controlled loop. While loop is recommended to use if the condition depends on...

4 minutes read.

C# static class

In C#, static class is the one which can only contain static members. It can’t be instantiated. It cannot be inherited. It can only have static constructors and cannot contain...

3 minutes read.

C# Multilevel Inheritance

In Multilevel inheritance, the class inheriting its parent class is further inherited by another class and so on. This type of inheritance is transitive that’s why the last derived class...

2 minutes read.

C# Destructor

Destructors works in the opposite way. They basically destruct the objects. We can’t send parameter in a destructor. We also can’t apply any modifier to the destructor. Likewise constructor, destructor...

1 minute read.

C# Properties

In C#, Properties enables class to provide a public way of setting and getting values or fields of the class. Properties provides Encapsulation in the class if we make all...

2 minutes read.

C# enum

Enum in C# is also known as enumerations. It basically refers to a named set of constants such as months, days, seasons, etc. Enum improves type safety. They contain fixed set...

3 minutes read.

C# Operators

Operator is a symbol that is responsible for the operations. There can be variety of operators for example arithmetic, logical, bitwise and many more. There are following type of operators used...

1 minute read.

Serialization in C#

Serialization in the C# is the process that converts the object into the byte streams by that we can use the files and save the files into the memory location...

4 minutes read.

C# Jump Statements

Break is a jump statement which is used to break the current loop or current flow of program and transfer the program control to the next block. It is used...

3 minutes read.

C# Inheritance

In C#, Inheritance is a mechanism by which one object acquires all the properties of another object automatically. The object which is inheriting the other object is called chilled object...

4 minutes read.

C# String

String can be defined as an array of characters. In C#, string is an object of System.String which represents sequence of characters. Strings can be concatenated, comparison, getting substring and...

6 minutes read.

C# Design Patterns

What are the Design Patterns? Design patterns are the reusable solutions to the problems that developers facing while solving any problem related to software development.Design patterns are the templates provided in...

6 minutes read.