This forum is now read-only. Please use our new forums at discuss.codecademy.com.

While Loops in PHP Forum

View Course » View Exercise

646 points
Picture
Submitted by
sunoy14
over 2 years ago

can somebody explain what's going on in the coin flip code?

can somebody explain what exactly each code is doing in the coin flip game? Thanks.

<?php
$headCount = 0;
$flipCount = 0;
while ($headCount < 3) {
$flip = rand(0,1);
$flipCount ++;
if ($flip){
$headCount ++;
echo "H";
}
else {
$headCount = 0;
echo "T";
}
}
echo "It took {$flipCount} flips!";
?>


4 votes

permalink

I actually wanted to ask the question myself, but reading it again I'm pretty sure I got it so I'll try to explain.

First variables $headCount and $flipCount are set to 0, that's the easy part :)

Then it says:
while $headCount < 3 do this:
set variable $flip randomly to 0 or 1;
add +1 to $flipCount;

then:
if $flip was set randomly to 1 it will read it as TRUE and do the code in the first curly brackets:
$headCount +1
echo H
it stops here and goes back up, so now we have $headCount at 1, which is still <3 so it does the while loop again
if the $flip is set to 1 three times in a row $headCount will no longer be <3 and the while loop will not run, so it will go all the way down to the echo "It took {$flipCount} flips!";

but if $flip was set to 0 it reads it as FALSE and does the else curly brackets which say:
set $headCount to 0 and echo T
Then it goes back up, reads that $headCount is now 0 and starts the while loop again

That's it.

I tried to explain it as simple as I could, I hope you could follow.
Ask if something is not clear.

113 points
26aec2c96350a9b0ded255179c89c78e?s=140&d=retro
Submitted by
Thunderhorse
over 2 years ago

3 Comments

Fd20a96bfb491f4d9fa3804d778193c3?s=140&d=retro abuhassaan over 2 years ago

Thanks. But, how does the computer know what $flip number is 1? I cannot figure it out.

Fd20a96bfb491f4d9fa3804d778193c3?s=140&d=retro abuhassaan over 2 years ago

Is zero not part of $flip variable? how the computer know?

0b1e1ac8ee9fcf549e662382d5c2a570?s=140&d=retro HaggardSage over 2 years ago

Both parameters are part of the numbers randomly selected by the code. Part of PHP code recognizes "rand" as a command to select a random number between and including the two parameters given. In this case 0 and 1. Since there are no integers that exist between 0 and 1, only those two integers can be selected, Whatever random number (between 0 and 1) is generated, the code assigns that value to the $flip variable. It's like placing the numbers in a hat and having you draw one out. Whatever gets pulled from the hat is the value for the $field variable.


1 vote

permalink

if ($flip) {$headcount++} says exactly this:
if $flip is true(1) add 1 to $headcount
The code adds 1 to the current number of $headcount at the beginning of the code, and the while loop starts again to check if $headcount is still < 3

And for the else part:
Ask yourself what do we want the program to do if the random flip gives 0.
We want the program to reset the counting of $headcount and start over. That's what $headCount = 0 does. It sets the $headCount to 0, echoes T and the while loop goes back up to check if the $headCount is < 3.

113 points
26aec2c96350a9b0ded255179c89c78e?s=140&d=retro
Submitted by
Thunderhorse
over 2 years ago


0 votes

permalink

Thanks for the explaination. It's much clearer now :)
One question though. How does the code count the $headcount, H and T?

Suppose I get two "H" in a row, does the code count those two "H" and the number of consecutive returns automatically like $headcount = 2 ? Which code does that? I believe that the code if($flip) {$headcount++} is only to tell the program to check if the condition is ++ from the $flip=rand(0,1)? Or is it telling the program to remember the number the number of consecutive "H" each time? I cannot see a code that tells the program to store the number of times consecutive "H" has been returned. Or does the while loop do that automatically?

And in the code else {$headcount =0}, I believe the $headcount variable is only to tell the program to check if $headcount is a zero from $flip=rand(0,1) (just like $headcount++ was to tell the program to check condition in the "if" statement)? Or is it to tell the program that the $headcount has to be set to zero no matter even if there were already any number of "H" returned?

Please share your suggestions and point me out where I'm wrong. I appreciate the help.
Thanks again :)

646 points
Picture
Submitted by
sunoy14
over 2 years ago


0 votes

permalink

if ($flip) {$headcount++} says exactly
this: if $flip is true(1) add 1 to
$headcount The code adds 1 to the
current number of $headcount at the
beginning of the code, and the while
..........................................................

But in
if ($flip){
$headCount ++;
echo "H";
}
how does the code know that there was an "H" as a result? Since $flip was set to random(0,1);
The $flip condition can be either 0 or 1. So how does the code know the exact condition? There are no specifications given to the code. Only random specification.
Maybe the $heacount++ in if($flip){$headcount++} is instead checking if the result was a ++ instead of 0? Just guessing :)

And maybe the code counts the consecutive results automatically and checks whether the number of results is <3? And if there is a "T" before 3 consecutive "H", it sets the count to zero automatically?

It's getting a bit confusing :D

646 points
Picture
Submitted by
sunoy14
over 2 years ago

1 Comment

0b1e1ac8ee9fcf549e662382d5c2a570?s=140&d=retro HaggardSage over 2 years ago

The result isn't H. The code simple states that if the result is true (or 1) then echo "H". It would be like me telling you whenever I say "hi", you say "bingo". The result can only be 0 or 1. With the rand command, numbers between and including the parameters are randomly selected by the computer, like pulling a number out of a hat. Since the parameters are between 0 and 1 only those are the only options available. If the parameters were between 1 - 100 then any number between those could be randomly generated by the code. When the computer generates the random number between 0 - 1 it places that result in the $filp variable. When we use "rand" we are asking the computer to pick a number between the parameters. I know I was probably redundant in my explanation, but hopefully one of the ways I stated it clicked.