#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2

    Question on the arguments of for


    I learned that the arguments for the for loop are
    Code:
    for(variable initialization; loop condition; variable update)
    but I saw codes which had 1st and 3rd arguments not of the form "x = " (assigning a value to a variable), but of the form "scanf("%lf", x)".
    I confirmed that this works just as well, but I'm confused about exactly what I'm allowed to put in the 1st and third arguments.

    instead thinking as

    variable initialization
    and
    variable update

    for them, is it more correct to consider them

    the statement to be executed once at the beginning of the whole loop
    and
    the statement to be executed at the end of each loop?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    The first section is usually referred to as the declaration section, the second is usually referred to as the control section, and the last is the iteration section.
    From the C11 Draft standard: N1570 Committee Draft April 12, 2011 ISO/IEC9899:201x

    6.8.5.3 The for statement
    The statement
    for ( clause-1 ; expression-2 ; expression-3 ) statement
    behaves as follows: The expression expression-2 is the controlling expression that is
    evaluated before each execution of the loop body. The expression expression-3 is
    evaluated as a void expression after each execution of the loop body. If clause-1 is a
    declaration, the scope of any identifiers it declares is the remainder of the declaration and
    the entire loop, including the other two expressions; it is reached in the order of execution
    before the first evaluation of the controlling expression. If clause-1 is an expression, it is
    evaluated as a void expression before the first evaluation of the controlling expression.158)
    Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a
    nonzero constant.
    Jim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    I see, thank you.
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,112
    Rep Power
    1803
    Originally Posted by 046
    for them, is it more correct to consider them

    the statement to be executed once at the beginning of the whole loop
    and
    the statement to be executed at the end of each loop?
    Not quite, because a statement and an expression are not the same thing. An expression has both value and type, an statement may not have either.

    Each of the three elements may be any valid expression, including function calls. I would suggest that any expression involving scanf() is probably ill advised. It is generally best to keep it simple for maintainability and clarity. Any expression involving a function with side-effects as a control for a for-loop is probably a bad idea.

    Not all code you don't understand is good code - often the reverse.

    Code:
    for( e1; e2; e3 )
    {
        body
    }
    is exactly equivalent to
    Code:
    e1 ;
    while( e2 )
    {
        body
    
        e3 ;
    }
    Generally id e1 and e3 are not related to e2 (in the sense that the are expressions with variables in common), then the second form should be used. Its a style thing however; you can break that guideline, but if you are producing code that others must work on or maintain, no one will thank you for "clever" code.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by clifford
    Code:
    for( e1; e2; e3 )
    {
        body
    }
    is exactly equivalent to
    Code:
    e1 ;
    while( e2 )
    {
        body
    
        e3 ;
    }
    Not so when body contains break or continue statements.

IMN logo majestic logo threadwatch logo seochat tools logo