# For Loops

• 11-05-2003
apoc632
For Loops
This probably is a stupid question hey we all got to start programming somewhere.

I got to use for loops to generate two patterns as follows.
*****
****
***
**
*

and

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

Any Ideas.
• 11-05-2003
Kyro
well
Code:

```for(i = 0; i < 10; ++i) { }```
is the same as
Code:

```i = 0; while(i < 10) {     ++i; }```
write some code if u haven't already and we'll help u out from there.
• 11-05-2003
curlious
Try expirmenting with nested for loops.

For instance there are five lines to suggestion which implies a loop counting to five. Second there is an inner loop which print a number of stars based on the current line.

Code:

```numberofstars = maxstars;  // maxstars=5 for (int lines=0; lines<5;++lines) {      while (numberofstar>0)     {         cout << '*';         numberofstar =numberofstars-1;        }      maxstars--;  // decrement by 1     numberstars=maxstars; }```
• 11-05-2003
apoc632
clueless
I've absolutley no idea how to write a for loop. Does this website explain them well. I've written while loops if that is any help.
• 11-05-2003
Kyro
• 11-05-2003
Prelude
>I've absolutley no idea how to write a for loop.
I had problems with for loops when I started. It helps to build a for loop from the equivalent while loop. You construct it like so:
Code:

```/* Start with a while loop */ i = 0; while ( i < 10 ) {   i++; } /* Put semicolons around the condition */ i = 0; while ( ; i < 10 ; ) {   i++; } /* Put the initialization of the condition variable before the condition */ while ( i = 0 ; i < 10 ; ) {   i++; } /* Put the increment of the condition variable after the condition */ while ( i = 0 ; i < 10 ; i++ ) { } /* Change the keyword while to for */ for ( i = 0 ; i < 10 ; i++ ) { }```
And you're done! Just remember that only the first and last steps of this process result in valid C code. ;)
• 11-05-2003
caroundw5h
why doesn't this code work?

Code:

```// starts declining # include <stdio.h> int main() { int stars; for(stars=5; stars>=1;stars--) // also for stars in range(6,0-1)     printf("*\n"*stars);     system("pause"); return 0; }```
• 11-05-2003
caroundw5h
Re: clueless
Quote:

Originally posted by apoc632
I've absolutley no idea how to write a for loop. Does this website explain them well. I've written while loops if that is any help.

LEARN FOR / WHILE LOOPS HERE
• 11-05-2003
Prelude
>why doesn't this code work?
Because it has syntax errors and undefined behavior. If you clean it up so that it works, you get this:
Code:

```// starts declining # include <stdio.h> int main() {   int stars;     for(stars=5; stars>=1;stars--) // also for stars in range(6,0-1)     printf("*\n");     return 0; }```
• 11-05-2003
caroundw5h
Quote:

Originally posted by Prelude
[B]>why doesn't this code work?
Because it has syntax errors and undefined behavior. If you clean it up so that it works, you get this:
Your code doesn't work;

I assume you didn't understand my question. The code should output what apoc wanted it to.

that is why
Code:

`printf("*\n" * stars);`
I believe the problem is in that statement.
• 11-05-2003
Prelude
>Your code doesn't work;
It works just fine, you just weren't expecting what you got. But let's play on your assumptions a bit if you don't mind:
Code:

`printf("*\n" * stars);`
Even if this were to print "*\n" stars times, you would still only have one * per line, so it wouldn't work the way you want even if it were legal.

>I assume you didn't understand my question.
Actually, I did. But to answer it would be to solve the problem for you, and that takes all of the fun out of it. :)

>The code should output what apoc wanted it to.
While it's possible to only use one loop (I've posted code that does so on this forum before), such a solution isn't meant for beginners and is primarily a curiosity for experienced C users to while away the time. A beginner's solution will always use nested loops.