Back to Big Idea 3: Algorithms & Programming

Lesson PreviewLesson 5 of 18 in this unit

3.5 Boolean Expressions

Part of Big Idea 3: Algorithms & Programming in AP® Computer Science Principles. Read the opening section, then create a free account for the full interactive lesson, practice, and progress tracking.

Boolean expressions play a fundamental role in computer science, as they enable programs to make decisions based on comparisons and logical operations. These expressions are essential in determining how a program should behave when faced with different input values. By evaluating conditions and relationships between variables, Boolean expressions control the flow of a program and make it responsive to various situations.

A Boolean value is a basic concept in programming and can only be one of two possible outcomes: true or false. These values are critical for conditional statements, which allow the program to make decisions. For example, when using a relational operator like a = b, the expression evaluates to true if a and b are equal, or false if they are not. These comparisons are integral to controlling the logic of a program.

Relational operators are used to compare two values, helping to establish relationships between them. These operators include =, ≠, >, <, ≥, and ≤. Each operator serves a specific purpose: for instance, = checks if two values are equal, while > checks if one value is greater than the other. Relational operators always result in a Boolean value, either true or false. So, if a = b, the expression will return true if a and b are indeed equal, and false if they are not. Understanding how these operators work is crucial for developing logic in a program.

For example, consider the following code: a = 5 b = 10 print(a == b) # checks if a equals b print(a > b) # checks if a is greater than b print(a < b) # checks if a is less than b

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 Principles.