# for structure whitout {}?

• 04-09-2004
blackgold>>
for structure whitout {}?
Code:

``` for ( int i = 0; i <= 10; i++ ) ;   cout << setw( 2 ) << i << "! = " << factorial( i ) << endl; return 0; }```
Ok i am learning functions right now and was wondering why does the code in the secound line keep repeating if their are no { } between it?

Thanks alot.
Some things i have to ask sorry.
Cya
• 04-09-2004
Prelude
>for ( int i = 0; i <= 10; i++ ) ;
Remove the semicolon.
• 04-09-2004
blackgold>>
Oh that was typing mistake but my question is still the same.
Thanks.
• 04-09-2004
laserlight
You mean:
Code:

```for ( int i = 0; i <= 10; i++ )   cout << setw( 2 ) << i << "! = " << factorial( i ) << endl;```
Well, the reason would be that with a single statement, the braces are unnecessary.

So this would be ok:
Code:

```if (condition)         line_of_code(); else         line_of_code(); while (condition)         line_of_code();```
• 04-09-2004
Prelude
If the body of a loop is a single statement then the braces can be omitted. These two loops are equivalent:
Code:

```for ( int i = 0; i < 10; i++ ) {   cout<< i <<'\n'; } for ( int i = 0; i < 10; i++ )   cout<< i <<'\n';```
If you have more than one statement in the body then you must use braces. These two loops are not equivalent:
Code:

```for ( int i = 0, j = 9; i < 10; i++, j-- ) {   cout<< i <<'\t';   cout<< j <<'\n'; } for ( int i = 0, j = 9; i < 10; i++, j-- )   cout<< i <<'\t';   cout<< j <<'\n';```
• 04-09-2004
blackgold>>
I see what you mean now.

I tried the examples you gave also and it's cool.

Thanks both of you.

I sometimes learn to much then have to go back when i see something i don't quite understand.

Thanks alot.
cya