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 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:

  1. Initialization → Start counting (int i = 1;)
  2. Condition → Keep going until a limit (i <= 10;)
  3. 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?

  1. Step 1: The loop starts with i = 1.
  2. Step 2: It checks i <= 10. If true, it runs the loop.
  3. Step 3: It prints "Push-up X", then increases i by 1.
  4. Step 4: When i becomes 11, the condition i <= 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! 🚀

Leave a Comment

Share this Doc

For loop

Or copy link