tags:

views:

386

answers:

4

Hi guys,

I want to know if there's a website that provides amazing code star designs for my console application. For example I want a code that can output the pyramid using for loops:

    *
   ***
  *****
*********

Or code that can output the half-life logo using for loops. It doesn't matter where the code was created, as long as I can understand the for loop it is ok.

+1  A: 

Google Code Search

Aamir
+1  A: 
int height = 5;

for (int count = 1; count <= height; count++)
    Console.WriteLine(new String('*', count * 2 - 1).PadLeft(height + count));
ThePower
You should use Environment.NewLine instead of the string literal "\r\n". :) (It's one of those little things many people don't realize exists.)
280Z28
Thanks :-)
ThePower
A: 
int rowCount = 5;
for (int i = 0; i < rowCount; i++)
{
    Console.Write(new string(' ', rowCount - i - 1));
    Console.Write(new string('*', 2 * i + 1));
    Console.WriteLine();
}
280Z28
A: 

Regarding your comment to the question: you're saying that one can do "some complex stuff" with the for-loop you're trying to discover. I have to say: you seem to be on the wrong track. The for-loop has always the same simple structure:

for (Type variable = startvalue; condition; action)
{
    // Do stuff
}

The "complex" stuff is to sometimes find out what condition is or what action to take. condition may be anything that evaluates to boolean and action may be anything too.

So the "complex" stuff has nothing to do with the structure of the for-loop itself. You could also write the following:

for (int i = 0; DateTime.Now < new DateTime(2009, 12, 31); i++)
{
    Console.WriteLine("I've iterated {0} times before 31st December 2009!", i);
}

The condition does not even consider the i variable, but still it is a valid for-loop.

Thorsten Dittmar