Advertisement



< Prev
Next >



C# While Loop





Just like Java, the C# language also provides us an important looping structure using which we could execute one or multiple statements within a loop until a condition is met. The condition of while loop is tested for the boolean value - true or false.
Note:






C# while loop example


//C# while loop example


using System;

class WhileEx1  
{

public static void Main()
{

	int i=0;

	while(i<5)
	{
		Console.WriteLine("i = " + i);
		i++;
	}
}  
}


Output


i = 0
i = 1
i = 2
i = 3
i = 4



Advertisement




While loop must have an iteration part in its body or it runs an endless loop


//C# An endless while loop example


using System;

class WhileEx1  
{

public static void Main()
{

	int i=0;

	while(i<5)
	{
		Console.WriteLine("i = " + i);
		//We have not specified the iteration part of while loop
		//Hence, this while loop will run endlessly.
	}
}  
} 


Output


This program when executed, runs indefinitely in a loop and printing the initialized value of i, 0, over and over again, because of the absence of incremental part to increment the value of i.



Please share this article -





< Prev
Next >
< C# Nested If Statement
C# Do While Loop >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement