Introduction to Programming/Boolean Variables

From Wikiversity
Jump to navigation Jump to search

Boolean[edit]

Boolean variables use a single bit and thus can store only two values. They can be thought of as on and off or 1 and 0 but they are most often thought of as being true or false. Boolean variables are useful for handling information such as, "Did the customer order cheese on her burger?"

Boolean variables and expressions are of fundamental importance in writing computer programs—you will see why in the very next section.


Boolean Literals[edit]

Many languages do not have either Boolean variables or literals, only Boolean expressions. For those languages that do have them, there are only two, true and false (though the case may vary).

Boolean Expressions[edit]

Within the context of computer programming, most Boolean expressions do not have any Boolean variables or literals, but rather look something like a > 5. Where > is a Boolean operator that causes the left and right hand operands to be evaluated and the resulting values compared. In the case of a > 5, the expression will be true if the variable a contains a value larger than 5 and false otherwise.

Relational Operators[edit]

Most programming languages have some form of the following comparison operators:

Description Java, C, C++ Fortran
Greater than > .GT.
Greater than or equal >= .GE.
Less than < .LT.
Less than or equal <= .LE.
Equal == .EQ.
Not Equal != .NE.

Please note that the type of a > 5 (or its equivalent in other languages) is Boolean. This means that 7 > a > 5 is an illegal expression in most languages because 7 > a is a boolean. If a had a value of 6, then this would evaluate to:

(7 > 6) > 5
(true) > 5

Boolean values cannot be compared to integer values.

Logical Operators[edit]

So how would you write the expression 7 > a > 5 in a computer program? The answer is that it is really two sub-expressions, is 7 > a? and is a > 5? and both have to be true for the full expression to be true.

Most programming languages have Boolean operators for and, or and not.

And is a binary operator that takes two Boolean operands. The result is true if and only if both operands are true.

Or is a binary operator that takes two Boolean operands. The result is false if and only if both operands are false.

Not is a unary operator that takes one Boolean operand. The result is the opposite of the value of the operand.

The logical operators usually have a lower precedence than the relation or comparison operators. So, in Java, then, we could write the expression above as 7 > a && a > 5 where && is the Boolean and operator. For the sake of readability, parenthesis are recommended though, i.e. (7 > a) && (a > 5).

Review of Concepts[edit]

  • relational operator – a binary operator that compares two values (such as integers) and produces a Boolean result
  • logical operator – an operator that takes one or two Boolean values and produces a Boolean result

Exercise[edit]

It's time to take another break and do some hands-on work. Follow the link to the appropriate section for your language—if there is one. Boolean expressions and operators for languages that do not have Boolean variables will be handled in the next section.