C# While Loop

C# While Loop is used to execute a set of statements in a loop based on a condition.

Syntax of C# While

Following is the syntax of While Loop in C#.

while(condition){
     /* statement(s) */
 }

where

  • while is the keyword.
  • condition is a boolean expression.
  • statement(s) are a set of C# statements which will be executed in a loop.
ADVERTISEMENT

Example 1 – C# While Loop

Following program is a simple example for while loop. The C# program prints numbers from 5 to 9.

Program.cs

using System;

namespace CSharpExamples {
    class Program {
        static void Main(string[] args) {
            int a=5;
            while(a<=9){
                Console.WriteLine(a);
                a++;
            }
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
5
6
7
8
9

How does C# While Works?

C# While Loop

Step 1: The program control enters the while loop by checking the condition.

Step 2: If the condition evaluates to true, then the statement(s) inside while loop are executed.

Step 3: After executing all the statement(s), the condition is again checked. Step 2 and Step 3 are executed in a loop until condition evaluates to false.

Step 4: If the condition evaluates to false, the program control comes out of while loop, and the execution of while loop statement is completed.

Example 2 – While Loop with Break

In this example, we will use a break statement to come out of the loop prematurely (without condition becoming false).

Program.cs

using System;

namespace CSharpExamples {
    class Program {
        static void Main(string[] args) {
            int a=5;
            while(a<=9){
                Console.WriteLine(a);
                a++;
                if(a==8){
                    break;
                }
            }
        }
    }
}

When the value of a becomes 8, break statement is executed and the program control comes out of the while loop.

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
5
6
7

Nested While Loop in C#

We can place while loop inside a while loop. Following example demonstrates how.

Example 3 – While Loop inside While Loop

In this example, we will write a while loop inside another while loop, making it a nested while loop.

Program.cs

using System;

namespace CSharpExamples {
    class Program {
        static void Main(string[] args) {
            int a=1;
            while(a<4){
                int b=1;
                while(b<3){
                    Console.WriteLine(a+"."+b);
                    b++;
                }
                a++;
            }
        }
    }
}

Output

PS D:\workspace\csharp\HelloWorld> dotnet run
1.1
1.2
2.1
2.2
3.1
3.2

Infinite While Loop

We can write a while loop that gets executed infinitely in a loop. All you have to do is provide true in place of condition and no break statement in the while loop.

Example 4 – C# Infinite While Loop

The following program prints number starting from 1. When it reaches the limit of integer datatype in C#, the sign bit is overflowed and you will see negative numbers and then positive numbers again and so on.

Program.cs

using System;

namespace CSharpExamples {
    class Program {
        static void Main(string[] args) {
            int a=1;
            while(true){
                Console.WriteLine(a++);
            }
        }
    }
}

Output

Only part of the output is provided here.

PS D:\workspace\csharp\HelloWorld> dotnet run
1
2
3
4
5
6
.....

Conclusion

In this C# Tutorial, we learned the syntax of C# while loop and usage of while loop in different scenarios like: nested while loops, infinite loop, while loop with break statement, etc.