C# For Loop Tutorial – Simple Explanation with Example
Imagine This…
Think about counting push-ups during a workout. 🏋️ You decide to do 10 push-ups and count them one by one:
🔢➡️ 1, 2, 3… all the way to 10!
This is exactly how a for loop works in C#! It repeats an action a specific number of times—just like counting push-ups!
Now, let’s dive into C# For Loop Tutorial with an easy For loop Example C# and a fun explanation.
What You Are Going to Learn in This Lesson
✔️ What is a for loop in C#?
✔️ How does it work?
✔️ A simple For loop Example C#
✔️ Complete code with output
✔️ A friendly conclusion to wrap things up!
What is a For Loop in C#?
A for loop is used when you know how many times you want to repeat an action. It consists of three parts:
- Initialization → Start counting (
int i = 1;
) - Condition → Keep going until a limit (
i <= 10;
) - Increment → Increase the count (
i++
)
For Loop Example C# (Counting Push-ups 💪)
Let’s write a simple C# program that counts push-ups using a for loop:
using System;
class Program
{
static void Main()
{
Console.WriteLine("Let's do 10 push-ups!");
for (int i = 1; i <= 10; i++) // Loop from 1 to 10
{
Console.WriteLine($"Push-up {i}"); // Display each count
}
Console.WriteLine("Great job! Workout complete! 🎉");
}
}
Expected Output
Let's do 10 push-ups!
Push-up 1
Push-up 2
Push-up 3
Push-up 4
Push-up 5
Push-up 6
Push-up 7
Push-up 8
Push-up 9
Push-up 10
Great job! Workout complete! 🎉
How Does This Code Work?
- Step 1: The loop starts with
i = 1
. - Step 2: It checks
i <= 10
. If true, it runs the loop. - Step 3: It prints
"Push-up X"
, then increasesi
by 1. - Step 4: When
i
becomes 11, the conditioni <= 10
becomes false, so the loop stops.
Why Use a For Loop?
- Saves time – No need to write the same code 10 times!
- Less mistakes – Everything runs smoothly without forgetting a step.
- Easy to manage – Just change the limit, and you’re good to go!
Conclusion
Now you know how a for loop works in C#! 🎉 It’s super useful when you need to repeat something a fixed number of times—like counting, printing, or even workouts!
If you have difficulty or a question, drop a comment. We will be happy to help you! 😊
Next What?
In the next chapter, you will learn about the Foreach loop in C#! It’s another cool way to loop through items easily. Stay tuned! 🚀