for loop

This is a discussion on for loop within the C++ Programming forums, part of the General Programming Boards category; I have the following line-> for(j=30; j>0; j--) Can this be re-written as -> for(j=30; j; j--) ? I'm not ...

  1. #1
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163

    for loop

    I have the following line->
    for(j=30; j>0; j--)
    Can this be re-written as ->
    for(j=30; j; j--) ?

    I'm not game enough to try it because it is in the middle of a complex algorithm and I already have enough problems without trying something new But as I wrote it I just wondered
    My site to register for all my other websites!
    'Clifton Bazaar'

  2. #2
    Unregistered
    Guest
    no - at least I dont think so. The middle part of the for loop tells the loop when to end, without a value I dont know what it would do

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>>
    I'm not game enough to try it because it is in the middle of a complex algorithm and I already have enough problems without trying something new But as I wrote it I just wondered
    <<<

    So write a simple test program and try it! You'll find you'll be doing that a lot.

    As a hint to the answer, the "middle" term is evaluated as a boolean expression, when an integer has the value zero it is FALSE...

    As a style point though, which is less likely to confuse? Moral, just because something is legal, doesn't mean it should be used.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    33
    well, depends what you want the program to do, the first one is:
    j=30, take away 1 from j as long as j is greater than 0.
    the second one would be:
    j=30, take away 1 from j as long as j is j.

  5. #5
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    Thanks for the replies.

    So write a simple test program and try it! You'll find you'll be doing that a lot.
    The reason I didn't do this is because (as I said before) I am right in the middle of a complex algorithm and didn't want to break my 'concentration', it will be something I will be testing when things slow down a bit around here
    My site to register for all my other websites!
    'Clifton Bazaar'

Popular pages Recent additions subscribe to a feed

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