Having trouble understanding 'for' loop parameters

This is a discussion on Having trouble understanding 'for' loop parameters within the C Programming forums, part of the General Programming Boards category; i am working through a fundamental C book and i thought i understood for loops but this has me completely ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Location
    127.0.0.1
    Posts
    9

    Having trouble understanding 'for' loop parameters

    i am working through a fundamental C book and i thought i understood for loops but this has me completely stumped. maybe someone here can explain the difference of the below snippets of code.

    Code:
    int c = 0;
    int i = 0;
    
    for (c = getchar(); c != EOF;)
    {
    if (c == ' ')
        i++;
    }
    printf("%d", i);
    and

    Code:
    int c;
    int i = 0;
    
    for ( c = 0; (c = getchar()) != EOF;)
    {
    if(c == ' ')
        i++;
    }
    printf("%d", i);
    they both compile and execute but the first one will not exit when i press ctrl + d and give the print out. the second one will work just fine giving the print out i want.

    the only difference i can see is in the for loops but they are the same, right? obviously not but i can't see how. can someone please explain the difference here?

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    for( initialization [optional] ;   conditional [optional] evaluate to TRUE if no cond ; increment/decrement [optiona] )   semicolon are not optional!
    for( i = 0; i < 10; i++)
    statement_list;
    In this loop, i is initialized to zero the first time. if the condition i < 10 is true, statement_list is executed, then i++ is executed at the end. Then go on to checking condition, and so on.
    i = 0;
    for(; i < 10;i++) // opt init eg

    for( ; ; ) // infinite loop

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    A for loop has three fields, an init field done once, a condition, and a update field.

    Code:
    for( first initial state; condition; update )
    In your first example you set the initial value of c to what ever getchar() returns, then you run the loop until c is EOF, which it will never be unless it's the first thing that getchar() gets in the init of c.

    In your second example you init c to 0, then you continuously get new values for c in each iteration until c is EOF.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    The first parameter is only executed ONCE...when the loop is first entered.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    This code:
    Code:
    for (initialization; conditional; incrementation)
    {
        ...
    }

    Is equivelent to:

    Code:
    initialization;
    while (conditional)
    {
        ...
        incrementation;
    }

    So, your first example as a while loop looks like this:

    Code:
    int c = 0;
    int i = 0;
    
    c = getchar();
    while (c!=EOF)
    {
        if (c == ' ')
        i++;
    }
    printf("%d", i);

    whereas your second code snippet translates to:

    Code:
    int c;
    int i = 0;
    
    c=0;
    while ((c = getchar()) != EOF)
    {
        if(c == ' ')
        i++;
    }
    printf("%d", i);
    Last edited by KBriggs; 06-23-2010 at 07:03 PM.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Location
    127.0.0.1
    Posts
    9
    DISREGARD THIS POST.

    i figured it out on my own. thanks again for all the help



    in my first snippet i already have c initialized when i declare it so could i do this:

    Code:
    for(;c = getchar(); c != EOF)
    notice the initial semi colon.

    i know this may seem trivial/annoying/why doesn't he just do it the way that works but i would like to understand all possibilities and why they do or don't work.

    thank you all for your responses and patience for dealing with a noob.
    Last edited by spottedzebra; 06-23-2010 at 07:17 PM.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The condition is now:

    Code:
    while(c)

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Subsonics View Post
    The condition is now:

    Code:
    while(c)
    No, it's still...

    Code:
    while(c = getchar())
    They are by no means equivalent.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by zacs7 View Post
    No, it's still...

    Code:
    while(c = getchar())
    They are by no means equivalent.
    Sure, but what is evaluated is c. How c get's it's value is irrelevant to the evaluation, in this case it's by getchar().

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Subsonics View Post
    Sure, but what is evaluated is c. How c get's it's value is irrelevant to the evaluation, in this case it's by getchar().
    Maybe so, but the time c gets it value is relevant.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by zacs7 View Post
    Maybe so, but the time c gets it value is relevant.
    What do you mean? Shall the loop continue or not? If c.

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Code:
    for(;c = getchar(); c != EOF)
    The c!=EOF does nothing in that position. It would just be eexecuted as:

    c!=EOF;

    at the end of each iteration. This would evaluate to 1 or 0 depending on if it is true or not, but it would have no effect whatsoever on the loop.

    If you are trying to pecify the stopping condition for a loop, it has to go in the second of the three slots.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function with variable number of parameters
    By mikahell in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 03:35 PM
  2. I'm having a really hard time understanding classes.
    By imortal in forum C++ Programming
    Replies: 4
    Last Post: 05-25-2003, 01:02 PM
  3. Passing parameters from VB to C++ through ActiveX DLL
    By torbjorn in forum Windows Programming
    Replies: 0
    Last Post: 12-10-2002, 02:13 AM
  4. Trouble Understanding Classes and Objects. Please Help.
    By Jeffcubed in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2002, 01:23 PM
  5. command-line parameters.
    By Tombear in forum C Programming
    Replies: 2
    Last Post: 10-28-2001, 07:40 AM

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