-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
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 (
>=
).– Luiz Felipe
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
– Marco Antonio Sktf10
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
– Marco Antonio Sktf10