Complete C# Tutorial

C# Loop Examples: Easy and Practical code Examples in C#

Hey there, coding buddy! πŸ‘‹ Ready to dive into the world of C# loop examples? Loops are like magic tools in programmingβ€”they help you repeat tasks without writing the same code over and over. Imagine having to print numbers from 1 to 100 manually… Sounds tiring, right? πŸ˜… That’s where loops come to the rescue! πŸ¦Έβ€β™‚οΈ

In this lesson, you’ll explore various loop examples in C# like for, foreach, while, and even advanced ones like Parallel.For. Don’t worry, I’ll keep things simple, fun, and easy to understand. Plus, you’ll find real-world examples that you can actually relate to. So, how about we get started? πŸš€

πŸŒ€ 1. For Loop

βœ… Solution:

				
					using System;

class Program {
    static void Main() {
        for (int i = 1; i <= 5; i++) {
            Console.WriteLine(i);
        }
    }
}
				
			

πŸ“ Code Explanation:

  • int i = 1: Start from 1.
  • i <= 5: Run until i is 5.
  • i++: Increase i by 1 each loop.

➑️ Output:

				
					1  
2  
3  
4  
5  
				
			

πŸ”„ 2. Foreach Loop

βœ… Solution:

				
					using System;

class Program {
    static void Main() {
        string[] fruits = { "Apple", "Banana", "Cherry" };
        foreach (string fruit in fruits) {
            Console.WriteLine(fruit);
        }
    }
}
				
			

πŸ“ Code Explanation:

  • foreach (string fruit in fruits): Loops through each item in fruits.

➑️ Output:

				
					Apple  
Banana  
Cherry  

				
			

πŸ” 3. While Loop

βœ… Solution:

				
					using System;

class Program {
    static void Main() {
        int i = 1;
        while (i <= 3) {
            Console.WriteLine(i);
            i++;
        }
    }
}
				
			

πŸ“ Code Explanation:

  • Starts at 1, prints it, then increments until i > 3.

➑️ Output:

				
					1  
2  
3  
				
			

πŸ”‚ 4. Do-While Loop

βœ… Solution:

				
					using System;

class Program {
    static void Main() {
        int i = 1;
        do {
            Console.WriteLine(i);
            i++;
        } while (i <= 3);
    }
}
				
			

πŸ“ Code Explanation:

  • Executes code block first, checks condition later.

➑️ Output:

				
					1  
2  
3  
				
			

⚑ 5. Parallel.For Loop (For Multithreading)

βœ… Solution:

				
					using System;
using System.Threading.Tasks;

class Program {
    static void Main() {
        Parallel.For(1, 6, i => {
            Console.WriteLine($"Number: {i}");
        });
    }
}
				
			

πŸ“ Code Explanation:

  • Runs iterations simultaneously for speed.

➑️ Output:

				
					Number: 3  
Number: 1  
Number: 5  
Number: 2  
Number: 4  
				
			

⚑ 6. Parallel.ForEach Loop

βœ… Solution:

				
					using System;
using System.Threading.Tasks;

class Program {
    static void Main() {
        string[] colors = { "Red", "Green", "Blue" };
        Parallel.ForEach(colors, color => {
            Console.WriteLine(color);
        });
    }
}
				
			

πŸ“ Code Explanation:

  • Each item is processed in parallel, speeding up execution.

➑️ Output:

				
					Red  
Green  
Blue  				
			

πŸš€ 7. Goto Loop (Use with caution!)

βœ… Solution:

				
					using System;

class Program {
    static void Main() {
        int i = 1;
    start:
        if (i <= 3) {
            Console.WriteLine(i);
            i++;
            goto start;
        }
    }
}
				
			

πŸ“ Code Explanation:

  • Not recommended for general use, but useful in certain cases.

➑️ Output:

				
					1  
2  
3  
				
			

πŸ”„ 8. Recursion (Functions calling themselves!)

βœ… Solution:

				
					using System;

class Program {
    static void PrintNumbers(int n) {
        if (n > 3) return; // Base case
        Console.WriteLine(n);
        PrintNumbers(n + 1); // Recursive call
    }

    static void Main() {
        PrintNumbers(1);
    }
}
				
			

πŸ“ Code Explanation:

  • Calls PrintNumbers until n > 3.

➑️ Output:

				
					1  
2  
3  
				
			

πŸ† Conclusion

Woohoo! πŸŽ‰ You made it through the world of loops! Feeling like a coding wizard yet? πŸ§™β€β™€οΈβœ¨ Loops are a huge part of programming, and now you know how to use them to make your code shorter, faster, and more powerful. From the simple for loop to the brainy recursion, you’ve tackled them all. High five! πŸ™Œ

Remember, loops are all about repetition with purpose. So, keep practicing and you’ll get better and faster at spotting when to use which loop. You’ve got this! πŸ’ͺ

πŸš€ Next What?

Alright, rockstar! 🌟 You’ve just mastered the basics of loops. But waitβ€”it’s time for a challenge! In the next lesson, you’ll get to practice some cool coding exercises on loops. 🎯 Yep, you’ll solve real-world problems, write your own code, and feel super accomplished. How exciting is that? 😎

So, are you ready to put those looping skills to the test? Grab your favorite drink β˜•, fire up your code editor, and let’s get coding in the next chapter! πŸš€

Leave a Comment

Share this Doc

Programming examples

Or copy link