SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: 'for' statement

  1. #1
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    'for' statement

    Is this valid?

    PHP Code:
    for($i = ((1) * 3); $i >= 0$i 3
    - Will

  2. #2
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol is this a trick question? (if not please ignore me )

    ok simplify it a little (still the same though):
    PHP Code:
    for($i 3$i >= 0$i 3
    well that, my friend, is an
    infinite loop... $i is always more than 3 cos $i - 3 doesn't actually change the value of $i...

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2002
    Location
    Launceston, Australia
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehehe that's what I thought when looking at this! As far as I can see, it should work - however it will only run through twice (ie first when $i is 3 therefore greater than zero, then gets decremented by 3, equals zero - still >= 0, but then $i = -3, therefore evaluates false).

  4. #4
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jacobkball, i think that if it were like this (note the '-=' instead of just '-'):
    PHP Code:
    for($i 3$i >= 0$i -= 3
    then it'd work like you said...

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2002
    Location
    Launceston, Australia
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd have to disagree with that, since $i -= 3 is the same as $i = $i - 3, which ends up being the same anyway

    Once the initialisation of $i has happened ie where $i = 3, the only operations that then occur on $i in the for loop are the comparison, ie $i >= 0 (which it is, twice, in this case) and the decrementation ie $i - 3 (or $i-=3).

  6. #6
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so you're saying:
    PHP Code:
    for($i 3$i >= 0$i -= 3
    is the same as this:
    PHP Code:
    for($i 3$i >= 0$i 3
    ??

  7. #7
    SitePoint Zealot
    Join Date
    Jan 2002
    Location
    Launceston, Australia
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, maybe I should test these things first before I sprout my big mouth off I apologise to you, redemption - you are correct on all counts. I think I might go to bed now (it's 1.30am here!) - that's my excuse, and I'm sticking to it heheheheeheheh

  8. #8
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe no problem dude!... i do this all the time (screw up i mean ) esp. when it gets late...

    it's 11.30pm here so i'm 2 hours 'clearer' than you... at least you can take consolation in that

  9. #9
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anyone actually tell me how to get the desired effect from the for statement, which is this...

    I want it to repeat as many times as the multiple of 3 is more than or equal to 0 (zero). Therefore, if $i were to equal -3 then the for statement would not run.

    - Will

  10. #10
    PHP Developer W1LL's Avatar
    Join Date
    Apr 2001
    Location
    Leicester, UK
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind guys, the $i -= 3 at the end worked!

    Cheers guys!

    - Will


Bookmarks

Posting Permissions

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