Boolean Expressions
Explore how Boolean expressions control program flow by evaluating relational operators that return true or false. Understand their role in selection and conditional statements, essential for making decisions in Java coding and the AP Computer Science exam.
We'll cover the following...
This unit focuses on selection, which is performed using conditional statements. Conditional statements give the program the ability to decide and respond appropriately. Boolean variables, relation operators, and logical operators are all involved in building a conditional statement.
What is a boolean expression?
A boolean expression is an expression used to compare the primitive or reference values using relational operators. A boolean expression will always evaluate to a boolean value: true or false. This is the basic syntax:
operand1 operator operand2
The data type of operand1 and operand2 must be the same, and operator can be any relational operator.
Equal to or not equal to
- If
operatoris==, the expression will evaluate totrueifoperand1andoperand2are equal. If not, then it will returnfalse. - If
operatoris!=, the expression will evaluate totrueifoperand1andoperand2are not equal. If they are equal, then it will returnfalse.
Less than or greater than
The following operators can be used to compare two arithmetic values:
- If
operatoris<, the expression will evaluate totrueifoperand1is less thanoperand2. If not, then it will returnfalse. - If
operatoris>, the expression will evaluate totrueifoperand1is greater thanoperand2. If not, then it will returnfalse. - If
operatoris<=, the expression will evaluate totrueifoperand1is less than or equal tooperand2. If not, then it will returnfalse. - If
operatoris>=, the expression will evaluate totrueifoperand1is greater than or equal tooperand2. If not, then it will returnfalse.