     Next: Control Structures Up: Boolean Expressions (True and Previous: The special value undef   Contents   Index

# Boolean Algebra

Exactly the way numbers can be combined with addition and multiplication, Boolean expressions can be combined with Logical Operators3.1:

 Example Name Result \$a && \$b And \$a if \$a is false, \$b otherwise \$a || \$b Or \$a if \$a is true, \$b otherwise ! \$a Not True if \$a is not true \$a and \$b And \$a if \$a is false, \$b otherwise \$a or \$b Or \$a if \$a is true, \$b otherwise not \$a Not True if \$a is not true \$a xor \$b Xor True if \$a or \$b is true, but not both

With 0 for false and 1 for true, binary addition is almost the same as boolean or. Binary multiplication is almost the same as boolean and. They function exactly the same because binary 10 (decimal 2) is true in perl. Thus, (1 and 1) is true in perl and (1+1) is true in perl.

 + 0 1 * 0 1 0 0 1 0 0 0 1 1 10 1 0 1 ||, or 0 1 &&, and 0 1 0 0 1 0 0 0 1 1 1 1 0 1

The value of the boolean expression can be saved in a variable.

\$boolean = (2+4) eq 6;

print "\$boolean\n";

will print

1
which from the definition of true and false 3.2.1 is true.     Next: Control Structures Up: Boolean Expressions (True and Previous: The special value undef   Contents   Index
Tom Hunt 2002-06-09