DevAcademia lesson/C++ Basics
Boolean Expressions
Topic progressTopic 36 of 109 - 33% complete
~5 min lesson17 learning blocks2 code examples1 checks/interactions
Lesson outline
What are Boolean Expressions?
Boolean expressions are conditions that evaluate to either true or false. They are fundamental for controlling program flow in decision-making constructs such as if statements and loops.
These expressions are typically formed using comparison operators and logical operators.
Comparison Operators
| Operator | Description | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | true |
| != | Not equal to | 5 != 5 | false |
| > | Greater than | 5 > 3 | true |
| < | Less than | 5 < 3 | false |
| >= | Greater than or equal to | 5 >= 5 | true |
| <= | Less than or equal to | 5 <= 3 | false |
Logical Operators
| Operator | Description | Example | Result |
|---|---|---|---|
| && | Logical AND: true only if both operands are true | true && false | false |
| || | Logical OR: true if at least one operand is true | true || false | true |
| ! | Logical NOT: inverts the operand's truth value | !true | false |
Operator Precedence
The order in which operators are evaluated can affect the outcome of complex expressions. Use parentheses to explicitly control evaluation order and make expressions easier to read.
- Parentheses () (highest among these)
- Logical NOT ! (unary)
- Relational: <, <=, >, >=
- Equality: ==, !=
- Logical AND &&
- Logical OR || (lowest among these)
Common Pitfalls
- Don't confuse
=(assignment) with==(equality) in conditions. - Any nonzero integer converts to
true; zero converts tofalse. - Don't mix up logical operators (
&&,||) with bitwise ones (&,|,^).
Was this topic clear?