1. Logical Data Type: bool
As we've already seen, C# has a super useful operator — if-else. It runs one block of code if the condition in parentheses is true, and another block if the condition is false.
To make it easy to work with expressions that can be true or false, C# added a special type — bool. The main thing about it is that variables of this type can only have two values: true (true) and false (false).
You can't assign any other values to bool variables. The compiler just won't let you.
Examples of bool variables:
bool isAdult = true;
bool hasMoney = false;
A bool variable only has two values — true (true) and false (false). That's it! Kinda like a test: either "passed" or "failed" (oops).
2. Comparison Operations
So why do we need such a basic type? The real power of bool shows up when we build logical expressions. Usually, you get them using comparison operators.
| Operator | Purpose | Example | Value |
|---|---|---|---|
| == | Equals | |
true or false |
| != | Not equals | |
true or false |
| > | Greater than | |
true or false |
| < | Less than | |
true or false |
| >= | Greater or equal | |
true or false |
| <= | Less or equal | |
true or false |
Example:
int age = 20;
bool isAdult = age >= 18; // true (20 is greater or equal to 18)
bool isTeenager = age >= 13 && age < 18; // false (20 is not less than 18)
bool isEven = age % 2 == 0; // true (20 divides by 2 with no remainder)
isAdult here will be true, because 20 is definitely "adult" age.
Important note 1:
You can't split up operators that are made of two symbols.
a < = 10
speed > = max
age = = 18
time ! = 0
Important note 2:
Notice that there are no => or =< operators: only <= and >=. If you write a =< 3, your code just won't compile.
Important note 3:
In C#, you can't write something like 18 < age < 65. The expression 18 < age will be either true or false. And you can't compare true < 65 (different types). At least, not in C#.
3. Logical Operations
In C#, you can't write 18 < age < 65 — that's a syntax error and your program won't compile.
But you can write it like this:
(18 < age) AND (age < 65)
Of course, instead of the word AND you'll use a different logical operator, and that's what we're about to talk about.
C# has three logical operators: AND, OR, and NOT.
Good news: with parentheses, you can build logical expressions as complex as you want.
Bad news: the C# devs decided to use C++-style symbols instead of the words and, or, and not.
| Logical Operator | Expectation | Reality |
|---|---|---|
| AND (∧) | and | && |
| OR (∨) | or | || |
| NOT (¬) | not | ! |
Here are a few examples of logical operators in C#:
| Expression | Translation | Explanation |
|---|---|---|
| (0 < a) && (a < 100) | (0 < a) and (a < 100) | (0 < a) AND (a < 100) |
| (!a) && (!b) | (not a) and (not b) | (NOT a) AND (NOT b) |
| !(!a || !b) | not((not a) or (not b)) | NOT((NOT a) OR (NOT b)) |
4. Truth Tables
Let's quickly go over logical operations.
AND Operator
The AND operator, also known as AND, &&, conjunction, or logical multiplication.
| Expression | Result |
|---|---|
| true && true | true |
| true && false | false |
| false && true | false |
| false && false | false |
In other words, the result is true only if both values in the expression are true. In all other cases, it's false.
OR Operator
The OR operator, also known as OR, ||, disjunction, or logical addition.
| Expression | Result |
|---|---|
| true || true | true |
| true || false | true |
| false || true | true |
| false || false | false |
In other words, the result is true if at least one value in the expression is true. If both are false, the result is false.
NOT Operator
The NOT operator, also known as NOT, !, or inversion.
| Expression | Result |
|---|---|
| ! true | false |
| ! false | true |
It flips true to false and vice versa.
Handy expressions:
| Expression | Result |
|---|---|
| m && !m | false |
| m || !m | true |
| !(a && b) | !a || !b |
| !(a || b) | !a && !b |
5. Logical Operation Examples: and, or, not
The "AND" operation, AND, is written as &&
The result is true only when both variables are true.
int age = 16;
bool hasTicket = true;
bool canAttend = age >= 14 && hasTicket; // true (16 is more than 14 AND has a ticket)
If you're at least 14 AND you have a ticket — you're in!
The "OR" operation, OR, is written as (||)
The result is true if at least one condition is true.
bool isAdmin = false;
bool isModerator = true;
bool canEdit = isAdmin || isModerator; // true (can edit if at least one of these is true)
If you're an admin OR a moderator — go ahead and push those buttons!
The "NOT" operation, NOT, is written as (!)
The inverter flips true to false and vice versa.
bool isWeekend = false;
bool shouldGoToWork = !isWeekend; // true (if it's not the weekend, time to work)
If it's NOT the weekend — time to get up!
Combining: build conditions as complex as you want
You can combine as many logical operations as you want (just don't go overboard, or you'll end up with something like "if he's not NOT not NOT not an admin..."). Parentheses are your friend (and you should use them for clarity!).
Example:
int age = 17;
bool hasTicket = false;
bool isVip = true;
bool canGo = (age >= 18 && hasTicket) || isVip; // true, because isVip = true
In this example, the logic is: if the person is an adult and has a ticket, or if they're VIP — let them in. If they're not VIP — only if they're an adult and have a ticket.
GO TO FULL VERSION