Results 1 to 6 of 6

Thread: While Loop

  1. #1

    Thread Starter
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    While Loop

    This may be a simple question but i'm trying to do a loop while one value doesn't equal the other but for some reason the loop never runs even if the values = each other. Can anyone help me out?

    PHP Code:
    while ($Number1 != $Number2) {

    $Number2 =  intval(rand(100999));

    $Number1 $Number2;



  2. #2
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: While Loop

    $Number1 = $Number2; statement false' the loop.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: While Loop

    Quote Originally Posted by lintz
    .. but for some reason the loop never runs even if the values = each other.
    That would be correct. Right? If you want the the loop to execute at least once you use do..while:
    PHP Code:
    do {
     
        
    $Number2 =  intval(rand(100999));

        
    $Number1 $Number2;

    } while (
    $Number1 != $Number2); 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: While Loop

    If initially the $Number1 and $Number2 differ on their values, that would at least execute once.

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: While Loop

    But if they are the same initially the while loop wouldn't execute. But I don't get it - they will always be the same, so where is the logic in putting a loop?
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6
    Lively Member
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    95

    Re: While Loop

    If there is no value set for each of the $Number variables before the loop, they would both equal 0, making the while loop not run, because they are equal.

Posting Permissions

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



Click Here to Expand Forum to Full Width