Computers aren’t just for arithmetic calculations; they can also make decisions based on certain conditions. This ability to make decisions allows programs to handle different outcomes. Let’s explore a few everyday scenarios where a computer might need to make decisions:
In each of these examples, the computer is evaluating a question that can be answered with “yes” or “no”, or more precisely, true or false . In programming, we use something called a boolean to represent these two values: true and false.
In C#, the bool
data type is used to store values that can be either true
or false
. This makes it perfect for logical expressions, which are questions or conditions that the computer can evaluate.
Example
bool isRaining = false;
bool hasUmbrella = true;
In this example, isRaining
is set to false
because it’s not raining, while hasUmbrella
is set to true
because you have an umbrella.
The study of how logical expressions are formed and evaluated is called boolean logic. In programming, these logical expressions are constructed using operators and values, and they always result in either true
or false
. Let’s revisit our earlier scenarios to see how they can be expressed as boolean logic:
true
or false
?true
or false
?true
or false
?Just as math has arithmetic operators (like +, -, *, /), C# has operators specifically for boolean logic. These operators allow you to create logical expressions that the computer can evaluate. There are several types of operators:
Equality Operators
Equality == | Result |
---|---|
100 == 100 |
true |
100 == 99 |
false |
"abc" == "abc" |
true |
"ABC" == "abc" |
false |
true == true |
true |
true == false |
false |
Inequality != | Result |
---|---|
100 != 100 |
false |
100 != 99 |
true |
"abc" != "abc" |
false |
"ABC" != "abc" |
true |
true != true |
false |
true != false |
true |
Comparison Operators
Greater/Less than > <
|
Result |
---|---|
100 > 100 |
false |
100 > 99 |
true |
0 < 10 |
true |
0 < -1 |
false |
Greater/Less than or equal >= <=
|
Result |
---|---|
100 >= 100 |
true |
10 >= 99 |
true |
0 <= 0 |
true |
0 <= -1 |
false |
Boolean Logical Operators
There are two important operators in this category that are not listed: logical AND, and logical OR. We will cover these in subsequent lessons.
Logical Negation (NOT) !
|
Result |
---|---|
!true |
false |
!false |
true |
!(100 > 100) |
true |
Let’s look at some examples of boolean expressions in C#:
int age = 18;
bool isOldEnough = age >= 18;
In this example, the expression age >= 18
checks if the age is greater than or equal to 18. If the condition is true, isOldEnough
is set to true
; otherwise, it will be set to false
.
bool isEqual = (1000 == 1000);
Here, we are checking if 1000 is equal to 1000. Since this is obviously true, isEqual
is assigned the value true
.
Boolean logic is essential for enabling computers to make decisions. By using boolean values (true or false) and operators, we can write programs that evaluate conditions and respond accordingly. We’ll dive deeper into boolean logic as we continue, exploring how it can be combined with control flow statements like if statements to create more complex decision-making structures.
// Initialize variables
string name = "Mr. Mortimer";
int age = 30;
double bankAccount = 150.00;
bool isATeacher = true;
bool isAMillionaire = false;
// Assign boolean expressions to variables
bool isMyNameMortimer = name == "Mr. Mortimer";
bool isOldEnoughToDrive = age >= 16;
bool canBuyDodgeChallenger = bankAccount > 31000;
bool isSeniorCitizen = age >= 65;
Console.WriteLine("-Name Checks-");
Console.WriteLine($"Is my name Mr. Mortimer? {isMyNameMortimer}");
Console.WriteLine($"Is my name Mr. Mertens? {name == "Mr. Mertens"}");
Console.WriteLine($"My name is not Mr. Merriman: {name != "Mr. Merriman"}");
Console.WriteLine("\n-Age-Related Checks-");
Console.WriteLine($"Am I old enough to drive? {isOldEnoughToDrive}");
bool isOldEnoughToRentCar = age >= 25;
Console.WriteLine($"Am I old enough to rent a car? {isOldEnoughToRentCar}");
Console.WriteLine($"Am I eligible for a senior citizen discount? {isSeniorCitizen}");
Console.WriteLine("-Bank Account Checks-");
Console.WriteLine($"Do I have enough to buy a Dodge Challenger? {canBuyDodgeChallenger}");
bool hasPositiveBalance = bankAccount > 0;
Console.WriteLine($"Do I have a positive bank balance? {hasPositiveBalance}");
Console.WriteLine("\n-Boolean Checks-");
Console.WriteLine($"{name} is a teacher: {isATeacher}");
Console.WriteLine($"{name} is a millionaire: {isAMillionaire}");
bool isNotMillionaire = !isAMillionaire;
Console.WriteLine($"So, {name} isn't a millionaire? {isNotMillionaire}");
// Bonus: logical AND && and logical OR || (Covered in lesson 2.4)
Console.WriteLine("\n-Logical Operator Examples-");
bool canDriveAndBuyCar = isOldEnoughToDrive && canBuyDodgeChallenger;
Console.WriteLine($"Am I old enough to drive AND have enough to buy a Dodge Challenger? {canDriveAndBuyCar}");
bool canDriveOrBuyCar = isOldEnoughToDrive || canBuyDodgeChallenger;
Console.WriteLine($"Am I old enough to drive OR have enough to buy a Dodge Challenger? {canDriveOrBuyCar}");
-Name Checks-
Is my name Mr. Mortimer? True
Is my name Mr. Mertens? False
My name is not Mr. Merriman: True
-Age-Related Checks-
Am I old enough to drive? True
Am I old enough to rent a car? True
Am I eligible for a senior citizen discount? False
-Bank Account Checks-
Do I have enough to buy a Dodge Challenger? False
Do I have a positive bank balance? True
-Boolean Checks-
Mr. Mortimer is a teacher: True
Mr. Mortimer is a millionaire: False
So, Mr. Mortimer isn't a millionaire? True
-Logical Operator Examples-
Am I old enough to drive AND have enough to buy a Dodge Challenger? False
Am I old enough to drive OR have enough to buy a Dodge Challenger? True
-This demo is just the beginning. Over the next few activities, you’ll see how powerful Boolean expressions can be in decision-making.