# if(\$flip)... Why does this work as a Boolean?

This has me a little bit puzzled? shouldn't it be if (\$flip == 1)?

I appreciate that the answer is either 1 or 0 but if \$flip is the 1 or True, what is False?

0 equals false.

Any value different from 0 equals true.

Sondre Em about 5 years ago

Sorry if I'm a bit slow, but I just want to confirm that I understand this right:

So, if I say "flip should be a random number, either 0 or 1".
(ie \$flip = rand(0,1);)

And then say - if(\$flip); then do something, then if the random string generates a 0 then nothing happens, but if it generates a 1, then the "if"-statement goes through?

If it said "rand(0,9)" then 0 would not do the "if", but a 1, 2 [...] 9 would?
And if it said "rand(1,9)", follow by a if(\$flip); - that would be an infinite loop?

Sondre Em about 5 years ago

(uch, my linespaces disappeared. sorry if the reply seems a bit cluttered)

If it said "rand(0,9)" then 0 would not do the "if", but a 1, 2 [...] 9 would?
Yes, that's true.

And if it said "rand(1,9)", follow by a if(\$flip); - that would be an infinite loop?
In this case no, because the condition for the loop is (\$headCount < 3).
But you would always get three heads in three flips, because the condition (\$flip) would always be true.