Welcome to The Coding College! In this tutorial, we’ll dive into Boolean Expressions in C++, a fundamental concept for decision-making and control flow in programming. Boolean expressions evaluate to either true
or false
and are commonly used with comparison and logical operators.
What Are Boolean Expressions?
A Boolean Expression is an expression that evaluates to a Boolean value (true
or false
). They are widely used in:
- Conditional statements (
if
,else
, etc.). - Loops (
while
,for
, etc.). - Logical operations.
Components of Boolean Expressions
- Comparison Operators
- Logical Operators
- Boolean Variables
1. Comparison Operators
Comparison operators compare two values and return a Boolean result.
Operator | Description | Example | Result |
---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 3 | true |
< | Less than | 3 < 5 | true |
> | Greater than | 5 > 3 | true |
<= | Less than or equal to | 5 <= 5 | true |
>= | Greater than or equal to | 7 >= 8 | false |
Example: Using Comparison Operators
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
cout << "a == b: " << (a == b) << endl;
cout << "a != b: " << (a != b) << endl;
cout << "a < b: " << (a < b) << endl;
cout << "a > b: " << (a > b) << endl;
return 0;
}
Output:
a == b: 0
a != b: 1
a < b: 1
a > b: 0
2. Logical Operators
Logical operators combine Boolean expressions to create more complex conditions.
Operator | Description | Example | Result |
---|---|---|---|
&& | Logical AND (all conditions true) | (a > b) && (c < d) | true |
` | ` | Logical OR (at least one is true) | |
! | Logical NOT (invert true/false) | !(a > b) | false |
Example: Logical Operators in Action
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 20;
cout << "x < y && x > 5: " << ((x < y) && (x > 5)) << endl;
cout << "x > y || y > 15: " << ((x > y) || (y > 15)) << endl;
cout << "!(x < y): " << !(x < y) << endl;
return 0;
}
Output:
x < y && x > 5: 1
x > y || y > 15: 1
!(x < y): 0
3. Boolean Variables
Boolean expressions can involve Boolean variables, which store true
or false
.
#include <iostream>
using namespace std;
int main() {
bool isHot = true;
bool isSunny = false;
cout << "Is it hot and sunny? " << (isHot && isSunny) << endl;
cout << "Is it hot or sunny? " << (isHot || isSunny) << endl;
cout << "Is it not sunny? " << (!isSunny) << endl;
return 0;
}
Output:
Is it hot and sunny? 0
Is it hot or sunny? 1
Is it not sunny? 1
Boolean Expressions in Conditional Statements
Boolean expressions are commonly used in if
, else
, and else if
statements to control program flow.
Example:
#include <iostream>
using namespace std;
int main() {
int score = 85;
if (score >= 90) {
cout << "Grade: A" << endl;
} else if (score >= 75) {
cout << "Grade: B" << endl;
} else {
cout << "Grade: C" << endl;
}
return 0;
}
Output:
Grade: B
Boolean Expressions in Loops
Boolean expressions are used to control the execution of loops.
Example: while
Loop
#include <iostream>
using namespace std;
int main() {
int count = 0;
while (count < 5) {
cout << "Count: " << count << endl;
count++;
}
return 0;
}
Output:
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Example: for
Loop
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) {
cout << i << " is even." << endl;
} else {
cout << i << " is odd." << endl;
}
}
return 0;
}
Output:
0 is even.
1 is odd.
2 is even.
3 is odd.
4 is even.
Boolean Expressions with Functions
Boolean expressions can be returned from functions to encapsulate logic.
Example:
#include <iostream>
using namespace std;
bool isEven(int number) {
return number % 2 == 0;
}
int main() {
int num = 10;
if (isEven(num)) {
cout << num << " is even." << endl;
} else {
cout << num << " is odd." << endl;
}
return 0;
}
Output:
10 is even.
Summary
- Boolean expressions are critical for decision-making and control flow in C++.
- Combine comparison and logical operators to build complex expressions.
- Use them in conditional statements, loops, and functions for dynamic programming.
Explore More at The Coding College
Visit The Coding College to learn more about Boolean expressions, control structures, and advanced C++ concepts.
What’s Next?
- Learn about Boolean functions and recursion.
- Explore error handling with Boolean checks.
- Dive into advanced logical operations in algorithms.