Back to Boolean Expressions and if Statements

Lesson PreviewLesson 1 of 8 in this unit

3.1 Boolean Expressions

Part of Boolean Expressions and if Statements in AP® Computer Science A. Read the opening section, then create a free account for the full interactive lesson, practice, and progress tracking.

Boolean expressions are like simple “yes or no” questions that a computer can answer. When you compare two values (e.g., Is 5 greater than 3?), the result is always either true or false. This true-or-false logic is how your computer decides which instructions to execute next: if the condition is true, it follows one path; if it’s false, it follows another. There are quite a few symbols that can be used to form boolean expressions. The first one is the == (equal-to) operator, which tests whether the expression on the left is equal to the expression on the right.

java
int x = 5;
int y = 5; 
boolean isEqual = (x == y);
System.out.println(isEqual);

int a = 5;
int b = 10;
System.out.println(x == y);

int a = 7; 
int b = 7; 
System.out.println(a == b);

Output

true
false
true

Continue the full lesson in Stellar

Create a free account to unlock the remaining sections, checkpoints, course practice, and progress tracking for AP® Computer Science A.