Click here to Skip to main content
15,895,800 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made a diamond pattern program using for loop. I want to do it using foreach loop. Any one can please help me out how do i convert my for loops to foreach loop.

Here is code:
C#
Console.WriteLine("Program for displaying pattern of *.");

          Console.Write("Enter the maximum number of *: ");

          int n = Convert.ToInt32(Console.ReadLine());
          Console.WriteLine("\nHere is the Diamond of Stars\n");
          for (int i = 0; i <= (n - 1); i = i + 2)
          {
              for (int j = 0; j <= i; j++)
                  Console.Write("*");
              Console.WriteLine();
          }
          for (int i = n - 1; i >= 1; i = i - 2)
          {
              for (int j = 0; j < (i - 1); j++)
                  Console.Write("*");
              Console.WriteLine();
          }
Posted
Comments
Tomas Takac 4-Aug-15 6:26am    
Why do you want to use foreach when for works just fine?
Member 11808002 4-Aug-15 6:27am    
for sake of practise.
F-ES Sitecore 4-Aug-15 6:36am    
There is no direct foreach equivalent of what you're doing. You'd have to create a List<int> and add to the list the numbers 0, 2, 4, 6 etc etc, then do a foreach(int i in mylist) for the first loop, then reverse the list and do the same for the second loop. You could use an array of int[] rather than a List, I forget which out of arrays and List has a Reverse function.
Member 11808002 4-Aug-15 6:36am    
Help me out please. I am stuck here

1 solution

In those cases, it makes no sense to try: you aren't using a collection of objects, so there is no base for a foreach loop - these only work when you are iterating though a collection of objects that implements the IEnumerable or IEnumerable<T> interface.
Int32 doesn't do that, so you can't use a foreach to replace your for loops.
 
Share this answer
 
Comments
Member 11808002 4-Aug-15 6:59am    
You mean to say. I get value from user using array, and then i am able to do this task using foreach loop?
OriginalGriff 4-Aug-15 7:26am    
Not realy - the user returns you a single number, not a collection of numbers. And there isn't really any way you can get the user to give you the "right" numbers as a collection without complicating your code horribly, and presuming that you hate your users! :laugh:

You could easily replace it with a while loop - but then you'd just be doing the three parts of the for loop manually instead of collected in a single place where it's easy to read.
The for loop is the right one for this exercise!
The change I would probably make would be to extract a method which printed a line "n" characters long, and call that in two places instead of your inner loops.

I'd also recommend that you always use curly brackets, even when you don't have to - until you are a little more experienced.
There is little more frustrating than trying to work out what is wrong because you add a line "inside" the loop or conditional and it doesn't get executed at the right time because there are no curly brackets! It's a little more typing - but hardly any - and it can save you hours of hair pulling followed by a "doh!" moment... :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900