Learn C# Programming
Master C# programming from basics to advanced concepts with our comprehensive tutorial series. Perfect for beginners and experienced developers.
C# while Loop
The while loop executes a block of code repeatedly as long as a specified condition is true.
Basic while Loop Syntax
while (condition)
{
// Code to execute
// Don't forget to update the condition variable!
}
Simple while Loop
int count = 1;
while (count <= 5)
{
Console.WriteLine($"Count: {count}");
count++; // Important: increment to avoid infinite loop
}
// Output: Count: 1, Count: 2, Count: 3, Count: 4, Count: 5
User Input Validation
int number;
bool isValid = false;
while (!isValid)
{
Console.Write("Enter a number between 1 and 10: ");
string input = Console.ReadLine();
if (int.TryParse(input, out number) && number >= 1 && number <= 10)
{
isValid = true;
Console.WriteLine($"Valid number entered: {number}");
}
else
{
Console.WriteLine("Invalid input. Please try again.");
}
}
Reading Until Specific Input
string userInput;
Console.WriteLine("Type 'exit' to quit:");
while ((userInput = Console.ReadLine()) != "exit")
{
Console.WriteLine($"You typed: {userInput}");
Console.WriteLine("Type 'exit' to quit:");
}
Console.WriteLine("Goodbye!");
Processing Collections
List numbers = new List {1, 2, 3, 4, 5};
int index = 0;
while (index < numbers.Count)
{
Console.WriteLine($"Number at index {index}: {numbers[index]}");
index++;
}
// Finding an element
int target = 3;
int position = 0;
bool found = false;
while (position < numbers.Count && !found)
{
if (numbers[position] == target)
{
found = true;
Console.WriteLine($"Found {target} at index {position}");
}
position++;
}
if (!found)
{
Console.WriteLine($"{target} not found");
}
Nested while Loops
int i = 1;
while (i <= 3)
{
int j = 1;
while (j <= 3)
{
Console.Write($"({i},{j}) ");
j++;
}
Console.WriteLine(); // New line
i++;
}
// Output:
// (1,1) (1,2) (1,3)
// (2,1) (2,2) (2,3)
// (3,1) (3,2) (3,3)
while vs for Loop
// When you know the exact number of iterations, use for
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
// When the number of iterations depends on a condition, use while
Random random = new Random();
int randomNumber;
while ((randomNumber = random.Next(1, 7)) != 6)
{
Console.WriteLine($"Rolled: {randomNumber}");
}
Console.WriteLine("Finally rolled a 6!");
Common Pitfalls
// WRONG: Infinite loop - condition never changes
// int x = 5;
// while (x > 0)
// {
// Console.WriteLine(x);
// // Missing: x--; or x = x - 1;
// }
// CORRECT: Always update the condition variable
int x = 5;
while (x > 0)
{
Console.WriteLine(x);
x--; // This ensures the loop will eventually end
}
💡 Best Practices
- Always ensure the loop condition will eventually become false
- Initialize variables before the loop
- Update condition variables inside the loop
- Use while for unknown number of iterations
- Use for when you know the exact number of iterations
- Consider using break and continue for complex flow control
Frequently Asked Questions
Master C# Programming with Our Comprehensive Tutorial
Our C# programming tutorial is designed to take you from a complete beginner to an advanced developer. Whether you're looking to build web applications, desktop software, or mobile apps, C# provides the foundation you need for modern software development.
Start with the basics like variables, data types, and control structures, then progress to advanced topics including object-oriented programming, LINQ, async programming, and more. Each lesson includes practical examples and exercises to reinforce your learning.
Join thousands of developers who have learned C# programming through our step-by-step tutorials. Begin your journey today and unlock the power of .NET development.