A program loop question....which color wins?

Discussion in 'PHP' started by sunnyboy1984, Feb 26, 2007.

  1. #1
    Hi:

    can anyone please explain how many times does the following program loop? Please list the values of $move and $color at the end of each loop, and explain each result if possible. Thank you.



    <?php
    $move = 0;
    $color = "red";
    $turns = 0;
    while ($move <= 10) {
    if ($turns > 100) {
    #oops, infinite loop!
    echo "Abort!";
    break;
    }
    else if ($move < 4 && $move <> 2) {
    $move += 3;
    $color = "green";
    }
    else if ($move == 8 AND $color == "red") {
    $move--;
    $color = "yellow";
    }
    else if ($color == "yellow" XOR $move >= 7) {
    $move++;
    $color = "blue";
    }
    else {
    $move += 2;
    $color = "red";
    }
    $turns++;
    }
    ?>
     
    sunnyboy1984, Feb 26, 2007 IP
  2. Ardolix

    Ardolix Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    First value of $move is 0, so condition ($move < 4 && $move <> 2) is true, second it's 3 (value added in this if-else), then it's become 6 that not satisfy any condition, so it goes to last else, where it's become 8, now it satisfy ($color == "yellow" XOR $move >= 7)...

    Simply, add at the end of loop:
    echo "Loop nr.$turns, \$move $move, \$color $color\n";

    You got:
    Loop nr.1, $move 3, $color green
    Loop nr.2, $move 6, $color green
    Loop nr.3, $move 8, $color red
    Loop nr.4, $move 7, $color yellow
    Loop nr.5, $move 9, $color red
    Loop nr.6, $move 10, $color blue
    Loop nr.7, $move 11, $color blue
     
    Ardolix, Feb 26, 2007 IP
  3. sunnyboy1984

    sunnyboy1984 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Sorry, still not very clear.

    while ($move <= 10) {
    if ($turns > 100) {
    #oops, infinite loop!
    echo "Abort!";
    break;
    }

    for the above part, we get $move = 0, is that right? because $turns is not larger than 100, it is 0.

    else if ($move < 4 && $move <> 2) {
    $move += 3;
    $color = "green";
    }

    for the above part, because $move = 0, now 0+3 = 3, is that right? up to this point, $move = 3 and color = "green" ?

    so how come it then becomes 6? I am lost after this. Please explain more, thank you.
     
    sunnyboy1984, Feb 26, 2007 IP
  4. Ardolix

    Ardolix Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    While condition ($move <= 10) is true the operation repeats. So, the increment repeats, initially $move becomes 3, then second once it becomes 6.

    Look en.wikipedia.org/wiki/while_loop for more details.
     
    Ardolix, Feb 27, 2007 IP
  5. sunnyboy1984

    sunnyboy1984 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Kind of understand.

    Can you give an example showing how to change the conditional statements only to make a different color wins?

    By the way, XOR means to execute the function if the value does not match the condition? so if color is yellow, or move>=7, do not execute?

    else if ($color == "yellow" XOR $move >= 7) {
    $move++;
    $color = "blue";
    }



    Thank you
     
    sunnyboy1984, Mar 2, 2007 IP