for loop

This is a discussion on for loop within the C++ Programming forums, part of the General Programming Boards category; Code: for( NODE * p = A.front; p; p = p->next ) To better understand what I'm working on can ...

  1. #1
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88

    for loop

    Code:
    for( NODE * p = A.front; p; p = p->next )
    To better understand what I'm working on can someone please explain the condition for this for loop?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    Just break the for loop into a while and everything will become more clear.
    Code:
    Node* p = A.front; // I guess, that member front is of type Node*
    while(p) // which is equivalent to while(p != 0), which is equivalent to while(p != NULL)
    {
        //body of the for loop
        p = p->next;  //last part of for loop
    }
    I guess you probably are running through a simple linked list. From my point of view, while loops are more appropriate in this case, because you do not really know the number that you want your loop to be executed.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The condition is equivalent to p != NULL.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User CASHOUT's Avatar
    Join Date
    Jul 2011
    Location
    Florida
    Posts
    88
    thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 09:17 AM
  2. Replies: 1
    Last Post: 12-26-2011, 06:36 PM
  3. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  4. Help.. newbie for loop..stuck with the loop..
    By jochen in forum C Programming
    Replies: 15
    Last Post: 10-01-2007, 12:31 AM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM

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