mardi 21 avril 2020

while-loop with an empty body and an assignment in the condition

I recently encountered the following construct on an abandoned PHP code project:


while(($my_var = mt_rand(3, 6))%2 != 0);

(Note the empty body of the loop). This ensures that $my_var is assigned either the number 4 or the number 6.

Is there any (measurable, objective) advantage in using the above construction, over something like

$my_var = mt_rand(2, 3) * 2;

(I am particularly wondering why the author chose to include 3 in the randomization range if the result has to be even anyway.)




Aucun commentaire:

Enregistrer un commentaire