Computers aren’t limited to performing arithmetic calculations. We are able to program computers to make decisions using logical expressions. When a computer can make decisions, it’s able to determine different outcomes in a program. Here are a few example scenarios:
These examples all use instances of decision making. Think of it like a computer being able to answer yes or no, correct or incorrect, or more specifically, True or False. Programming languages have a data type called a boolean that represents the values true
and false
. The study of computing logical expressions is known as boolean logic. Logical expressions in programming result in either a True or False answer. Let’s revisit the examples above:
true
or false
?true
or false
?true
or false
?Just as math has arithmetic operators (+ - * /
), C# has various categories of boolean operators: equality operators, comparison operators, and logical operators. We won’t cover every logical operator in this lesson, but there is more to come.
Here is a brief rundown of the various operators: equality ==
, inequality !=
, greater than (>
), less than (<
), greater than or equal to (>=
), less than or equal to (<=
), and not (!
). Logic expressions can be constructed using different data types combined with these operators. The result will be either true
or false
. Here’s an example:
1000 == 1000
Is 1000 equal to 1000? It is, so the result is the value true
. You can see a number of different examples in the tables below. These are known as truth tables.
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 |
Greater than/Less than (> ,< ) | Result |
---|---|
100 > 100 | false |
100 > 99 | true |
0 < 100 | true |
0 < -1 | false |
Greater than or equal/Less than or equal (>= ,<= ) | Result |
---|---|
100 >= 100 | true |
100 <= 99 | false |
Not (! ) | Result |
---|---|
!true | false |
!false | true |
!(100 > 100) | true |
==
) and inequality (!=
)>
) and less than (<
)>=
) and less than or equal (<=
)!
)string name = "Mr. Mortimer";
int age = 30;
double bankAccount = 150;
bool isATeacher = true;
bool isAMillionaire = false;
Console.WriteLine($"Is my name Mr. Mortimer? {name == "Mr. Mortimer"}");
Console.WriteLine($"Is my name Mr. Mertens? {name == "Mr. Mertens"}");
Console.WriteLine($"My name is not Mr. Merriman: {name != "Mr. Merriman"}");
Console.WriteLine($"Am I old enough to drive? {age >= 16}");
Console.WriteLine($"Do I have enough to buy a Dodge Challenger? {bankAccount > 31000}");
Console.WriteLine($"{name} is a teacher: {isATeacher == true}");
Console.WriteLine($"{name} is a millionaire: {isAMillionaire}");
Console.WriteLine($"So, {name} isn't a millionaire? {!isAMillionaire}");
Is my name Mr. Mortimer? True
Is my name Mr. Mertens? False
My name is not Mr. Merriman: True
Am I old enough to drive? True
Do I have enough to buy a Dodge Challenger? False
Mr. Mortimer is a teacher: True
Mr. Mortimer is a millionaire: False
So, Mr. Mortimer isn't a millionaire? True