SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    While loop syntax

    Can anyone explain the syntax of the following while loop statement?
    While 1 what? (what is 1 applied to?)

    <?php
    $count = 0;

    while (1)
    {
    echo "Iteration $count<br>";
    if ($count == 10)
    break;
    else
    $count++;
    }
    ?>

  2. #2
    SitePoint Zealot nsr81's Avatar
    Join Date
    Nov 2002
    Location
    B'klyn, NY
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    This while loop is checking the condition inside the body using an if statement. If the condition does not meet, it will go on forever, due to while(1) statment. Which is saying, go on while 1 is true, 1 is alway true so the loop keeps on iterating.

    Perhaps a better way to write the loop would be as follows:
    PHP Code:
    <?
    $count
    =0;
     
    while(
    $count 11)
    {
        echo 
    "Iteration $count<br>";
        
    $count++;
    }
    ?>
    both of them will loop through the body 10 times.
    Nasir
    nasir.us

  3. #3
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nasir explanation about while(1) is fine :-),
    but the loop cycle is done 11 times and not 10 times.
    Also in the first example after the while loop $count will be 10, while in the second example $count will be 11.


    :-) Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  4. #4
    SitePoint Zealot nsr81's Avatar
    Join Date
    Nov 2002
    Location
    B'klyn, NY
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yup, but the loop body is not executed on 11th iteration. That's where the loop stops. I put 11 because the $count was being printed during the loop in the original version, till 10. So the last condition check can be disregarded anyway.
    Nasir
    nasir.us

  5. #5
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand the 'while (1)' concept in itself. I understand it is a conditional statement, but I don't understand why '1' will always be true (true in terms of what) forgive my 'newbieness'

  6. #6
    SitePoint Zealot nsr81's Avatar
    Join Date
    Nov 2002
    Location
    B'klyn, NY
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In most programming languages, the boolean "true" computes down to 1 and boolean "false" computes down to 0. For while() loop to stop, the condition has to be false at some point. And if you put anything that is always true, i.e. any non zero value, the while loop will keep on going. This value can be 1, "a", "13alsd", or anyother non-zero character/digit/string.

    I must also point out that it is very dangerous to use a condition which is always true. If not properly coded, it can lead to infinite loops. Loops that never stop. In worst case, an infinite loop can crash the system.
    Nasir
    nasir.us

  7. #7
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nsr81
    yup, but the loop body is not executed on 11th iteration. That's where the loop stops. I put 11 because the $count was being printed during the loop in the original version, till 10. So the last condition check can be disregarded anyway.
    I think we want to say the same thing,
    when $count is zero, it's iteration number one
    when $count is ten, it's iteration number eleven

    in fact echo will be called 11 times


    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  8. #8
    SitePoint Enthusiast Powerlord's Avatar
    Join Date
    May 2003
    Location
    Mason, MI, USA
    Posts
    37
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nsr81
    Perhaps a better way to write the loop would be as follows:
    PHP Code:
    <?
    $count
    =0;
     
    while(
    $count 11)
    {
        echo 
    "Iteration $count<br>";
        
    $count++;
    }
    ?>
    The best way to write this loop is to not use while at all, but to use for:

    PHP Code:
    for($count 1$count <= 10$count++) {
        echo 
    "Iteration $count<br>";

    Why I'm the first person to mention this mystifies me.
    Ross Bemrose,
    Independant MySQL/Perl/PHP Developer

  9. #9
    SitePoint Zealot nsr81's Avatar
    Join Date
    Nov 2002
    Location
    B'klyn, NY
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because the topic of the discussion was while loop itself
    Nasir
    nasir.us

  10. #10
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Guys! the topic of discussion originally was '(1)' after the word 'while'. I still don't understand what this means. I understand that 'while(etc.)' is a conditional statement, but I don't understand the significance of '1'. What holds the value of '1'?.

    I can understand an expression like 'while ($something = 1)' or 'while (1>$something), but I don't understand the relevance of '1' on its own, it seems like only one half of an equation.

    I really would like to nail this, and I 'm having trouble with it so any help is appreciated.
    Argent
    Last edited by Argent; Jun 3, 2003 at 04:49.

  11. #11
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    Canada
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there is no significance to the 1, it just means execute forever. Now unless you actually want it to execute forever (or rather, until the process is terminated)..that you would probably include a break; in there somewhere:

    ie
    PHP Code:
    while(1) {
    ...
    ...
    break;

    What holds the value of one? I'm not sure exactly what you mean by this but the 1 is just a constant value.

    Nasir explained the boolean logic up above, that should have been enough for you to understand what the 1 does.

  12. #12
    SitePoint Zealot
    Join Date
    Apr 2003
    Location
    Perth W.A.
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, thanks for that. While I don't understand Boolean logic (forgive me) and/or the context in which it was used, the fact that '(1)' after 'while' makes the script execute forever is good enough for me.
    Problem solved ;-)
    Cheers
    Argent

  13. #13
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    Canada
    Posts
    174
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Argent
    OK, thanks for that. While I don't understand Boolean logic (forgive me) and/or the context in which it was used, the fact that '(1)' after 'while' makes the script execute forever is good enough for me.
    Problem solved ;-)
    Cheers
    Argent
    whoa there, it doesn't make the script execute forever, just the portion of the script within the while loop. Just think of it as saying:

    PHP Code:
    while( true ) {
     ...  
    // this stuff will be continously executed
     
    ...  // unless you have a break; in the code
     


  14. #14
    SitePoint Enthusiast hooha's Avatar
    Join Date
    Apr 2002
    Location
    Arizona, USA
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe a more basic explanation of booleans would help. Forgive me if this is an excessive or annoying amount of detail. I can see you've already got the main concepts down, but maybe digging a little deeper into booleans will help (1) make more sense. I remember similar confusion when I was learning this stuff.

    In Boolean algebra [which is what we all put inside while(), if(), etc...], the expression inside the '()' is evaluated by the computer as either true or false. Those are the only 2 possibilities. If the expression can be evaluated at true, the statement block of the while() or if() is executed, otherwise, it isn't executed.

    You can build quite complex conditions with Booleans, consisting of several subexpressions joined by AND [&&], OR [||], and NOT [!=] operators. [I'm assuming you know what those do, but please reply if not...] PHP takes all of these and reduces them to a single true/false value before deciding whether to execute the statement block.

    PHP Code:
    $two 2;
    $three 3;

    (
    $three == 3// is evaluated as (true).
    ($two == 3// is evaluated as (false).
    ($three == || $two == 3// is evaluated as (true || false), hence (true), because OR requires only one expression to be true.
    ($three == && $two == 3//is evaluated as (true && false), hence (false), because AND requires both expressions to be true. 
    In PHP and other loosely-typed languages [more on that in the next paragraph] you can express true/false in several ways. There are the constants true and false [No quotes. Quotes make them strings, which are different from constants, though PHP might also convert 'true' to true - I can't remember... ]. There are also the integers 1 and 0, or '1' and '0' as strings.

    PHP is a loosely-typed language, which means you don't have to explicitly say whether a variable is an integer, string, array, or other type. It also means PHP will sometimes convert your variable to another type depending on the context. Most of this time this doesn't matter too much, but it's an important difference from strongly-typed languages like Java or C++ where you have to explicitly state the type of a variable. You can require PHP to compare data types as well as values [so 1 is not seen as equivalent to '1' or true] using syntax like (1==='1'), which is false.

    PHP Code:
    (1=='1'// true
    (1==='1'// false 
    We're used to looking at expressions with an operator, and a value on either side of the operator, like ($var==2), (3<5), or ($var < 4 || $otherVar ==9). These are the most common kinds of expressions, but PHP doesn't really care about that. What PHP cares about is what the expression can be reduced to - either true or false.

    When PHP sees (1), it reduces it to (true), and there you have it - a true expression in the eyes of PHP, and the code of the statement block [whether while() or if() or whatever] will be executed. You can also legally write something like
    PHP Code:
    while(0
    which is always evaluated as false, and thus the statement block is never executed.

    Because of PHP's automatic type conversion [mentioned above], you can get the same results from statements like
    PHP Code:
    while(true// always true
    while('1'// always true
    if ('0'// always false 
    It looks a little strange at first, but once you get used to the way PHP [and every other language I've ever used] evaluates things, it does make sense.

    Again, if this was way too basic I'm not trying to insult anyones' intellijence .
    *| Now with 98% less stank... |*


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
  •