Rewriting a for loop as a while/do-while loop

This is a discussion on Rewriting a for loop as a while/do-while loop within the C++ Programming forums, part of the General Programming Boards category; Okay, I know this isn't exactly rocket science but I can't seem to rewrite this for loop to another type ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    7

    Rewriting a for loop as a while/do-while loop

    Okay, I know this isn't exactly rocket science but I can't seem to rewrite this for loop to another type of loop:

    Code:
    for (; z != n; n = n->l)
    {
          do_something(n);
    }
    The problem is the first condition (at the start) of the for loop is blank, instead of something like for (i = 0; i < 25; i++)... so I'm a little confused as to how the loop works...

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    z is already defined elsewhere.

    Code:
    for(int i = 1; i < 2; ++i)
    {
    }
    is the same as
    Code:
    int i = 1;
    for(; i < 2; ++i)
    {
    }
    which is the same as
    Code:
    int i = 1;
    while(i < 2)
    {
    
    //do something
    ++i;
    }

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    In general,
    Code:
    for (init; cond; inc) {
      // loop body
    }
    is equivalent to
    Code:
    init;
    while (cond) {
      // loop body
      inc;
    }
    Edit: If init is blank, there's no init. If inc is blank, there's no inc. If cond is blank, then cond is assumed true (so for example for (;;) is an infinite loop).

    Edit: Except for the behavior of continue, as K&R2 reminded me. If there's a continue statement in the for loop's body, then inc is executed before the next iteration. In the while loop, that doesn't happen - the next iteration starts immediately.
    Last edited by robatino; 04-27-2007 at 12:24 PM.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    7
    Thank you very much The replies were very useful.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    for ( a ; b ; c ) d;

    Is almost equivalent to

    a;
    while ( b ) d ; c ;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by Salem View Post
    for ( a ; b ; c ) d;

    Is almost equivalent to

    a;
    while ( b ) d ; c ;
    Well, not quite.

    Code:
    a;
    while(b) { d; c; }
    But I understand what you meant.

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    To be more pedantic:
    Code:
    { //the loop itself is in a separate scope (life-time of the loop counter)
        a;
        while (b)
        { // so is the loop body
            d; c;
        }
    }

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And to incorporate the continue case:
    Code:
    {
      a;
      while(b)
      {
        transformed_d;
        incrementer: c;
      }
    }
    where transformed_d is the sequence of statements in d with each "continue;" replaced by "goto incrementer;"
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 01:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM

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