Search⌘ K
AI Features

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.

Introduction

An algorithm can be divided into three building blocks:

  • Sequencing
  • Selection
  • Iteration

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 operator is ==, the expression will evaluate to true if operand1 and operand2 are equal. If not, then it will return false.
  • If operator is !=, the expression will evaluate to true if operand1 and operand2 are not equal. If they are equal, then it will return false.

Less than or greater than

The following operators can be used to compare two arithmetic values:

  • If operator is <, the expression will evaluate to true if operand1 is less than operand2. If not, then it will return false.
  • If operator is >, the expression will evaluate to true if operand1 is greater than operand2. If not, then it will return false.
  • If operator is <=, the expression will evaluate to true if operand1 is less than or equal to operand2. If not, then it will return false.
  • If operator is >=, the expression will evaluate to true if operand1 is greater than or equal to operand2. If not, then it will return false.