Why does "0" not appear? Once the logic of "do while" is: do yourself then check

Asked

Viewed 72 times

-5

If you literally analyze the loop do while, makes perfect sense since something is done while this is true, but pragmatically has no logic.

For example, in the code below, when we arrive at the value 1 (remembering that he enters from the 9 in the do while then I don’t know why you have a 10 there) he prints inside the loop and checks if he (1) is greater than 0, what in fact it is, and if indeed it is, should be printed the 0 soon after, since the condition was met previously and then logically should close the loop since 0 is not greater than 0.

<?php
    $valor1 = 10;

    do {
        echo 'O valor ' . $valor1-- . ' é maior que 0';
        echo '<br>';
    } while ($valor1 > 0);

    echo 'fim do script';
?>

Upshot:

O valor 10 é maior que 0
O valor 9 é maior que 0
O valor 8 é maior que 0
O valor 7 é maior que 0
O valor 6 é maior que 0
O valor 5 é maior que 0
O valor 4 é maior que 0
O valor 3 é maior que 0
O valor 2 é maior que 0
O valor 1 é maior que 0
fim do script
  • 3

    Because 0 is not greater than 0, then the condition evaluates to false. Zero is equal to zero. Therefore, you must use the operator greater than or equal to (>=).

  • yes, but the do performs before evaluating that condition. He first prints the value 0 to then check it and when checking it will turn out false in this case, correct, but he has printed before, at least this was expected. Unless the do is undone if the while checks false but then the structure wouldn’t be itself would be a simple while

  • I’m not even getting into the merits result, tlgd that this is expected, but... it still doesn’t make sense practically only theoretically. But anyway, I’ll let this pass

2 answers

2


Every mistake is made on this premise:

... (remembering that he enters from the 9 in do while then I don’t know why there’s a 10 there...

Wrong, the variable $valor enters into the loop of the-valid 10 and then:

  • is printed.
  • subtracted from 1.
  • compared to 0...
    • If $valor is greater than 0 reiterates.
    • If $valor is not greater than 0 leaves the iteration loop.

That’s because the decrement operator who uses, $valor--, is known as the Post-decrement operator that is governed by this table:

Example Name Effect
--$to Pre-decrement Decrease $a by one, and then return $a.
$to-- Post-decrement Returns $a, and then decreases $a by one.

That is, the contents of the variable will be printed first $valor and only then after printing $valor will be decremented.

With this information the mistake can be easily undone through a table test.
A simplification of your code will be used for the test:

<?php

$valor = 10;   

do{
    //Aqui é aplicado em $valor o operador de pós-incremento ou seja 
    //primeiro é obtido o valor da variável e só então que é decrementada.
    echo 'o valor '. $valor-- .' é maior que O.'.PHP_EOL;
    
} while($valor > 0);

echo 'finalizado';

Where in the test:

  • The column code shows the code being executed.
  • The column $value before shows the contents of the variable valor before the code is executed.
  • The column $value after shows the contents of the variable valor after the code is executed.
  • The column stdout shows what was printed in standard output after code is executed.
code $value before $value after stdout
<?php - - -
$valor = 10; - 10 -
do{ 10 10 -
echo 'o valor '. $valor-- .' é maior que O.'.PHP_EOL; 10 9 value 10 is greater than 0.
} while($valor > 0); 9 9 -
do{ 9 9 -
echo 'o valor '. $valor-- .' é maior que O.'.PHP_EOL; 9 8 9 is greater than 0.
} while($valor > 0); 8 8 -
do{ 8 8 -
echo 'o valor '. $valor-- .' é maior que O.'.PHP_EOL; 8 7 value 8 is greater than 0.
} while($valor > 0); 7 7 -
do{ 7 7 -
echo 'o valor '. $valor-- .' é maior que O.'.PHP_EOL; 7 6 value 7 is greater than 0.
} while($valor > 0); 6 6 -
do{ 6 6 -
echo 'o valor '. $valor-- .' é maior que O.'.PHP_EOL; 6 5 value 6 is greater than 0.
} while($valor > 0); 5 5 -
do{ 5 5 -
echo 'o valor '. $valor-- .' é maior que O.'.PHP_EOL; 5 4 value 5 is greater than 0.
} while($valor > 0); 4 4 -
do{ 4 4 -
echo 'o valor '. $valor-- .' é maior que O.'.PHP_EOL; 4 3 value 4 is greater than 0.
} while($valor > 0); 3 3 -
do{ 3 3 -
echo 'o valor '. $valor-- .' é maior que O.'.PHP_EOL; 3 2 value 3 is greater than 0.
} while($valor > 0); 2 2 -
do{ 2 2 -
echo 'o valor '. $valor-- .' é maior que O.'.PHP_EOL; 2 1 value 2 is greater than 0.
} while($valor > 0); 1 1 -
do{ 1 1 -
echo 'o valor '. $valor-- .' é maior que O.'.PHP_EOL; 1 0 value 1 is greater than 0.
} while($valor > 0); 0 0 -
echo 'finalizado'; 0 0 finalized

1

Are you confusing some concepts of while and do while, as well as the incrementer "i++" (and "i--").

remembering that he enters the paritir of the 9 in the "of the while" so do not know why has a 10 ali kk

Is assigned the value 10 to $valor1, therefore, upon entering the do, the value of that variable É MESMO 10.

On arriving at the 1, values 10 to 2 have already been printed. This is because $valor1--, before the "evaluation" is made and then the decrease. Thus, in the 1st iteration the value is printed 10 and then descend and do while(9 > 0).

Therefore, the iteration started by 1, will still print 1, decrease variable and use 0 in the comparison of while.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.