Boolean Expressions

What are boolean expressions? 

First what's a boolean? It is a simple decision-making data type.  A boolean is a value that is true or false, yes or no, 1 or 0. These values can be entered directly into Java expressions using one of two boolean literals: true or false.

Example code for a boolean literal:

It is important to note that the syntax for a boolean is always lowercase. If you make the "b" uppercase then your code will not work.

Java has things called "rational operators" to use in expressions that result in boolean values. The purpose of the operators is to compare two quantities in some way. Rational operators are similar to inequalities. When using them, if the statement is correct/true then the boolean expression value will be true (1) and print "true", if it is not true/false then it's value is false (0) and will print "false". Boolean expressions can also be called rational expressions.

There are six rational operators: < (less than) , <=  (less than or equal to) ,  > (greater than) , >= (greater than or equal to) , == (equal) , != (not equal).

Here are some examples of boolean expressions using Rational Operators:


Remember that the rational operator "!" means not. So in the case of this next example, the rational operator means "not equal to".


You can use boolean expressions with rational operators in math equations as well. If the answer is true then the output will print true and the boolean's value will be true/1. If it is false then the boolean's value will be false/0. Here is an example:



Note that rational operators are second in order of importance in these expressions. Therefore, you have to perform the math operations before you compare the numbers to either side of the operator to determine if the boolean value is true or false.

Comments

Popular Posts