Incomputer science, aBoolean expression (also known aslogical expression) is anexpression used inprogramming languages that produces aBoolean value when evaluated. A Boolean value is eithertrue orfalse. A Boolean expression may be composed of a combination of the Boolean constantsTrue/False orYes/No,Boolean-typed variables, Boolean-valued operators, andBoolean-valued functions.[1]
Boolean expressions correspond topropositional formulas in logic and are associated toBoolean circuits.[2]
Mostprogramming languages have the Boolean operatorsOR,AND andNOT; inC and somelanguages inspired by it, these are represented by "||" (double pipe character), "&&" (doubleampersand) and "!" (exclamation point) respectively, while the correspondingbitwise operations are represented by "|", "&" and "~" (tilde).[3] In the mathematical literature the symbols used are often "+" (plus), "·" (dot) andoverbar, or "∨" (vel), "∧" (et) and "¬" (not) or "′" (prime).
Some languages, e.g.,Perl andRuby, have two sets of Boolean operators, with identical functions but different precedence. Typically these languages useand,or andnot for the lower precedence operators.
Some programming languages derived fromPL/I have a bit string type and use BIT(1) rather than a separate Boolean type. In those languages the same operators serve for Boolean operations and bitwise operations. The languages represent OR, AND, NOT and EXCLUSIVE OR by "|", "&", "¬" (infix) and "¬" (prefix).
Some programming languages, e.g.,Ada, haveshort-circuit Boolean operators. These operators use alazy evaluation, that is, if the value of the expression can be determined from the left hand Boolean expression then they do not evaluate the right hand Boolean expression. As a result, there may beside effects that only occur for one value of the left hand operand.
5 > 3 is evaluated astrue.3 > 5 is evaluated asfalse.5>=3 and3<=5 are equivalent Boolean expressions, both of which are evaluated astrue.X > 3), and often more (X > Y).