Learn C# Programming
Master C# programming from basics to advanced concepts with our comprehensive tutorial series. Perfect for beginners and experienced developers.
C# for Loop
The for loop is used to execute a block of code repeatedly for a specific number of times.
Basic for Loop Syntax
for (initialization; condition; increment/decrement)
{
// Code to execute
}
Simple for Loop
// Print numbers 1 to 5
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
// Output: 1, 2, 3, 4, 5
Counting Backwards
// Count down from 10 to 1
for (int i = 10; i >= 1; i--)
{
Console.WriteLine(i);
}
// Output: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Different Step Sizes
// Print even numbers from 2 to 10
for (int i = 2; i <= 10; i += 2)
{
Console.WriteLine(i);
}
// Print multiples of 5
for (int i = 5; i <= 50; i += 5)
{
Console.WriteLine(i);
}
Nested for Loops
// Multiplication table
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= 5; j++)
{
Console.Write($"{i * j}\t");
}
Console.WriteLine(); // New line after each row
}
// Pattern printing
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
Console.Write("* ");
}
Console.WriteLine();
}
for Loop with Arrays
int[] numbers = {10, 20, 30, 40, 50};
// Access array elements using index
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine($"Index {i}: {numbers[i]}");
}
// Calculate sum of array elements
int sum = 0;
for (int i = 0; i < numbers.Length; i++)
{
sum += numbers[i];
}
Console.WriteLine($"Sum: {sum}");
foreach Loop (Alternative)
int[] numbers = {10, 20, 30, 40, 50};
// foreach is simpler for iterating through collections
foreach (int number in numbers)
{
Console.WriteLine(number);
}
// foreach with strings
string[] names = {"Alice", "Bob", "Charlie"};
foreach (string name in names)
{
Console.WriteLine($"Hello, {name}!");
}
Infinite Loop (Be Careful!)
// This creates an infinite loop - avoid this!
// for (;;)
// {
// Console.WriteLine("This will run forever!");
// }
// Better approach with break condition
for (int i = 0; ; i++)
{
if (i >= 10)
break;
Console.WriteLine(i);
}
💡 Best Practices
- Use meaningful variable names (avoid single letters except for simple counters)
- Prefer foreach for simple iteration over collections
- Be careful with loop conditions to avoid infinite loops
- Consider using LINQ for complex data operations
- Keep nested loops to a minimum for better performance
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.