# Do While (Novice Question)

I have this code right here that simulates a coin flip and finds out how many times one can flip head before a tail comes up. I don’t full understand this code and have a few questions.

1. I know that rand picks either 0 or 1 but how does it get added with the ++ since adding 0 would result in the same number?

2. It says if(\$flip) echo H or T respectively. How does that code exactly work? How does the computer know 0 is head and 1 is tail?

\$flipCount = 0;
do {
\$flip = rand(0,1);
\$flipCount ++;
if (\$flip){
echo “H”;
}
else {
echo “T”;
}
} while (\$flip);
\$verb = “were”;
\$last = “flips”;
if (\$flipCount == 1) {
\$verb = “was”;
\$last = “flip”;
}
echo “

There {\$verb} {\$flipCount} {\$last}!

”;

Thanks a lot for your help!

If i’m reading that code correctly, \$flip will always be heads (H) because the condition will always be true since its always a 0 or a 1. For it to work as it should, the condition needs to change to something like

if(\$flip == 1){
echo “H”;
} else {
echo “T”;
}

1. \$flip and \$flipCount are different. \$flip is the result of the flip 0 or 1. \$flipCount is how many time you have flipped the coin. ++ just means increment the variable by 1.

2. if \$flip evaluates to true then echo H (any non 0 number is true) otherwise echo T because \$flip is 0 which is false.