Results 1 to 6 of 6
  1. #1
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default help understanding this for loop

    hello i was looking at some code today and came across a for loop constructed in a unique way
    Java Code:
    for ( ; tmp != null && !(tmp.info.equals(el));
    pred = pred.next, tmp = tmp.next);
    the part that really messes me up is starting the for loop with a ;

    can someone please explain to me what is going on

    thanks

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default

    Copy the code into a small program and add some printlns to show the values of all the variables as the loop executes.

    There are objects and method definitions missing that make it hard to say what the code is doing.
    Where are the definitions for tmp and pred?

    the for loop with a ;
    I assume you mean that the initialize section of the for() is missing. You can do the init before the loop starts.
    The example shows an empty init section. The init was done before the loop started.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Note that the 3 parts of a for loop that you are used to seeing: initialisation, condition, increment are all optional. Below is totally valid:
    Java Code:
    for(;;) {
    
    }
    This would be an infinite loop. You would need to add some code inside the loop to control it and when to break out of the loop.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,456
    Rep Power
    25

    Default

    Why do people write code like this without commenting what it is supposed to do?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I just took another look. It seems to be some sort of List iterator.

  6. #6
    hoosierfan24 is offline Member
    Join Date
    Oct 2010
    Posts
    45
    Rep Power
    0

    Default

    ok thanks I understand it now. Yeah the code was part of a delete method that would search through a linked list and delete the object it was looking for. And for the for loop a better way to code it would be
    Java Code:
    while(tmp != null && !(tmp.info.equals(el)) { pred = pred.next; tmp = tmp.next; }
    I think i was just thrown off by the weird look of the code.

    let me know if you want the whole delete method it works and runs perfectly

Similar Threads

  1. Need some help understanding Parsing
    By mcfer in forum New To Java
    Replies: 5
    Last Post: 07-15-2011, 08:43 PM
  2. Need some understanding and help!
    By Kevinius in forum New To Java
    Replies: 8
    Last Post: 05-14-2011, 05:50 AM
  3. Subclass understanding?
    By LifeElixer in forum New To Java
    Replies: 6
    Last Post: 04-13-2011, 08:57 AM
  4. Vector understanding
    By counterfox in forum New To Java
    Replies: 6
    Last Post: 05-04-2010, 10:59 AM
  5. need help in understanding collection
    By ShinTec in forum Advanced Java
    Replies: 2
    Last Post: 04-24-2010, 02:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •