for structure whitout {}?

This is a discussion on for structure whitout {}? within the C++ Programming forums, part of the General Programming Boards category; Code: for ( int i = 0; i <= 10; i++ ) ; cout << setw( 2 ) << i ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    28

    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

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,855
    >for ( int i = 0; i <= 10; i++ ) ;
    Remove the semicolon.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    Oh that was typing mistake but my question is still the same.
    Thanks.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,630
    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();
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,855
    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';
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    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

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21